dcodeIO / Preprocessor.js

A JavaScript source file preprocessor in pure JavaScript, e.g. to build different versions of a library.
http://dcode.io
Apache License 2.0
72 stars 19 forks source link

Preprocessor.js - A JavaScript preprocessor

Provides a JavaScript source file preprocessor, e.g. to build different versions of a library. It's for example used to build ProtoBuf.js (its build and main script are quite good examples).

Deprecation notice: Preprocessor.js has been deprecated in favor of MetaScript, a much more JavaScripty way for build time meta programming using JavaScript itself as the meta language. Check out the migration guide to get a quick impression of its merits.

Directives

Features

Command line utility

Install via npm: npm -g install preprocessor

Command line

Usage: preprocess sourceFile [baseDirectory] [-myKey[=myValue], ...] [> outFile]

preprocess Source.js . -FULL=true > Source.full.js

API

The API is quite simple:

var result = new Preprocessor(
    mainFileSource,
    baseDirectoryOrIncludes
).process(defines);

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 = require("preprocessor");
var source = "..."; // e.g. through fs.readFile
var pp = new Preprocessor(source, ".");
console.log(pp.process({
    FULL: true
}));

RequireJS / AMD

require(["/path/to/Preprocessor.js"], function(Preprocessor) {
    var source = "..."; // e.g. through fs.readFile / $.ajax
    var pp = new Preprocessor(source, ".");
    console.log(pp.process({
        FULL: true
    }));
});

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:

<script src="https://github.com/dcodeIO/Preprocessor.js/raw/master//raw.github.com/dcodeIO/Preprocessor.js/master/Preprocessor.min.js"></script>
var Preprocessor = dcodeIO.Preprocessor;
var source = "..."; // e.g. through. $.ajax
var pp = new Preprocessor(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 you are on.

Downloads

Documentation

Tests (& Examples) Build Status

License

Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.html