Class: Preprocessor

Preprocessor

Provides pre-processing of JavaScript source files, e.g. to build different versions of a library.

new Preprocessor(source, baseDirOrIncludes, preserveLineNumbers)

Constructs a new Preprocessor.
Parameters:
Name Type Argument Description
source string Source to process
baseDirOrIncludes string | Object.<string,string> <optional>
Source base directory used for includes (node.js only) or an object containing all the included sources by filename. Defaults to the current working directory.
preserveLineNumbers boolean When removing blocks of code, replace the block with blank lines so that line numbers are preserved, as long as #include is not used

Members

<static> DEFINE :RegExp

#define EXPRESSION

<static> ENDIF :RegExp

#endif/#else, #elif EXPRESSION

<static> EXPR :RegExp

Definition expression

<static> IF :RegExp

#ifdef/#ifndef SOMEDEFINE, #if EXPRESSION

<static> INCLUDE :RegExp

#include "path/to/file". Requires node.js' "fs" module.

<static> PUT :RegExp

#put EXPRESSION

baseDir :string

Source base directory.

defines :Array.<string>

Runtime defines.

errorSourceAhead :number

Error reporting source ahead length.

includes :Object.<string, string>

Included sources by filename.

isNode :boolean

Whether running inside of node.js or not.

preserveLineNumbers :boolean

Preserve line numbers when removing blocks of code

source :string

Source code to pre-process.

Methods

<static> addSlashes(str) → {string}

Adds slashes to an unescaped string.
Parameters:
Name Type Description
str string Unescaped string
Returns:
Escaped string
Type
string

<static> evaluate(runtimeDefines, inlineDefines, expr) → {*}

Evaluates an expression.
Parameters:
Name Type Argument Description
runtimeDefines object.<string,string> Runtime defines
inlineDefines Array.<string> | string Inline defines (optional for backward compatibility)
expr string <optional>
Expression to evaluate
Throws:
If the expression cannot be evaluated
Type
Error
Returns:
Expression result
Type
*

<static> indent(str, indent) → {string}

Indents a multi-line string.
Parameters:
Name Type Description
str string Multi-line string to indent
indent string Indent to use
Returns:
Indented string
Type
string

<static> nlToStr(str) → {string}

Transforms a string for display in error messages.
Parameters:
Name Type Description
str string String to transform
Returns:
Type
string

<static> stripSlashes(str) → {string}

Strips slashes from an escaped string.
Parameters:
Name Type Description
str string Escaped string
Returns:
Unescaped string
Type
string

process(defines, verbose) → {string}

Preprocesses.
Parameters:
Name Type Argument Description
defines object.<string,string> Defines
verbose function(string) <optional>
Print verbose processing information to the specified function as the first parameter. Defaults to not print debug information.
Throws:
If the source cannot be pre-processed
Type
Error
Returns:
Processed source
Type
string

toString() → {string}

Returns a string representation of this object.
Returns:
String representation as of "Preprocessor"
Type
string