← Back to mishoo/UglifyJS2

How to Deploy & Use mishoo/UglifyJS2

UglifyJS Deployment & Usage Guide

1. Prerequisites

  • Node.js: Latest stable version required (restart may be needed after installation)
  • npm: Bundled with Node.js
  • Git: For cloning the repository (development/contributing only)

2. Installation

Global CLI Installation

npm install uglify-js -g

Local Programmatic Installation

npm install uglify-js

Development Setup (Contributing)

git clone https://github.com/mishoo/UglifyJS.git
cd UglifyJS
npm install

3. Configuration

UglifyJS is configured via command-line flags or API options. No external configuration files are required.

CLI Configuration Pattern

uglifyjs [input files] [options]

Important: Pass input files first, then options. Use -- to separate options from files when options precede files:

uglifyjs --compress --mangle -- input.js

Key Configuration Options

FlagDescriptionExample
-c, --compress [options]Enable compression--compress pure_funcs=console.log
-m, --mangle [options]Mangle variable names--mangle reserved=['$','jQuery']
--mangle-props [options]Mangle property names--mangle-props builtins,domprops
-b, --beautify [options]Beautify output--beautify quote_style=1
-o, --output <file>Output file (default: STDOUT)-o minified.js
-p, --parse <options>Parser options--parse bare_returns
--annotationsPreserve /*@__PURE__*/ comments
--comments [filter]Preserve comments--comments '/^!/'

API Configuration

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

const options = {
  compress: {
    pure_funcs: ['console.log']
  },
  mangle: {
    reserved: ['$', 'jQuery']
  },
  output: {
    quote_style: 1
  },
  sourceMap: true
};

4. Build & Run

CLI Usage

Basic minification:

uglifyjs input.js -o output.min.js

Full compression pipeline:

uglifyjs input.js --compress --mangle --output output.min.js

Multiple files (same global scope):

uglifyjs file1.js file2.js file3.js --compress --mangle -o bundle.min.js

Read from STDIN:

cat input.js | uglifyjs --compress --mangle > output.min.js

Generate source maps:

uglifyjs input.js --compress --mangle --source-map -o output.min.js

SpiderMonkey AST input/output:

# Input SpiderMonkey AST (JSON)
uglifyjs --parse spidermonkey ast.json -o output.js

# Output SpiderMonkey AST
uglifyjs input.js -o spidermonkey --compress

Programmatic API Usage

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

// Single file
const code = fs.readFileSync('input.js', 'utf8');
const result = UglifyJS.minify(code, {
  compress: true,
  mangle: true,
  output: {
    comments: /^!/
  }
});

if (result.error) {
  console.error(result.error);
} else {
  fs.writeFileSync('output.min.js', result.code);
  if (result.map) {
    fs.writeFileSync('output.min.js.map', result.map);
  }
}

Multiple files with source maps:

const files = {
  'file1.js': fs.readFileSync('file1.js', 'utf8'),
  'file2.js': fs.readFileSync('file2.js', 'utf8')
};

const result = UglifyJS.minify(files, {
  sourceMap: {
    filename: 'bundle.min.js',
    url: 'bundle.min.js.map'
  }
});

Property mangling with cache (for incremental builds):

const cache = {};
const result = UglifyJS.minify(code, {
  mangle: {
    properties: {
      regex: /^_/
    }
  },
  nameCache: cache
});
// Save cache for next build
fs.writeFileSync('cache.json', JSON.stringify(cache));

Development Testing

npm test

5. Deployment

NPM Package Publishing (Maintainers)

npm version [major|minor|patch]
npm publish

CI/CD Integration

GitHub Actions example:

- name: Minify JavaScript
  run: |
    npm install -g uglify-js
    uglifyjs src/*.js --compress --mangle --source-map -o dist/app.min.js

Docker usage:

FROM node:alpine
RUN npm install -g uglify-js
COPY . /app
WORKDIR /app
RUN uglifyjs src/*.js --compress --mangle -o dist/bundle.js

Build System Integration

Package.json scripts:

{
  "scripts": {
    "build": "uglifyjs src/*.js --compress --mangle --source-map -o dist/app.min.js",
    "build:pretty": "uglifyjs src/*.js --beautify -o dist/app.js"
  }
}

6. Troubleshooting

"Unexpected token" or Parse Errors

Cause: Input uses ECMAScript features not supported by UglifyJS.
Solution: Pre-process with Babel before minifying:

babel input.js | uglifyjs --compress --mangle -o output.min.js

API Compatibility Errors

Cause: Using v2 API patterns with v3.
Solution: UglifyJS 3 uses UglifyJS.minify(files, options) returning a Promise/object with code and map properties, not the v2 callback/transform API.

Variable Name Collisions

Cause: Mangled names conflicting with globals.
Solution: Use reserved option:

uglifyjs input.js --mangle reserved=['$','jQuery','exports','require']

Comments Not Preserved

Cause: Compression removes dead code and associated comments.
Solution: Use --comments flag with specific filter:

uglifyjs input.js --compress --comments '/^!|@license|@preserve/' -o output.js

Out of Memory (Large Files)

Cause: Processing very large bundles.
Solution: Increase Node.js memory limit:

node --max-old-space-size=4096 $(which uglifyjs) large-file.js --compress -o output.js

Input File Order Issues

Cause: Files parsed in sequence but dependencies not loaded first.
Solution: Ensure dependency files precede dependent files in the command:

uglifyjs vendor.js app.js --compress --mangle -o bundle.js

Property Mangling Breaking Code

Cause: Built-in properties (DOM, globals) being mangled.
Solution: Exclude built-ins:

uglifyjs input.js --mangle-props reserved=['length','prototype','constructor'] -o output.js