Code Monkey home page Code Monkey logo

preprocessor.js's Introduction

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

  • Includes (always relative to the baseDirectory, defaults to "."):
...
// #include "path/to/file.js"
...
  • Static conditions:
// #ifdef FULL
console.log("Including extension");
// #include "path/to/extension.js"
// #else
console.log("Not including extension");
// #endif
  • Inverse static conditions:
// #ifndef FULL
console.log("Not including extension");
// #else
console.log("Including extension");
// #include "path/to/extension.js"
// #endif
  • Evaluable conditions:
// #if 1==2
console.log("1==2");
// #elif 2==2
console.log("2==2");
// #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
  • Shim compatible (include the script, then use var ByteBuffer = dcodeIO.ByteBuffer;)
  • node.js compatible, also available via npm
  • Closure Compiler ADVANCED_OPTIMIZATIONS compatible (fully annotated, Preprocessor.min.js has been compiled this way, Preprocessor.min.map is the source map)
  • Fully documented using jsdoc3
  • Well tested through nodeunit
  • Zero production dependencies
  • Small footprint

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="//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

preprocessor.js's People

Contributors

bgmort avatar dahmian avatar dcodeio avatar mikefeldman-gsn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

preprocessor.js's Issues

Put variable bug

Bug report.
Stumbled upon critical thing:
test.txt:

// #put NAME

preprocess test.txt -NAME=0.1.1 > result.txt

result.txt:

false

Seems that any passed variables casts to the boolean ones. It’s not the way I thought it supposed to work.
OS is windows 7.

include dont work if in a nested included file

when you include file a.js in folder a
and a.js include b.js in folder b .... and
b trying to include c.js ... the folder is not b but a. Include dont update current folder in nested inclusion

#define + #ifdef don't produce the correct output

Hi,

in the file testprep.js, I tried the following variations:

// #define var DEBUG=1
(or)
// #define DEBUG 1
(or)
// #define DEBUG=1

(and then in the same file):
// #ifdef DEBUG
console.log('debug');
// #else
console.log('nodebug');
// #endif

console.log('after');

However, the output is always the same:
$ preprocess testprep.js

console.log('nodebug');

console.log('after');

So either I'm completely misinterpreting the intent, or it isn't working

invalid multiple elifs

code below prints "2==3".

// #if 1==2
console.log("1==2");
// #elif 2==3
console.log("2==3");
// #elif 3==3
console.log("3==3");
// #endif

Allow noop without preprocess

The idea behind preprocess is very similar to conditional compilation, a JScript feature Microsoft shipped with IE. However, one of its core features was to:

always place conditional compilation code in comments. Consequently, hosts that do not support conditional compilation can ignore it.

Preprocessor doesn't allow this silent failure – if Preprocessor doesn't compile the code, the code intended exclusively for execution based on Preprocessor directives hidden in comments is exposed Javascript and will get executed regardless. This is especially crap for forked conditional logic, where contradictory outcomes will execute one after the other.

The way I'd like to use Preprocessor is to modify a couple of lines in my Javascript app's config if and when I compile it from the command line with certain arguments passed in. I'm wondering if my use case – call it 'noop-by-default' or 'safe-mode' – has any legs on it, or if it's just not intended usage.

Here's an example syntax for the sake of argument:

/* 
#pp
#ifdef FULL
console.log("Including extension");
#include "path/to/extension.js"
#else
console.log("Not including extension");
#endif
#endpp
*/

In the implementation above, #pp signals to capture all code up until #endpp and treat it as a separate execution context where non-pp statements are considered uncommented (where a non-pp statement is simply what's left after all #{token}{statement}'s have been matched). Of course getting this to work is another matter ;)

metascript dont replace preprocessor!!!

Sorry but metascrypt it completely different is not a meta language!

To advice to use metascript is wrong. Tell simply i dont follow anymore this project. It is more correct!

include_once does not work correctely nested in ifdef/ifndef

Imagine the following file:

// #ifndef FLAG
// #include_once "same/file.js"
// #endif

// #ifndef OTHERFLAG
// #include_once "same/file.js"
// #endif

processed with {FLAG:true}

The file "same/file.js" isn't in the result. It gets written at the place where the first include_once occurs but gets removed because of the ifndef. The second time it doesn't get included because it already got included.

Define constants/macros

Feature request.
It’d be very useful to have possibility to define constants/functions right in the code, as Erlang/C preprocessors do.

// #define PI=Math.PI
// #define function RADTODEG(x){return x*180/PI}

var angle = /* #put RADTODEG(3) */;

Error when parsing a "Zero-width no-break" space

I'm downloading a 3rd party file that has a "zero-width no-break" (U+FEFF) space in it and then running preprocess on it. My preprocessor script looks something like this:

define(['jquery'], function ($) {
    /* @include path/to/my/file.js */
});

The resulting file ends up with an invalid character which then causes jshint (and buster-coverage) to puke :P

Would be great if preprocessor could recognize the zero-width no-break character and know what to do with it.

(I actually know next to nothing about this stuff but came across this as I was debugging some errors in the application I'm working on. See http://en.wikipedia.org/wiki/Zero-width_no-break_space)

Thanks!

// #endif is invalid unless it is followed by a new line

When I tried to run the following example from the readme, I get an error:

// #if 1==2
console.log("1==2");
// #elif 2==2
console.log("2==2");
// #endif

Error: Illegal #endif: // #endif...
at Preprocessor.process (/usr/local/lib/node_modules/preprocessor/Preprocessor.js:258:31)

But if I add in a new line after the #endif, then it works:

// #if 1==2
console.log("1==2");
// #elif 2==2
console.log("2==2");
// #endif

It looks like the unit test is also expecting a \n, so that's why it passes. https://github.com/dcodeIO/Preprocessor.js/blob/master/tests/suite.js#L33

Error: Illegal #include (Preprocessor.js:225)

  1. Create a file test.js with the following content:
// #ifndef TEST
// #define var TEST=true
console.log('test');
// #endif
  1. Create a file main.js with the following content:
// #include "test.js"
console.log('testing include');
// #include "test.js"
  1. Preprocess main.js.

Result:
The file test.js is included twice in the output file.
The following error is shown:

C:\programs\nodejs\node_modules\preprocessor\Preprocessor.js:225
                        throw(new Error("Illegal #"+match[2]+": "+this.source.
                              ^
Error: Illegal #include: // #include "test.js
...
    at Preprocessor.process (C:\programs\nodejs\node_modules\preprocessor\Preprocessor.js:225:31)
    at Object.<anonymous> (C:\programs\nodejs\node_modules\preprocessor\bin\preprocess:65:16)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

Expected result:
The file test.js is included only once in the output file.
No errors are shown.

P. S. I see that there's an undocumented directive #include_once for such cases. But still this is an error.

define / ifdef

Does not seem to work:

// #define var X=true
// #ifdef X
console.log('hi')
// #endif

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.