Class: Uglifier

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

Constant Summary collapse

Error =
ExecJS::Error
DEFAULTS =

Default options for compilation

{
  :mangle => true, # Mangle variables names
  :toplevel => false, # Mangle top-level variable names
  :except => [], # 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)
  :unsafe => false, # Optimizations known to be unsafe in some situations
  :copyright => true, # Show copyright message
  :beautify => false, # Ouput indented code
  :beautify_options => {
    :indent_level => 4,
    :indent_start => 0,
    :quote_keys => false,
    :space_colon => 0,
    :ascii_only => 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

#initialize(options = {}) ⇒ Uglifier

Initialize new context for Uglifier with given options

options - Hash of options to override Uglifier::DEFAULTS



47
48
49
50
# File 'lib/uglifier.rb', line 47

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

.compile(source, options = {}) ⇒ Object

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



40
41
42
# File 'lib/uglifier.rb', line 40

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

Instance Method Details

#compile(source) ⇒ Object Also known as: compress

Minifies JavaScript code

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

Returns minified code as String



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

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[: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