Class: Uglifier

### Home Classes Methods

### In Files

- lib/uglifier.rb

### Parent

Object

### Methods

- ::compile
- ::new
- #compile
- #compress

### Files

- LICENSE.txt

### Class/Module Index

Quicksearch

- Uglifier

No matching classes.

# 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 42
    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 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

### 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 59
    def compile(source)
      source = source.respond_to?(:read) ? source.read : source.to_s

      js = []
      js << "var result = '';"
      js << "var source = #{json_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

      js << "ast = UglifyJS.uglify.ast_mangle(ast, #{json_encode(mangle_options)});"

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

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

      js << "result += UglifyJS.uglify.gen_code(ast, #{json_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.