in Javascript, Web development, Windows

How to minify and merge javascript files with UglifyJS 2

UglifyJS is a JavaScript parser, minifier, compressor or beautifier toolkit.

You can find detailed information about UglifyJS and supported parameters on the UglifyJS GitHub site.

1. Install Node.js

We’ll use the Node.js package manager to install UglifyJS. Download and install from nodejs.org

2. Install UglifyJS 2 using NPM in command prompt (cmd)

npm install uglify-js -g

3. Minify a single file

Using command prompt, navigate to the directory where you javascript file is located and run the following command:

uglifyjs --compress --mangle --output {filename}.min.js -- {filename}.js

Replace {filename} with your javascript filename (excluding the .js extension)

4. Merge multiple javascript files to a single minified file (this will reduce the amount of HTTP requests required if you have many javascript files)

Using command prompt, navigate to the directory where you javascript files are located and run the following command:

uglifyjs --compress --mangle --output master.min.js -- {filename1}.js {filename2}.js {filename3}.js

Replace {filenameN} with your javascript filenames (excluding the .js extension). All files will be combined to the master.min.js file.