Preprocess HTML, JavaScript, and other files with directives based off custom or ENV configuration
<head>
<title>Your App
<!-- @if NODE_ENV='production' -->
<script src="some/production/lib/like/analytics.js"></script>
<!-- @endif -->
</head>
<body>
<!-- @ifdef DEBUG -->
<h1>Debugging mode - <!-- @echo RELEASE_TAG --> </h1>
<!-- @endif -->
<p>
<!-- @include welcome_message.txt -->
</p>
</body>var configValue = '/* @echo FOO */' || 'default value';
// @ifdef DEBUG
someDebuggingCall()
// @endifThe 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
<body>
<!-- @exclude -->
<header>You're on dev!</header>
<!-- @endexclude -->
</body>After build
<body>
</body>@if VAR='value'/@endifThis will include the enclosed block if your test passes@ifdef VAR/@endifThis will include the enclosed block if VAR is defined (typeof !== 'undefined')@ifndef VAR/@endifThis will include the enclosed block if VAR is not defined (typeof === 'undefined')@includeThis will include the source from an external file@exclude/@endexcludeThis will remove the enclosed block upon processing@echo VARThis will include the environment variable VAR into your source
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)
<body>
<!-- @if NODE_ENV!='production' -->
<header>You're on dev!</header>
<!-- @endif -->
<!-- @if NODE_ENV='production' -->
<script src="some/production/javascript.js"></script>
<!-- @endif -->
<script>
var fingerprint = '<!-- @echo COMMIT_HASH -->' || 'DEFAULT';
</script>
</body>With a NODE_ENV set to production and 0xDEADBEEF in
COMMIT_HASH this will be built to look like
<body>
<script src="some/production/javascript.js"></script>
<script>
var fingerprint = '0xDEADBEEF' || 'DEFAULT';
</script>
</body>With NODE_ENV not set or set to dev and nothing in COMMIT_HASH, the built file will be
<body>
<header>You're on dev!</header>
<script>
var fingerprint = '' || 'DEFAULT';
</script>
</body>Extended syntax below, but will work without specifying a test
normalFunction();
//@exclude
superExpensiveDebugFunction()
//@endexclude
'/* @echo USERNAME */'
anotherFunction();Built with a NODE_ENV of production :
normalFunction();
'jsoverson'
anotherFunction();Install via npm
$ npm install --save preprocessUse 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 -->';
pp.preprocess(text);
// -> 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
- 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.