Table of Contents
Minification and Uglification in Java Script
Minification is the practice of removing all unnecessary characters from code without changing its functionality.
This includes removing all unnecessary whitespace, newlines, comments, etc.
Here's an example of how you could use it:
npm install terser -g
Minify a file:
terser file.js -o file.min.js
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.
npm install uglify-js -g
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
Using both tools independently:
Terser for minification:
terser file.js -o file.terser.js
uglifyjs file.terser.js -o file.uglify.js -c -m
This can automate the minification and uglification process as part of your regular development and deployment process, and may include other optimizations like tree-shaking to remove unused code.