» npm install uglify-js
Why do people “uglify” code?
Minification removes comments and whitespace. Identifiers are renamed to something shorter where possible. Some minifiers also replace "true" with "!0" and stuff like that.
Tree-shaking is also getting popular in JS-land. Tree-shaking gets rid of the unused stuff. E.g. if a library got 50 functions, but you're only using 3, only these 3 (and whatever these 3 use and so on) gets included in the final package.
Both things are done to reduce the file size.
More on reddit.comDoes uglify-js support es6 and above?
How to use uglify js efficiently?
Does it make sense to do both minify and uglify?
Videos
I was messing around with Firefox dev tools and came across an option to beautify JS code. So I put in google’s homepage domain to beautify it and compared it to viewing page source. Same text but the beautify made it easier to read.
My question is what is the purpose of uglyfying it if there are programs that can beautify it? Is it to save memory or processing power? Please explain in layman’s.
Minification removes comments and whitespace. Identifiers are renamed to something shorter where possible. Some minifiers also replace "true" with "!0" and stuff like that.
Tree-shaking is also getting popular in JS-land. Tree-shaking gets rid of the unused stuff. E.g. if a library got 50 functions, but you're only using 3, only these 3 (and whatever these 3 use and so on) gets included in the final package.
Both things are done to reduce the file size.
White space is option and ignored in javascript. White space takes up file size. Removing white space reduces file size and makes it easier to down load (the term I've heard is minify)
Unless you're referring to something else?
» npm install @sheetjs/uglify-js
There isn't any real distinction between the two. Even UglifyJS calls itself a minification toolkit.
The distinction could be more relevant when comparing JavaScript minification to CSS minification—CSS minification involves only removing whitespace—the original code remains intact.
With JavaScript, it is possible to not only remove whitespace, but also to make transformations to the code, such as truncating variable names to single characters.
Minifying JavaScript not only makes the source smaller; it also makes the code less readable, or obfuscates it. But do not operate under the assumption that minification or uglification, or whatever you want to call it, is a security measure. It isn't encryption. The code is harder to read, but it is not impossible to read, and while it's not usually possible to return minified code back to its original form, it is possible to 'beautify' it and make it more readable.
It doesn't make sense to both minify and uglify, because most minifiers will both remove whitespace and unnecessary characters, as well as obfuscate the code. All you're doing is introducing another build step.
Minifying is just removing unnecessary white-space and redundant like comments and semicolons. And it can be reversed back when needed.
Uglifying is transforming the code into an "unreadable" form by changing variable names, function names, etc., to hide the original content. Once it is used, there isn't any way to reverse it back.
Some libraries, like UglifyJS, does minification when used, by removing unnecessary parts. But in general, uglifying is making the code unreadable.
Minifying your code speeds up your page loading, making visitors and search engines happy, and it is also readable. Most major browsers minify the code before execution.