class Uglifier

A wrapper around the UglifyJS interface

Constants

DEFAULTS

Default options for compilation

ES5FallbackPath

ES5 shims source path

Error

Error class for compilation errors.

JS

JavaScript code to call UglifyJS

SourcePath

UglifyJS source path

SplitFallbackPath

String.split shim source path

VERSION

Current version of Uglifier.

Public Class Methods

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

Minifies JavaScript code using implicit context.

@param source [IO, String] valid JS source code. @param options [Hash] optional overrides to Uglifier::DEFAULTS @return [String] minified code.

# File lib/uglifier.rb, line 149
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.

@param source [IO, String] valid JS source code. @param options [Hash] optional overrides to Uglifier::DEFAULTS @return [Array(String, String)] minified code and source map.

# File lib/uglifier.rb, line 158
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

@param options [Hash] optional overrides to Uglifier::DEFAULTS

# File lib/uglifier.rb, line 165
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(uglifyjs_source)
end

Public Instance Methods

compile(source) click to toggle source

Minifies JavaScript code

@param source [IO, String] valid JS source code. @return [String] minified code.

# File lib/uglifier.rb, line 177
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

@param source [IO, String] valid JS source code. @return [Array(String, String)] minified code and source map.

# File lib/uglifier.rb, line 186
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 235
def comment_options
  case comment_setting
  when :all, true
    true
  when :jsdoc
    "jsdoc"
  when :copyright
    encode_regexp(/(^!)|Copyright/i)
  when Regexp
    encode_regexp(comment_setting)
  else
    false
  end
end
comment_setting() click to toggle source
# File lib/uglifier.rb, line 250
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 226
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 311
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 291
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 301
def encode_regexp(regexp)
  modifiers = if regexp.casefold?
                "i"
              else
                ""
              end

  [regexp.source, modifiers]
end
mangle_options() click to toggle source
# File lib/uglifier.rb, line 222
def mangle_options
  conditional_option(@options[:mangle], DEFAULTS[:mangle])
end
output_options() click to toggle source
# File lib/uglifier.rb, line 262
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 287
def parse_options
  { :filename => @options[:source_filename] }
end
read_source(source) click to toggle source
# File lib/uglifier.rb, line 214
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 199
def run_uglifyjs(source, generate_map)
  options = {
    :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
  }

  @context.call(Uglifier::JS, options)
end
screw_ie8?() click to toggle source
# File lib/uglifier.rb, line 269
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 277
def source_map_options
  {
    :file => @options[:output_filename],
    :root => @options[:source_root],
    :orig => @options[:input_source_map],
    :map_url => @options[:source_map_url],
    :url => @options[:source_url]
  }
end
uglifyjs_source() click to toggle source
# File lib/uglifier.rb, line 192
def uglifyjs_source
  [ES5FallbackPath, SplitFallbackPath, SourcePath].map do |file|
    File.open(file, "r:UTF-8") { |f| f.read }
  end.join("\n")
end