- Notifications
You must be signed in to change notification settings - Fork20
A JavaScript source file preprocessor in pure JavaScript, e.g. to build different versions of a library.
License
dcodeIO/Preprocessor.js
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Provides a JavaScript source file preprocessor, e.g. to build different versions of a library. It's for example used tobuildProtoBuf.js (itsbuildandmain script are quite good examples).
Deprecation notice: Preprocessor.js has been deprecated in favor ofMetaScript,a much more JavaScripty way for build time meta programming using JavaScript itself as the meta language. Check out themigration guide to get a quick impression of its merits.
- Includes (always relative to the
baseDirectory, defaults to "."):
...// #include "path/to/file.js"...- Static conditions:
// #ifdef FULLconsole.log("Including extension");// #include "path/to/extension.js"// #elseconsole.log("Not including extension");// #endif
- Inverse static conditions:
// #ifndef FULLconsole.log("Not including extension");// #elseconsole.log("Including extension");// #include "path/to/extension.js"// #endif
- Evaluable conditions:
// #if 1==2console.log("1==2");// #elif 2==2console.log("2==2");// #endif
- Inline variables and functions:
// #define var PI=Math.PI// #define function RADTODEG(x){return x*180/PI}varangle=// #put RADTODEG(3)+";"
Writing the result of evaluated expressions:
varversion=// #put '"'+VERSION+'";"'varstr=// #put "\"Hello world!\";"varonePlusOne=// #put (1+1)+";"
- CommonJS compatible
- RequireJS/AMD compatible
- Shim compatible (include the script, then use var ByteBuffer = dcodeIO.ByteBuffer;)
- node.js compatible, also available vianpm
- Closure Compiler ADVANCED_OPTIMIZATIONS compatible (fully annotated,
Preprocessor.min.jshas been compiled this way,Preprocessor.min.mapis the source map) - Fully documented usingjsdoc3
- Well tested throughnodeunit
- Zero production dependencies
- Small footprint
Install via npm:npm -g install preprocessor
Usage:preprocess sourceFile [baseDirectory] [-myKey[=myValue], ...] [> outFile]
preprocess Source.js. -FULL=true> Source.full.js
The API is quite simple:
varresult=newPreprocessor(mainFileSource,baseDirectoryOrIncludes).process(defines);
withbaseDirectoryOrIncludes being either a string containing the path to the base directory or an object of includedsources by filename. When running in a browser, only the later is supported.
varPreprocessor=require("preprocessor");varsource="...";// e.g. through fs.readFilevarpp=newPreprocessor(source,".");console.log(pp.process({FULL:true}));
require(["/path/to/Preprocessor.js"],function(Preprocessor){varsource="...";// e.g. through fs.readFile / $.ajaxvarpp=newPreprocessor(source,".");console.log(pp.process({FULL:true}));});
Note: To use the#include directive in the browser, do not specify the base directory but an object of includedsources by filename:
<scriptsrc="//raw.github.com/dcodeIO/Preprocessor.js/master/Preprocessor.min.js"></script>
varPreprocessor=dcodeIO.Preprocessor;varsource="...";// e.g. through. $.ajaxvarpp=newPreprocessor(source,{"./includes/extension.js":"var myVar = 2;"// <- #include "includes/extension.js"});alert(pp.process({FULL:true}));
Using includes instead of a base directory like shown in the example above is supported regardless of the platform youare on.
Apache License, Version 2.0 -http://www.apache.org/licenses/LICENSE-2.0.html
About
A JavaScript source file preprocessor in pure JavaScript, e.g. to build different versions of a library.
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.

