class Uglifier

A wrapper around the UglifyJS interface

Constants

DEFAULTS

Default options for compilation

ES5FallbackPath

ES5 shims source path

Error
JS
SourcePath

UglifyJS source patch

SplitFallbackPath

String.split shim source path

VERSION

Public Class Methods

compile(source, options = {}) click to toggle source

Minifies JavaScript code using implicit context.

source should be a String or IO object containing valid JavaScript. options contain optional overrides to Uglifier::DEFAULTS

Returns minified code as String

# File lib/uglifier.rb, line 136
def self.compile(source, options = {})
  new(options).compile(source)
end
compile_with_map(source, options = {}) click to toggle source

Minifies JavaScript code and generates a source map using implicit context.

source should be a String or IO object containing valid JavaScript. options contain optional overrides to Uglifier::DEFAULTS

Returns a pair of [minified code as String, source map as a String]

# File lib/uglifier.rb, line 146
def self.compile_with_map(source, options = {})
  new(options).compile_with_map(source)
end
new(options = {}) click to toggle source

Initialize new context for Uglifier with given options

options - Hash of options to override Uglifier::DEFAULTS

# File lib/uglifier.rb, line 153
def initialize(options = {})
  (options.keys - DEFAULTS.keys - [:comments, :squeeze, :copyright])[0..1].each do |missing|
    raise ArgumentError, "Invalid option: #{missing}"
  end
  @options = options
  @context = ExecJS.compile(File.open(ES5FallbackPath, "r:UTF-8").read +
                            File.open(SplitFallbackPath, "r:UTF-8").read +
                            File.open(SourcePath, "r:UTF-8").read)
end

Public Instance Methods

compile(source) click to toggle source

Minifies JavaScript code

source should be a String or IO object containing valid JavaScript.

Returns minified code as String

# File lib/uglifier.rb, line 168
def compile(source)
  run_uglifyjs(source, false)
end
Also aliased as: compress
compile_with_map(source) click to toggle source

Minifies JavaScript code and generates a source map

source should be a String or IO object containing valid JavaScript.

Returns a pair of [minified code as String, source map as a String]

# File lib/uglifier.rb, line 178
def compile_with_map(source)
  run_uglifyjs(source, true)
end
compress(source)
Alias for: compile

Private Instance Methods

comment_options() click to toggle source
# File lib/uglifier.rb, line 219
def comment_options
  case comment_setting
  when :all, true
    true
  when :jsdoc
    "jsdoc"
  when :copyright
    encode_regexp(/Copyright/)
  when Regexp
    encode_regexp(comment_setting)
  else
    false
  end
end
comment_setting() click to toggle source
# File lib/uglifier.rb, line 234
def comment_setting
  if @options.has_key?(:output) && @options[:output].has_key?(:comments)
    @options[:output][:comments]
  elsif @options.has_key?(:comments)
    @options[:comments]
  elsif @options[:copyright] == false
    :none
  else
    DEFAULTS[:output][:comments]
  end
end
compressor_options() click to toggle source
# File lib/uglifier.rb, line 210
def compressor_options
  defaults = conditional_option(
    DEFAULTS[:compress],
    :global_defs => @options[:define] || {},
    :screw_ie8 => @options[:screw_ie8] || DEFAULTS[:screw_ie8]
  )
  conditional_option(@options[:compress] || @options[:squeeze], defaults)
end
conditional_option(value, defaults) click to toggle source
# File lib/uglifier.rb, line 297
def conditional_option(value, defaults)
  if value == true || value.nil?
    defaults
  elsif value
    defaults.merge(value)
  else
    false
  end
end
enclose_options() click to toggle source
# File lib/uglifier.rb, line 273
def enclose_options
  if @options[:enclose]
    @options[:enclose].map do |pair|
      pair.first + ':' + pair.last
    end
  else
    false
  end
end
encode_regexp(regexp) click to toggle source
# File lib/uglifier.rb, line 287
def encode_regexp(regexp)
  modifiers = if regexp.casefold?
                "i"
              else
                ""
              end

  [regexp.source, modifiers]
end
json_encode(obj) click to toggle source
# File lib/uglifier.rb, line 283
def json_encode(obj)
  JSON.dump(obj)
end
mangle_options() click to toggle source
# File lib/uglifier.rb, line 206
def mangle_options
  conditional_option(@options[:mangle], DEFAULTS[:mangle])
end
output_options() click to toggle source
# File lib/uglifier.rb, line 246
def output_options
  DEFAULTS[:output].merge(@options[:output] || {}).merge(
    :comments => comment_options,
    :screw_ie8 => screw_ie8?
  ).reject { |key, _| key == :ie_proof }
end
parse_options() click to toggle source
# File lib/uglifier.rb, line 269
def parse_options
  { :filename => @options[:source_filename] }
end
read_source(source) click to toggle source
# File lib/uglifier.rb, line 198
def read_source(source)
  if source.respond_to?(:read)
    source.read
  else
    source.to_s
  end
end
run_uglifyjs(source, generate_map) click to toggle source

Run UglifyJS for given source code

# File lib/uglifier.rb, line 185
def run_uglifyjs(source, generate_map)
  @context.exec(Uglifier::JS % json_encode(
    :source => read_source(source),
    :output => output_options,
    :compress => compressor_options,
    :mangle => mangle_options,
    :parse_options => parse_options,
    :source_map_options => source_map_options,
    :generate_map => generate_map,
    :enclose => enclose_options
  ))
end
screw_ie8?() click to toggle source
# File lib/uglifier.rb, line 253
def screw_ie8?
  if (@options[:output] || {}).has_key?(:ie_proof)
    false
  else
    @options[:screw_ie8] || DEFAULTS[:screw_ie8]
  end
end
source_map_options() click to toggle source
# File lib/uglifier.rb, line 261
def source_map_options
  {
    :file => @options[:output_filename],
    :root => @options[:source_root],
    :orig => @options[:input_source_map]
  }
end