Your browser does not support JavaScript!
Upvotes Anonymous 2 User 0

How to Minify Your JavaScript Using UglifyJS2 (Windows)

Want to decrease the size of your JavaScript files so they load faster? It's easy to minify your JavaScript files with UglifyJS2, a NodeJS module that you can install as a command-line tool.
Install NodeJS
Instructions for installing NodeJS are here.
Install UglifyJS2
Open up a command prompt (e.g. from the Start menu by typing cmd):
and run the following command (copied from here) to install UglifyJS2 as a command-line application:
npm install uglify-js -g
Minify!
Now you can take some sample JavaScript (e.g. c:\test\sample.js):
(function() {
    var message = 'alert this message';
    alert(message);
})();
and run a command to compress and mangle the JavaScript, with the output stored in sample.min.js:
uglifyjs sample.js -c -m -o sample.min.js
Running it looks like this:
The sample.min.js file now contains the minified JavaScript:
!function(){var a="alert this message";alert(a)}();
To save typing, I often create a .bat file to issue the command.

That's it. Now you can easily minify your JavaScript.
Was this helpful?

Tags

Comments

Leave a Comment

I agree to the Terms of Service
Design © 2015, Downranked, LLC.,
Original user code contributions under MIT License