In Files

Parent

Class/Module Index [+]

Quicksearch

Uglifier

Constants

DEFAULTS

Default options for compilation

ES5FallbackPath
Error
SourcePath

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 41
def self.compile(source, options = {})
  self.new(options).compile(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 48
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

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 58
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 <<       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";      }
  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
Also aliased as: compress
compress(source) click to toggle source
Alias for: compile

[Validate]

Generated with the Darkfish Rdoc Generator 2.