Uglifier

Ruby wrapper for UglifyJS JavaScript compressor.

UglifyJS currently is extensively tested with ES5, but also includes experimental ES6/ES2015+/Harmony support.

More stable alternatives for working with ES6 code is to first transpile to ES5 with e.g. babel-transpiler or using Closure Compiler to directly minify ES6 code.

ES6 / ES2015+ / Harmony mode

When using Uglifier with ES6 syntax without any options, an error will be thrown.

Uglifier::Error: Unexpected token: punc ((). To use ES6 syntax, harmony mode must be enabled with Uglifier.new(:harmony => true).

The experimental ES6 syntax support can be enabled by passing :harmony => true option to Uglifier.

Uglifier.compile(js, harmony: true)

Rails

When used in Rails, replace

config.assets.js_compressor = :uglifier

with

config.assets.js_compressor = Uglifier.new(harmony: true)

in config/environments/production.rb.

Installation

Uglifier is available as a ruby gem.

$ gem install uglifier

Ensure that your environment has a JavaScript interpreter supported by ExecJS. Using therubyracer gem is a safe choice if a runtime isn't already present. Note that while JScript built-in Windows 7 and older works, it is extremely slow.

Usage

require 'uglifier'

Uglifier.new.compile(File.read("source.js"))
# => js file minified

# Or alternatively
Uglifier.compile(File.read("source.js"))

Uglifier also supports generating source maps:

uglified, source_map = Uglifier.new.compile_with_map(source)

When initializing UglifyJS, you can tune the behavior of UglifyJS by passing options. For example, if you want disable variable name mangling:

Uglifier.new(:mangle => false).compile(source)

# Or
Uglifier.compile(source, :mangle => false)

Available options and their defaults are

{
  :output => {
    :ascii_only => true,        # Escape non-ASCII characters
    :comments => :copyright,    # Preserve comments (:all, :jsdoc, :copyright, :none, Regexp (see below))
    :inline_script => false,    # Escape occurrences of </script in strings
    :quote_keys => false,       # Quote keys in object literals
    :max_line_len => 32 * 1024, # Maximum line length in minified code
    :bracketize => false,       # Bracketize if, for, do, while or with statements, even if their body is a single statement
    :semicolons => true,        # Separate statements with semicolons
    :preserve_line => false,    # Preserve line numbers in outputs
    :beautify => false,         # Beautify output
    :indent_level => 4,         # Indent level in spaces
    :indent_start => 0,         # Starting indent level
    :width => 80,               # Specify line width when beautifier is used (only with beautifier)
    :preamble => nil,           # Preamble for the generated JS file. Can be used to insert any code or comment.
    :wrap_iife => false         # Wrap IIFEs in parenthesis. Note: this disables the negate_iife compression option.
    :shebang =>

When passing a regular expression to the output => comments option, be sure to pass a valid Ruby Regexp. The beginning and ending of comments are removed and cannot be matched ( , , //). For example: When matching

/*!
   comment
  /

use Uglifier.new(output: {comments: /^!/}).

Development

Tests are run using

bundle exec rake

See CONTRIBUTING for details about working on and contributing to Uglifier.

© Ville Lautanala. Released under MIT license, see LICENSE for details.