Preprocessor.js - A JavaScript preprocessor
Provides a JavaScript source file preprocessor.
Directives
- Includes (always relative to the
baseDirectory
, defaults to "."):
...// #include "path/to/file.js"...
- Static conditions:
// #ifdef FULLconsole;// #include "path/to/extension.js"// #elseconsole;// #endif
- Inverse static conditions:
// #ifndef FULLconsole;// #elseconsole;// #include "path/to/extension.js"// #endif
- Evaluable conditions:
// #if 1==2console;// #elif 2==2console;// #endif
- Inline variables and functions:
// #define var PI=Math.PI// #define function RADTODEG(x){return x*180/PI}var angle = // #put RADTODEG(3)+";"
-
Writing the result of evaluated expressions:
var version = // #put '"'+VERSION+'";"'var str = // #put "\"Hello world!\";"var onePlusOne = // #put (1+1)+";"
Features
- CommonJS compatible
- RequireJS/AMD compatible
- node.js compatible, also available via npm
- Fully documented using jsdoc3
- Well tested through tape
- Zero production dependencies
- Small footprint
Command line utility
Install via npm: npm -g install preprocessor
Command line
Usage: preprocess sourceFile [baseDirectory] [-myKey[=myValue], ...] [--verbose] [> outFile]
preprocess Source.js . -FULL=true > Source.full.js
API
The API is quite simple:
var result = mainFileSource baseDirectoryOrIncludes;
with baseDirectoryOrIncludes
being either a string containing the path to the base directory or an object of included
sources by filename. When running in a browser, only the later is supported.
node.js / CommonJS
var Preprocessor = ;var source = "..."; // e.g. through fs.readFilevar pp = source ".";console;
RequireJS / AMD
;
Browser / shim
Note: To use the #include
directive in the browser, do not specify the base directory but an object of included
sources by filename:
var Preprocessor = nashdotPreprocessor;var source = "..."; // e.g. through. $.ajaxvar pp = source "./includes/extension.js": "var myVar = 2;" // <- #include "includes/extension.js";;
Using includes instead of a base directory like shown in the example above is supported regardless of the platform you are on.
Downloads
Documentation
Tests (& Examples)
License
Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.html