How-to: Using YUI to auto-compress/minify JavaScript
Submitted by Monday, 25 January, 2010 - 17:23
on
Yahoo's YUICompressor is my favorite JavaScript compression/minifier utility.
I wanted to hit a button w/in JEdit and have my source javascripts run through the compressor.
I made a copy* of the Run_Script.bsh macro script and modified as follows
else if(mode.equals("javascript")) {
newpath = buffer.getPath();
if (newpath.contains(".source")) {
newpath = newpath.replace(".source", "");
myPathToYUICompressor = "/var/www/yui/yuicompressor-2.4.2/build/yuicompressor-2.4.2.jar";
execScript("YUI Compressor", "java -jar " + myPathToYUICompressor + " " + path + " -o " + newpath);
} else {
execScript("Windows Script Host", "wscript " + path);
}
}
You will of course need to have a copy of the YUI compressor and my need to edit the myPathToYUICompressor var above.
Additionally, as written, the code assumes you name your javascript sources with '.source' in the filename. Modify to '-uncompressed' or somesuch as to your practice.
*For a windows install, locate 'Run_Script.bsh' in the as-installed JEdit directory.
-Windoz: C:\Program Files\jEdit\macros\Misc\Run_Script.bsh
-LINUX: probably eg /usr/share/jedit/ (try >> whereis jedit)
Copy 'Run_Script.bsh' into your personal ~/.jedit/ directory. I did so and also renamed it to 'Run_Skript.bsh' so that all of my customized jEdit scripts are in my personal directory, hidden from jedit upgrades.
example:
w/in jedit, i am editing /path/to/my/JS/myJS.source.js
hitting the macro with the uncompressed javascript file in the buffer produces /path/to/my/JS/myJS.js as a compressed version.
I wanted to hit a button w/in JEdit and have my source javascripts run through the compressor.
I made a copy* of the Run_Script.bsh macro script and modified as follows
else if(mode.equals("javascript")) {
newpath = buffer.getPath();
if (newpath.contains(".source")) {
newpath = newpath.replace(".source", "");
myPathToYUICompressor = "/var/www/yui/yuicompressor-2.4.2/build/yuicompressor-2.4.2.jar";
execScript("YUI Compressor", "java -jar " + myPathToYUICompressor + " " + path + " -o " + newpath);
} else {
execScript("Windows Script Host", "wscript " + path);
}
}
You will of course need to have a copy of the YUI compressor and my need to edit the myPathToYUICompressor var above.
Additionally, as written, the code assumes you name your javascript sources with '.source' in the filename. Modify to '-uncompressed' or somesuch as to your practice.
*For a windows install, locate 'Run_Script.bsh' in the as-installed JEdit directory.
-Windoz: C:\Program Files\jEdit\macros\Misc\Run_Script.bsh
-LINUX: probably eg /usr/share/jedit/ (try >> whereis jedit)
Copy 'Run_Script.bsh' into your personal ~/.jedit/ directory. I did so and also renamed it to 'Run_Skript.bsh' so that all of my customized jEdit scripts are in my personal directory, hidden from jedit upgrades.
example:
w/in jedit, i am editing /path/to/my/JS/myJS.source.js
hitting the macro with the uncompressed javascript file in the buffer produces /path/to/my/JS/myJS.js as a compressed version.