User Tools

Site Tools


java-script:minification-uglification

This is an old revision of the document!


Minification and Uglification in Java Script

Minification and uglification are techniques to make your JavaScript files smaller, and sometimes obfuscate the code, so it's harder to read. This can lead to faster load times and a little extra security through obscurity.

Minification

Minification is the practice of removing all unnecessary characters from code without changing its functionality.
This includes removing all unnecessary whitespace, newlines, comments, etc.

Terser is a popular tool for minifying modern JavaScript files.
Here's an example of how you could use it:
Install it:

npm install terser

Minify a file:

npx terser file.js -o file.min.js

Uglification

Uglification is a more extreme form of minification that can also mangle variable names, making the code harder to read and understand. This can have some small additional benefits in terms of file size and can make reverse-engineering your code a little more challenging.

Uglify is a popular tool for both minification and uglification.

Install it:

npm install uglify-js

Uglify a file using both compression (-c) and name mangling (-m):

uglifyjs file.js -o file.min.js -c -m

Using both minification and uglification for a file

We can use both Terser and UglifyJS to minify a JavaScript file, but not simultaneously on the same command line. They are separate tools that achieve similar purposes but may have slight differences in the way they handle the minification process.

Using both tools independently:
Terser for minification:

terser file.js -o file.terser.js

Then uglification:

uglifyjs file.terser.js -o file.uglify.js -c -m
java-script/minification-uglification.1691599546.txt.gz · Last modified: 2023/08/09 19:45 by odefta