UglifyJS

3.19.3 · active · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically minify JavaScript code using `uglify-js`, including common compression and mangling options, and handling potential errors or warnings.

const UglifyJS = require('uglify-js');

const code = `
function greet(name) {
  const message = 'Hello, ' + name + '!';
  console.log(message);
  if (name === 'World') {
    return 'Special greeting';
  }
  return 'General greeting';
}

const myName = 'World';
greet(myName);
`;

const options = {
  compress: {
    dead_code: true,
    drop_console: true,
    reduce_vars: true
  },
  mangle: {
    toplevel: true,
    properties: false,
  },
  output: {
    comments: 'some',
    beautify: false,
  },
};

const result = UglifyJS.minify(code, options);

if (result.error) {
  console.error('Uglification Error:', result.error);
} else {
  console.log('Minified code:\n', result.code);
  if (result.warnings) {
    console.warn('Uglification Warnings:', result.warnings);
  }
}

view raw JSON →