UglifyJS is a robust JavaScript parser, minifier, compressor, and beautifier toolkit, currently at stable version 3.19.3. It is primarily designed for ECMAScript 5 (ES5) and most modern JavaScript language features, often receiving frequent bug fixes and performance improvements as seen in its recent release cadence. For newer or 'more exotic' ECMAScript features (e.g., beyond ES2015 without transpilation), developers are advised to pre-process their code with a transpiler like Babel before passing it to UglifyJS. Key differentiators for version 3 include a simplified programmatic API and command-line interface compared to its predecessor, UglifyJS 2, and a continued focus on efficient code size reduction through advanced compression and mangling techniques.
npm install uglify-jsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to programmatically minify JavaScript code using `uglify-js`, including common compression and mangling options, and handling potential errors or warnings.
Refer to the official UglifyJS 3 documentation for the new API and CLI usage. Specifically, `minify()` is the primary programmatic function.
Integrate a transpilation step (e.g., Babel) into your build process to convert modern JavaScript syntax to ES5 or a compatible target before `uglify-js` processes the files.
If your input is CommonJS, ensure you explicitly disable ES module parsing by setting the appropriate option (e.g., `parse: { module: false }` in the `minify` options, or avoiding `--module` on the CLI) or verify your code's compatibility with ES module parsing.Always use the `reserved` option within `mangle.properties` to specify property names that should *never* be mangled. For DOM properties, consider `domprops: false` or carefully use the `builtins` option. Thorough testing after mangling is essential.
Set the `parse.bare_returns: true` option to allow return statements outside of functions. This is particularly useful for CommonJS modules or userscripts wrapped by an external caller.
Upgrade `uglify-js` to version 2.6.0 or greater to mitigate ReDoS vulnerabilities. Regularly updating to the latest stable version is recommended for security patches.
Pre-process your JavaScript files with a transpiler such as Babel to transform modern syntax into ES5 before passing them to `uglify-js`. Ensure your build configuration (e.g., Webpack, Gulp) includes this step.
For CommonJS, use `const UglifyJS = require('uglify-js');` and then `UglifyJS.minify(code, options);`. For ESM, use `import { minify } from 'uglify-js';` and then `minify(code, options);`.Include `parse: { bare_returns: true }` in your `minify` options to instruct UglifyJS to tolerate `return` statements outside of functions, which is common in certain module patterns.Use the `reserved` option within `mangle.properties` to specify a list of property names that should never be mangled. If mangling DOM properties, set `domprops: false`. Thoroughly test your application after enabling property mangling.
No dependency data recorded yet.