Combining and compressing JavaScript in any web project is a necessity in most cases. Let me show you how you could use YUI Compressor to combine and compress JavaScripts files automatically for you when using NetBeans with a PHP project.
The key to this solution is in the “Project Properties” run configuration settings. Its typical to have it set to “Local Web Site” or similar so the start page shows up in your browser when pressing F6 (Run). You can still have this, and have some automation connected to it as well.
Download YUI Compressor
First of all we need to download the YUI Compressor JAR-file from Github. Then put it in your project root directory (for simplicity in this tutorial).
Configure NetBeans
Open up the project properties dialog and change the “Run As” combo box to “Script (run in command line)”. Uncheck the “Use Default PHP Interpreter” checkbox so that we can change the “PHP Interpreter” textbox. In the textbox you enter /bin/sh
(I am using Linux in this case). Set the “Index File” to run.sh
. The result will look like below:
Now we have a shell file that executes when the project is run, that means we can pretty much do anything before opening up the web page.
Automation script
Lets create the run.sh
file in our project root folder:
nano run.sh
In it we will put actions that we would like to be performed on Run:
java -jar yuicompressor-2.4.8.jar \
--type js \
--nomunge \
-o output.min.js \
input1.js \
input2.js
google-chrome http://mypage.com/
First we run the YUI Compressor with options of our liking. Then we use Google Chrome to open up the project in a web browser. This way we can still have it open in a web browser when the previous task finishes.
And there you have it, automated YUI Compressor in NetBeans!