npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tiny-preprocessor

v1.0.0

Published

A tiny DEV vs PROD preprocessor for JS and TS code

Downloads

2

Readme

Tiny Preprocessor

A tiny DEV vs PROD preprocessor for JS and TS code

It's super annoying to have to strip out logging and debug code, or to have to clean up values and function arguments that were changed to make debugging and testing easier during development, particularly if your development-only code is necessary for ongoing development between releases.

The library exports a single function -- preprocess -- which takes a string argument (your source code), transforms it according to preprocessor comments you've included, then returns the result.

Insert it into your preferred build pipeline so that your source is transformed before being passed to any transpilers, compilers, or just directly copied to a distributable folder. Be sure to run your test suite after preprocessing, to make sure you haven't accidentally broken your library with erroneous preprocessor comments.

During development, compile and test using whatever regular process you use outside of production builds, and keep the production build process separate for when you're ready to release.

Preprocessor Syntax:

Preprocessor blocks are simply IF or IF-ELSE constructs. They start with ## DEV|PROD, and are terminated with another ## token. // ## DEV ## comments affect the entire line. Square brackets [[ and ]] define the start and end of an affected block of code. Both forms of comment syntax are supported, depending on your needs. Typically your production-only code blocks will be contained within block comments, seeing as you don't want them to be active during development.

// --- OMIT A SINGLE LINE ----------------------------------------------------

log("This line will be excluded from the build"); // ## DEV ##

// --- OMIT A WHOLE BLOCK ----------------------------------------------------

// ## DEV [[
console.warn('This line will be excluded.');
write('This line too.');
// ]] ##

// --- USE A DIFFERENT VALUE IN PRODUCTION -----------------------------------

var value = /* ## DEV [[ */ 3 /* ]] ELSE [[ 27 ]] ## */;
// The production build for the above line will render as:
var value = 27;

// ## DEV [[
trace.silent(status1);
trace.silent(status2);
/* ]] ELSE [[
trace.verbose(status1);
trace.verbose(status2);
]] ## */

// The production build for the above lines will render as:
trace.verbose(status1);
trace.verbose(status2);

// --- PRODUCTION-ONLY CODE --------------------------------------------------

/* ## PROD [[
trace.verbose(status1);
trace.verbose(status2);
]] ## */

// The production build for the above lines will render as:
trace.verbose(status1);
trace.verbose(status2);

Installation

$ npm install --save-dev tiny-preprocessor

or

$ yarn add --dev tiny-preprocessor

Usage

import {preprocess} from 'tiny-preprocessor';

const input = '... some source code ...';
const output = preprocess(input);

Gulp

I like to use Gulp, so this is how I use tiny-preprocessor:

// gulpfile.js

const {preprocess} = require(`tiny-preprocessor`);
const gulp = require(`gulp`);
const merge = require(`merge2`);
const plumber = require(`gulp-plumber`);
const sourcemaps = require(`gulp-sourcemaps`);
const transform = require(`gulp-transform`);

// ...

function preprocessSourceText(buffer) {
  return preprocess(buffer.toString());
}

function prebuild() {
  return merge([
    gulp.src(`src/**/*.ts`)
      .pipe(plumber())
      .pipe(transform(preprocessSourceText))
      .pipe(gulp.dest(`.build/ts`)), // copy to a temporary build folder (omitted from the repo and npm)

    gulp.src(`tests/**/*.ts`)
      .pipe(plumber())
      .pipe(transform(preprocessSourceText))
      .pipe(transform(replace(/\.\.\/src/g, `../ts`))) // fix paths so that tests will still compile
      .pipe(gulp.dest(`.build/ts.tests`)),
  ]);
}

// ...

function compile() {
  // your compile function should read from the build folder, e.g. .build/* in this case
  const src = gulp.src(`.build/ts/**/*.ts`)
    .pipe(plumber())
    .pipe(sourcemaps.init())
    // ...
    ;
  
  const tests = gulp.src(`.build/ts.tests/**/*.ts`)
    .pipe(plumber())
    .pipe(sourcemaps.init())
    // ...
    ;
  
  return merge([
    src,
    tests
  ]);
}

gulp.task(`prebuild`, prebuild);
gulp.task(`compile`, [`prebuild`], compile);

// ...