The X Library Compiler

Summary

XC is a Javascript compression and library creation tool. XC scans your application files and creates a custom X library file (with optional compression) which contains only those X variables, functions and objects (X symbols) used in your application.

XC also allows you to create a general-purpose library file containing only the X symbols you specify in a list.

XC's Javascript compression feature can be used on your application files even if you don't use any X symbols.

I welcome any comments/suggestions/bug-reports you may have about XC.

Compiled Application Code

XC optionally compiles all your application '*.js' files into the output file. The compiled app files will precede the compiled library files.

For "*.htm*" files, only the contents of SCRIPT elements are searched for X symbols. Javascript in HTML files is not compiled to the output file and function name obfuscation is not performed. So if you want to use function name obfuscation, all your Javascript should be in "*.js" files and not in the HTML files.

Compression

XC uses several different techniques to achieve optimal compression of the output file.

  • Removes newlines.
  • Replaces tabs with spaces.
  • Removes leading whitespace.
  • Removes sequential spaces when not in a string.
  • Removes spaces that have a delimiter on one side of them, when not in a string.
  • Removes single-line ("//") and multiple-line ("/* */") comments when not in a string. JScript conditional compilation comments are not removed (as of v1.02).
  • Optionally obfuscates library function names with a single character prefix followed by an uppercase base 36 number. By default the prefix is 'X' but you can specify your own obfprefix in the project file. You can specify '_' or '$' or any upper or lower case alpha character.

Object Prefix

X symbols of type 'function' can be prefixed with an object name you specify, so that they become methods of the object. By default the object name is 'X'. The symbol's 'x' is removed and the next character is made lower case. For example xLeft() becomes X.left(), xWidth() becomes X.width(), etc. The XC output file will have the code for the creation of the object: objprefix={};.

Using XC

XC Invocation

XC is a Win32 command-line program (a VC++ project) written in C. It is invoked as follows:

xc prj_name[.ext]

You can use any extension for your project file. If you don't supply an extension on the command line, '.xcp' will be assumed. XC will open the file 'prj_name.ext' in the current directory and create the output files, 'prj_name.js' and 'prj_name.log', also in the current directory.

You can associate 'xc.exe' with all '.xcp' files and then simply double-click on a project file to compile the project.

XC Project File

Format

XC looks for five directives in the project file: options, libpath, obfprefix, objprefix and appfiles. The general format of the project file is as follows.

; Comments are from ';' to the end of the line.

options cmp app obf  ; See option descriptions below.
                     ; This directive is optional.

obfprefix X          ; This character is used as the obfuscation prefix.
                     ; This directive is optional. The default is 'X'.
                     ; It can only be a single character.

objprefix X          ; This string is used as the object prefix.
                     ; This directive is optional. The default is 'X'.
                     ; It can be any legal Javascript identifier.

libpath ..\          ; X library files directory (requires trailing backslash).
                     ; This directive is required.

appfiles             ; Application file pathnames from next line to end of file.
                     ; This directive is required.

App file pathname 1
App file pathname 2
...
App file pathname n

Options

Following the 'options' directive is a space-delimited list of zero or more of the following. Prefix with '-' for false or '+' (or no prefix) for true.

lib Generate output file. Default = true.

cmp Compression applied to output file. Default = true.

app Compiles application js files to output file. Default = false.

obf Obfuscate symbol identifiers. The obfuscation prefix is given by the obfprefix directive. Forces -obj. Default = false.

obj Make functions methods of an object. The object name is given by the objprefix directive. Default = false.

dep Symbol dependents included in output. Default = true. When false it is useful for creating a general-purpose lib file from a list of X symbols. I use -dep to create x_core.js, x_event.js, etc. The list of symbols is put in the xcp file (commented with ';') and the only app file is the xcp file itself. See '/x/x_core.xcp' for an example.

log Generate log file. Default = false.

dbg Write debug info to log file. Forces +log. Default = false.

Building Application Libraries

The following project file builds a library that I use for two demos, floater bar and floater box. In these demos all application Javascript is in the html (php) files. The compiled library is compressed, application js is not compiled, symbols are not obfuscated, and symbol dependents are included.

; XC Project: floater_xlib
options +lib +cmp -app -obf -obj +dep -log -dbg
libpath ..\lib\
appfiles
floater.php
floater_bar.php

The above options correspond to the default option settings of XC, so the project file could also look like the following.

; XC Project: floater_xlib
libpath ..\lib\
appfiles
floater.php
floater_bar.php

Here's another application example. In this example all application js is in the js file, not in the html file. The compiled library is compressed, application js is compiled, symbols are obfuscated, and symbol dependents are included.

; XC Project: v3site
options +lib +cmp +app +obf -obj +dep -log -dbg
libpath x\lib\
appfiles
v3site_src.js

Building General-Purpose Libraries

By setting the 'dep' option to false (symbol dependents not included), XC will create a general-purpose library file. You simply provide XC with a list of symbol identifiers and a library consisting of those symbols (optionally compressed) is created.

All X variables, functions and objects (symbols) are in separate files, but initially they were categorized into x_core.js, x_event.js, etc. For backwards-compatibility I still provide those files, but now I generate those files with XC. In the /x/ directory you will find these files, along with the .xcp file for each. In that directory is also a batch file, build_all.bat, which will run XC on all .xcp files in that directory.

In /x/ you will find a general-purpose library file which is very useful during development. The file x.js contains all X symbols. During offline development, use x.js - this makes all symbols available and you don't have to figure out which library file to use. When you are ready for deployment run XC on your project and now you have an optimized library file containing only the symbols used in your application.

Adding Your Own Functions to the X Library

Adding your own functions to the library is easy once you understand the X library structure.

Related Documents

X Index - X Library Index.

X Quick-Start - Getting Started with the X Library.

X Structure - X Library Structure.

Application Support

Get your questions answered faster by posting at one of the following forums.

SitePoint.com

HFTOnline.com

CodingForums.com

License

By your use of X and/or CBE and/or any Javascript from this site you consent to the GNU LGPL - please read it. If you have any questions about the license, read the FAQ and/or come to the forums.