Projects:JSMin-Compressor

last edited byusericonlmajano on 01-Aug-2010

<< Back to Dashboard | << Projects Viewer

Contents

JSMin Compressor For ColdBox Applications

This is a plugin that interfaces with our own flavor of JSMin to minify css and js files. It can also compress several files of the same type into a single include in order to save even more bandwidth and file space. Of course it is recommended that you only compress into a single file either js or css assets.

Download

You can download the JSMin compressor from ForgeBox: http://www.coldbox.org/index.cfm/forgebox/view/JSMin-Compressor

Configuration Settings

In order for JSMin plugin to work, you must declare the following settings in your ColdBox configuration.

Setting Type Required Default Description
jsmin_enable Boolean false true flag to enable disable the packaging process
jsmin_cacheLocation String true --- The relative file location where cached minified js/css files will be stored, this location will be expanded. ex: includes/cache

Here is an example of the settings:

settings = {
	// JSMIN
	jsmin_enable = true,
	jsmin_cachelocation = "includes/cache",
};

Important: Please make sure that the directory for the cache location is writable and exists. If not, you will receive errors.

Usage

minifiy()

minify(assets:string) : html script or link tags

The main method of operation is minify(). You pass to it a list of assets to compress, but they have to be of the same type: js or css. Do not alternate or weird results will happen. This method returns a script or link include that you would output on your layouts, let's see an example:

<head>
#getMyPlugin("JSMin").minify('includes/js/myscripts.js','includes/js/jquery.js')#
#getMyPlugin("JSMin").minify('includes/css/site.css','includes/css/boxes.css')#
</head>

As you can see from the example above, you can easily render the minified version of all the assets. This plugin will minify each asset and if more than 1 is declared, then it will build a concatenated version of the js or css assets and cache them. When you inspect the source you will see that it includes 1 javascript file and 1 css file. These files are from the disk cache and are named with a unique id.

minifyToHead

minifyToHead(assets:string) : void

This method basically sends the html links and script tags to the head section using cfhtmlhead. You can use this method when calling JSMin via handlers or plugins or any other location than layouts.

So now go out and have fun minifiying with ColdBox!

 
Download in other Formats:
markup Markup | pdf PDF | swf SWF | html HTML | word Word

comments Comments (5)

adam.euans@gmail.com's Gravatar

Adam Euans said

at 09:14:03 AM 24-Dec-2009

The FileOutputStream should be closed on line 203 of JSmin.cfc after compressor has been set. (fos.close();)
adam.euans@gmail.com's Gravatar

Adam Euans said

at 09:19:11 AM 24-Dec-2009

The examples show the minify() function taking multiple arguments instead of just 1 argument as a list. I actually like the multiple arguments better - makes for cleaner code IMO.
lmajano@gmail.com's Gravatar

Luis Majano said

at 12:36:08 PM 25-Dec-2009

Thanks, will add that to the list. Also, yes, multiple arguments is a great thing so it can concatenate the files into a single bundle.
georgemurphy@websbygeorge.com's Gravatar

url George Murphy said

at 06:08:46 AM 01-Aug-2010

Hi Luis, can you please add an example of how to add the 2 required settings to the config file that JSMIN needs. Thanks so much.
lmajano@gmail.com's Gravatar

Luis Majano said

at 03:39:22 PM 01-Aug-2010

Added it George, hope it helps