0% found this document useful (0 votes)
296 views

Javascript Pre Processor

This is a simple JavaScript Preprocessor to enable conditional compilation. The syntax of the preprocessor is a subset of the C preprocessor. It is free for commercial and non-commercial use, except claim it as your own work.

Uploaded by

tezuya
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
296 views

Javascript Pre Processor

This is a simple JavaScript Preprocessor to enable conditional compilation. The syntax of the preprocessor is a subset of the C preprocessor. It is free for commercial and non-commercial use, except claim it as your own work.

Uploaded by

tezuya
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

JavaScript Preprocessor - bramstein.

com

http://www.bramstein.com/projects/preprocess/

Home

Projects

Articles

About

JavaScript Preprocessor
Introduction
This is a simple JavaScript preprocessor to enable conditional compilation. The syntax of the preprocessor is a subset of the C preprocessor. This means that all Jav aScript preprocessor directiv es are valid C preprocessor directiv es and can thus be used with the C preprocessor (but not the other way around.) The following directiv es are supported: #define <identifier> Defines an identifier. #undef <identifier> Remov es a defined identifier, or does nothing it the identifier is not defined. #ifdef <identifier> Tests whether an identifier is defined, and parses the code block until either an #endif or
#else directiv e is reached.

Download
V ersion 0.32 2009-08-10. js-preprocess.js (Uncompressed, 6.9KB) js-preprocess.min.js (Minified, 3.3KB)

Alternatives
js-build-tools jsmacro JavaScript Compiler The C preprocessor

#ifndef <identifier> Tests whether an identifier is not defined, and parses the code block until either an #endif or #else directive is reached. #else A code block executed when #ifdef or #ifndef return false. Code is parsed until an #endif directiv e is reached. #endif Ends a code block. A simple example of some JavaScript code using conditional compilation:

License
The preprocessor is licensed under the new BSD license. To summarize the license; it is completely free for commercial and non-commercial use and you can do with it whatever you want, except claim it as your own work.

#define DEBUG #define SAFE_LOOP function find(object, key) { #ifdef SAFE_LOOP if (object.hasOwnProperty(key)) { return object[key]; } #else if (key in object) { return object[key]; } #endif #ifdef DEBUG console.log('Warning: [find] returning undefined.'); #endif return undefined; }

Note that the definitions do not necessarily need to be in the file, they can also be passed as arguments by your build tool of choice. This makes it easy to generate multiple v ersions of your project tofor example create a debug build, or a specialized version designed to work in restrictiv e environments.

Running from Ant


Include the following code in your Ant build file, and ensure that the refid attribute points to a path reference that includes the Rhino Jav aScript engine. You may also wish to change the src attribute of the scriptdef element to point to the location you have installed the Jav aScript preprocessor.

1 di 3

22/08/2010 21.45

JavaScript Preprocessor - bramstein.com

http://www.bramstein.com/projects/preprocess/

<scriptdef name="preprocess" src="jspreprocess.js" language="javascript"> <classpath> <path refid="js.lib"/> </classpath> <attribute name="defines" /> <attribute name="todir" /> <attribute name="file"/> <attribute name="tofile"/> <element name="fileset" type="fileset" /> </scriptdef>

Once you'v e done that, you can use the preprocessor to pre-process a set of files. Note that the preprocessor will create new files, so if you set your output directory to your input directory it will effectively strip the preprocessor directives from your source code.

<preprocess todir="target" defines="DEBUG"> <fileset dir="src" includes="**/*.js" /> </preprocess>

You may also pre-process a single file using the file and tofile attributes.

<preprocess file="input.js" tofile="output.js" defines="DEBUG, SAFE_LOOP" />

You can pass multiple defines to the preprocessor by separating them with comma's.

Running from the command line using Rhino


You can also run the preprocessor from the command line using the Rhino JavaScript engine. The command line version takes the input file as the first argument and uses the remaining arguments as preprocessor definitions.

> rhino jspreprocess.js input.js [definition1 ...]

The preprocessor writes to the standard output, so to write it to a file you can redirect the standard output to a file as follows:

> rhino jspreprocess.js input.js DEBUG SAFE_LOOP > output.js

The example also shows how two definitions are passed to the preprocessor.

Using the preprocessor from JavaScript


The preprocessor exposes a single function called preprocess: you can simple call this function with as first argument a string (or an array of strings) containing the source code you want preprocessed. The second and optional argument is an object with as keys the predefined definitions you would like the preprocessor to use. The preprocessor will return to you an array of strings as the processed output. This output will hav e all preprocessor directives removed.

var result = preprocess('...', { DEBUG: true, SAFE_LOOP: true });

Frequently Asked Questions


Are there any plans to support #include directives? Not at the present time. I think Jav aScript would be better off with a proper module

2 di 3

22/08/2010 21.45

JavaScript Preprocessor - bramstein.com

http://www.bramstein.com/projects/preprocess/

system, which controls dependencies, file inclusions, and packaging. The JSLint/minification stage in my build process stops working because of the preprocessor directives. The simple solution is to run the preprocessor before running JSLint or minifying your code. Alternativ ely, you could set up a filter in your build process that strips out all the preprocessor directiv es.

Project updates
jQuery column selector A plugin for the jQuery Jav aScript library which adds a table column cell selector.

Latest articles
Adv anced pattern matching in Jav aScript Extracting v alues from Jav aScript objects

jLayout The jLayout Jav aScript library prov ides layout algorithms for laying out components and containers. JUnify JUnify is a Jav aScript library for performing unification on objects and arrays. GUI - Writing your own font engine XSLTJSON XSLTJSON is an XSLT 2.0 stylesheet to transform arbitrary XML to Jav aScript Object Notation (JSON). JSizes A small plugin for the jQuery Jav aScript library which adds support for v arious size related CSS properties. GUI - How selection works Pattern matching in Jav aScript GUI - Getting started

Copyright 2008-2010, Bram St ein. All right s reserved. Home | Project s | Art icles | About

3 di 3

22/08/2010 21.45

You might also like