CFjsmin
CFjsmin (beta) is a ColdFusion component that compresses JavaScript files or strings too a fraction of their original size (sometimes more than 50%). It achieves this by removing all non-essential whitespace and comments.
CFjsmin now has two different input/output modes -- string or file. This means you can do the following combinations:
- String (source) to String (destination)
- File to File
- String to File
- File to String
The component is also useful for packaging multiple JavaScript files into one compressed script file. This can lead to further improvements in speed. Internet Explorer only downloads two assets at a time when loading a page, so the less JavaScript files you have got, the quicker the site will load (we have over 200 individual files in our AJAX application at work, so it really helps us).
I am using a Java class called jsmin, by John Reilly, to actually do the grunt work. I modified the source a little so that it would accept a Reader class as input rather than a Stream, thus enabling the ability to compress strings or files. Because CFFILE is not being used to achieve this, you will find it is pretty quick even when dealing with lots of files.
I am providing the component as a "beta" release, so if you find any issues or have suggestions for improvements, please let me know.
I hope this helps you!
Download
Usage
-
-
<cfscript>
-
oJSMin = createObject("component","jsmin").init();
-
-
// Usage 1: File to file
-
-
// Set the source to be a file
-
oJSMin.setSource("file");
-
-
// Set the destination to be a file, passing filename as second parameter
-
// NOTE: Destination file MUST be different! If it is the same as the
-
// source filename, then the source file will be overwritten.
-
oJSMin.setDestination("file", "compressedFile.js");
-
-
// Compress the source string
-
oJSMin.compress("sourceFile.js");
-
-
// Close the destination stream
-
oJSMin.closeDestination();
-
-
-
// Usage 2: String to string
-
-
// Set the source to be a string
-
oJSMin.setSource("string");
-
-
// Set the destination to be a string
-
oJSMin.setDestination("string");
-
-
// Compress the source string
-
oJSMin.compress(sourceString);
-
-
// Call getString() to get the compressed string
-
compressedString = oJSMin.getString();
-
-
// Close the destination stream
-
oJSMin.closeDestination();
-
-
-
// Usage 3: String to file
-
-
// Set the source to be a string
-
oJSMin.setSource("string");
-
-
// Set the destination to be a file, passing filename as second parameter
-
oJSMin.setDestination("file", "compressedFile.js");
-
-
// Compress the source string
-
oJSMin.compress(sourceString);
-
-
// Close the destination stream
-
oJSMin.closeDestination();
-
-
-
// Usage 4: File to string
-
-
// Set the source to be a file
-
oJSMin.setSource("file");
-
oJSMin.setDestination("string");
-
-
// Compress the source file
-
oJSMin.compress("sourceFile.js");
-
-
// Call getString() to get the compressed string
-
compressedString = oJSMin.getString();
-
-
// Close the destination stream
-
oJSMin.closeDestination();
-
</cfscript>
-
Advanced Usage
You can also provide multiple sources, either multiple strings, files or a mixture of both.
-
-
<cfscript>
-
oJSMin = createObject("component","jsmin").init();
-
-
// Multiple source strings
-
-
// Set the destination to be a file, passing filename as second parameter
-
oJSMin.setDestination("string");
-
-
// Compress a string to the destination string
-
oJSMin.setSource("string");
-
oJSMin.compress(sourceString);
-
oJSMin.compress(sourceString2);
-
-
compressedString = oJSMin.getString();
-
-
// Close the destination stream
-
oJSMin.closeDestination();
-
-
-
// Multiple sources (files and strings)
-
-
// Set the destination to be a file, passing filename as second parameter
-
oJSMin.setDestination("file", "compressedFile.js");
-
-
// Compress a string to the destination file
-
oJSMin.setSource("string");
-
oJSMin.compress(sourceString);
-
-
// Compress a file to the destination file
-
oJSMin.setSource("file");
-
oJSMin.compress("sourceFile.js");
-
-
// Close the destination stream
-
oJSMin.closeDestination();
-
-
</cfscript>
-
Example output
-
-
// Before
-
function foo()
-
{
-
-
// Some comments
-
var a = 2;
-
-
// Some more comments
-
}
-
-
// After
-
function foo()
-
{var a=2;}
-