JavaScript Optimizer - Extensions Syntax

The optimizer's syntax is based on C/C++ preprocessor. Here are all allowed directives:
 
Syntax Description Sample
#define [name] The define directive marks the identifier [name] as existing. Marking an indentifier as existing is used to include JavaScript code fragments conditionally, based on existance/non-existance of given identifier (using #ifdef and #ifndef directives). #define GENERATE_IE_CODE
...
#ifdef GENERATE_IE_CODE
// the code in this block
// will be included
#else
// this code will not be included
#endif
#define [name] [value] This version of the define directive servers two purposes: it marks the identifier [name] as existing and it also will replace all occurrences of text [name] with [value] in all lines of the code following this define directive #define SCRIPT_VERSION 5.0 BETA
...
alert ("Script version is SCRIPT_VERSION");
// the above line will display:
// Script version is 5.0 BETA
#undef [name] Marks the identifier [name] as non-existing (reverses the action of the corresponding #define directive). #define INSERT_DEBUG_CODE
...
#ifdef INSERT_DEBUG_CODE
// this code will be inserted
#endif
...
#undef INSERT_DEBUG_CODE
#ifdef INSERT_DEBUG_CODE
// this code will NOT be inserted
#endif
#allundef Marks all previously defined identifiers as non-existing. #define INSERT_DEBUG_CODE
...
#ifdef INSERT_DEBUG_CODE
// this code will be inserted
#endif
...
#allundef
#ifdef INSERT_DEBUG_CODE
// this code will NOT be inserted
#endif
#ifdef [name]
[code A]
#endif

or

#ifdef [name]
[code A]
#else
[code B]
#endif

Conditionally includes code. In case that an identifier named [name] is existing, [code A] will be included in the optimized version of the code. If using the second variant (with the #else directive), [code A] will be included if [name] exists; otherwise [code B] will be included. Please note that each #ifdef directive must be matched by a closing #endif directive. You can use an #ifdef directive inside other #ifdef directives (thus forming a complex AND condition). #define INSERT_DEBUG_CODE
...
#ifdef INSERT_DEBUG_CODE
// this code will be inserted
#ifdef ABC
// this code will NOT be inserted
// since ABC is not defined
// and we'd need both ABC
// and INSERT_DEBUG_CODE here
#endif
#endif
...
#ifdef INSERT_DEBUG_CODE
// this code will also be inserted
#else
// this code will NOT be inserted
#endif
#ifndef [name]
[code A]
#endif

or

#ifndef [name]
[code A]
#else
[code B]
#endif

Conditionally includes code. In case that an identifier named [name] does NOT exist, [code A] will be included in the optimized version of the code. If using the second variant (with the #else directive), [code A] will be included if [name] DOES NOT exists; otherwise [code B] will be included. Please note that each #ifndef directive must be matched by a closing #endif directive. You can use an #ifndef directive inside other #ifdef or #ifndef directives. #define INSERT_DEBUG_CODE
...
#ifndef ABC
// this code will be inserted
#endif
...
#ifndef INSERT_DEBUG_CODE
// this code will NOT be inserted
#endif
...
#ifndef INSERT_DEBUG_CODE
// this code will NOT be inserted either
#else
// this code will be inserted
#endif
#include "[file]" Includes the contents of file found at location specified by [file]. WARNING: This is not supported in the online version. #include "abc.js"
Tips
  • By adding the #define [name] [value] directive for frequently used function and variable names, you can further optimize the code. E.g. if you have two frequently used functions: AddHighlightToItem and RemoveHighlightFromItem, you can define them as: #define f1 AddHighlightToItem and #define f2 RemoveHighlightFromItem. Now, every time these names appear in your code you save 16 or 21 bytes.
  • Conditionally included code can be very useful if you want to keep your debug code always available on demand; or - generally - if, based on your preferences, you decide to create a few versions of JavaScript code.