Class: Uglifier

Inherits:
Object
  • Object
show all
Defined in:
lib/uglifier.rb

Constant Summary

Error =
ExecJS::Error
DEFAULTS =

Default options for compilation

{
  :mangle => true, # Mangle variables names
  :toplevel => false, # Mangle top-level variable names
  :except => ["$super"], # Variable names to be excluded from mangling
  :max_line_length => 32 * 1024, # Maximum line length
  :squeeze => true, # Squeeze code resulting in smaller, but less-readable code
  :seqs => true, # Reduce consecutive statements in blocks into single statement
  :dead_code => true, # Remove dead code (e.g. after return)
  :lift_vars => false, # Lift all var declarations at the start of the scope
  :unsafe => false, # Optimizations known to be unsafe in some situations
  :copyright => true, # Show copyright message
  :ascii_only => false, # Encode non-ASCII characters as Unicode code points
  :inline_script => false, # Escape </script
  :quote_keys => false, # Quote keys in object literals
  :beautify => false, # Ouput indented code
  :beautify_options => {
    :indent_level => 4,
    :indent_start => 0,
    :space_colon => false
  }
}
SourcePath =
File.expand_path("../uglify.js", __FILE__)
ES5FallbackPath =
File.expand_path("../es5.js", __FILE__)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Uglifier) initialize(options = {})

Initialize new context for Uglifier with given options

options - Hash of options to override Uglifier::DEFAULTS



49
50
51
52
# File 'lib/uglifier.rb', line 49

def initialize(options = {})
  @options = DEFAULTS.merge(options)
  @context = ExecJS.compile(File.open(ES5FallbackPath, "r:UTF-8").read + File.open(SourcePath, "r:UTF-8").read)
end

Class Method Details

+ (Object) compile(source, options = {})

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



42
43
44
# File 'lib/uglifier.rb', line 42

def self.compile(source, options = {})
  self.new(options).compile(source)
end

Instance Method Details

- (Object) compile(source) Also known as: compress

Minifies JavaScript code

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

Returns minified code as String



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/uglifier.rb', line 59

def compile(source)
  source = source.respond_to?(:read) ? source.read : source.to_s

  js = []
  js << "var result = '';"
  js << "var source = #{MultiJson.encode(source)};"
  js << "var ast = UglifyJS.parser.parse(source);"

  if @options[:lift_vars]
    js << "ast = UglifyJS.uglify.ast_lift_variables(ast);"
  end

  if @options[:copyright]
    js << <<-JS
    var comments = UglifyJS.parser.tokenizer(source)().comments_before;
    for (var i = 0; i < comments.length; i++) {
      var c = comments[i];
      result += (c.type == "comment1") ? "//"+c.value+"\\n" : "/*"+c.value+"*/\\n";
    }
    JS
  end

  if @options[:mangle]
    js << "ast = UglifyJS.uglify.ast_mangle(ast, #{MultiJson.encode(mangle_options)});"
  end

  if @options[:squeeze]
    js << "ast = UglifyJS.uglify.ast_squeeze(ast, #{MultiJson.encode(squeeze_options)});"
  end

  if @options[:unsafe]
    js << "ast = UglifyJS.uglify.ast_squeeze_more(ast);"
  end

  js << "result += UglifyJS.uglify.gen_code(ast, #{MultiJson.encode(gen_code_options)});"

  if !@options[:beautify] && @options[:max_line_length]
    js << "result = UglifyJS.uglify.split_lines(result, #{@options[:max_line_length].to_i})"
  end

  js << "return result;"

  @context.exec js.join("\n")
end