Preprocess directives in HTML, JavaScript, etc directives based off variable context

npm install preprocess
484 downloads in the last day
2 762 downloads in the last week
13 235 downloads in the last month

preprocess Build Status

Preprocess HTML, JavaScript, and other files with directives based off custom or ENV configuration

What does it look like?

  <title>Your App

  <!-- @if NODE_ENV='production' -->
  <script src="some/production/lib/like/analytics.js"></script>
  <!-- @endif -->

  <!-- @ifdef DEBUG -->
  <h1>Debugging mode - <!-- @echo RELEASE_TAG --> </h1>
  <!-- @endif -->
  <!-- @include welcome_message.txt -->
var configValue = '/* @echo FOO */' || 'default value';

// @ifdef DEBUG 
// @endif

Directive syntax

Simple syntax

The most basic usage is for files that only have two states, non-processed and processed. In this case, your @exclude directives are removed after preprocessing

    <!-- @exclude -->
    <header>You're on dev!</header>
    <!-- @endexclude -->

After build


Advanced directives

  • @if VAR='value' / @endif This will include the enclosed block if your test passes
  • @ifdef VAR / @endif This will include the enclosed block if VAR is defined (typeof !== 'undefined')
  • @ifndef VAR / @endif This will include the enclosed block if VAR is not defined (typeof === 'undefined')
  • @include This will include the source from an external file. If the included source ends with a newline then the following line will be space indented to the level the @include was found.
  • @exclude / @endexclude This will remove the enclosed block upon processing
  • @echo VAR This will include the environment variable VAR into your source

Extended html Syntax

This is useful for more fine grained control of your files over multiple environment configurations. You have access to simple tests of any variable within the context (or ENV, if not supplied)

    <!-- @if NODE_ENV!='production' -->
    <header>You're on dev!</header>
    <!-- @endif -->

    <!-- @if NODE_ENV='production' -->
    <script src="some/production/javascript.js"></script>
    <!-- @endif -->

    var fingerprint = '<!-- @echo COMMIT_HASH -->' || 'DEFAULT';

With a NODE_ENV set to production and 0xDEADBEEF in COMMIT_HASH this will be built to look like

    <script src="some/production/javascript.js"></script>

    var fingerprint = '0xDEADBEEF' || 'DEFAULT';

With NODE_ENV not set or set to dev and nothing in COMMIT_HASH, the built file will be

    <header>You're on dev!</header>

    var fingerprint = '' || 'DEFAULT';

You can also have conditional blocks that are hidden by default by using the fictional !> end tag instead of --> after your condition:

<!-- @if true !>
<p>Process was run!</p>
<!-- @endif -->

JavaScript, CSS, C, Java Syntax

Extended syntax below, but will work without specifying a test


'/* @echo USERNAME */'


Built with a NODE_ENV of production :




CSS example

body {
/* @if NODE_ENV=='development' */
  background-color: red;
/* @endif */

// @include util.css

(CSS preprocessing supports single line comment style directives)

Shell, PHP


# @include

Configuration and Usage

Install via npm

$ npm install --save preprocess

Use the exposed preprocess method or the convenience file functions. The context, by default, is the current ENV config the process (process.env)

var pp = require('preprocess');

var text = 'Hi, I am <!-- @echo USERNAME -->';

// -> Hi, I am jsoverson

pp.preprocess(text, {USERNAME : "Bob"});
// -> Hi, I am Bob

// Simple wrapper around fs.readFile and fs.writeFile
pp.preprocessFile(src, dest, context, callback);

// Simple wrapper around fs.readFileSync and fs.writeFileSync
pp.preprocessFileSync(src, dest, context);


In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using jshint

Release History

  • 2.0.0 Added ability to echo strings, added conditional comments, removed lodash, merged 17, 13, 15, 16
  • 1.2.0 Added processing for hash-style comments (via @marsch). Added more file aliases.
  • 1.1.0 Added deep inclusion, fixed sequential ifs
  • 1.0.1 Fixed multiple inline echo statements
  • 1.0.0 Pulled from grunt-preprocess to stand alone


Copyright OneHealth Solutions, Inc

Written by Jarrod Overson

Licensed under the Apache 2.0 license.

npm loves you