CompressorJS

RobeeeJay edited this page Oct 23, 2012 · 3 revisions

This module compresses and serves static js content which requires compressing by the UglifyJS compiler. It serves the compressed files using the Lactate node module.

It also has an optional built in pre-processor for including external files.

new fw.CompressorJS({ staticDir: "/static", expireTime: "two days", cacheMem: true,
   updateInterval: 5000, extUncompressed: ".jscript", extCompressed: ".js", preProcessor: true, dontCompress: false});

All arguments apart from staticDir are optional.

updateInterval is the time period between checks that a less file has been changed.

extUncompressed is the extension for uncompressed files, defaults to ".jscript".

extCompressed is the extension for compressed files, defaults to ".js".

preProcessor defaults to false, but when set to true enables the simple pre-processor that allows the following command to be included inside javascript files:

@import "anotherfile.js";

The syntax is the same as for the Less compiler. It also supports protection against endless loops and will throw an error if it detects one.

dontCompress specifies if the resulting output should be compressed. Defaults to false, but useful to set it to true during debugging.

headers is an object containing key/value pairs which represent any custom headers you would like to set, like for example { "Content-Type": "application/javascript; charset=utf-8" }

staticDir should point to the root directory which contains your static content. expireTime is the cache time for the content, see the Lactate documentation for more details. cacheMem (enabled by default) is whether files should be cached to reduce disk access.

Simple Example

var stackFull = new fw.Stack();

stackFull.append(new fw.CompressorJS({ staticDir: __dirname + "/static" }));

If the request.url is "/styles/cat.js" and the file "./static/styles/cat.jscript" exists, this will compress that file into cat.js. If no such Less file exists, it will try to serve cat.js. If that doesn't exist, it will continue to the next entry in the Stack.