Javascript Pre Processor
Javascript Pre Processor
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.
1 di 3
22/08/2010 21.45
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.
You may also pre-process a single file using the file and tofile attributes.
You can pass multiple defines to the preprocessor by separating them with comma's.
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:
The example also shows how two definitions are passed to the preprocessor.
2 di 3
22/08/2010 21.45
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