Class: Uglifier

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

Overview

A wrapper around the UglifyJS interface

Constant Summary collapse

Error =

Error class for compilation errors.

ExecJS::Error
JS =

JavaScript code to call UglifyJS

<<-JS
  (function(options) {
    function comments(option) {
      if (Object.prototype.toString.call(option) === '[object Array]') {
        return new RegExp(option[0], option[1]);
      } else if (option == "jsdoc") {
        return function(node, comment) {
          if (comment.type == "comment2") {
            return /@preserve|@license|@cc_on/i.test(comment.value);
          } else {
            return false;
          }
        }
      } else {
        return option;
      }
    }

    var source = options.source;
    var ast = UglifyJS.parse(source, options.parse_options);
    ast.figure_out_scope();

    if (options.compress) {
      var compressor = UglifyJS.Compressor(options.compress);
      ast = ast.transform(compressor);
      ast.figure_out_scope();
    }

    if (options.mangle) {
      ast.compute_char_frequency();
      ast.mangle_names(options.mangle);
    }

    if (options.enclose) {
      ast = ast.wrap_enclose(options.enclose);
    }

    var gen_code_options = options.output;
    gen_code_options.comments = comments(options.output.comments);

    if (options.generate_map) {
        var source_map = UglifyJS.SourceMap(options.source_map_options);
        gen_code_options.source_map = source_map;
    }

    var stream = UglifyJS.OutputStream(gen_code_options);

    ast.print(stream);

    if (options.source_map_options.map_url) {
      stream += "\\n//# sourceMappingURL=" + options.source_map_options.map_url;
    }

    if (options.source_map_options.url) {
      stream += "\\n//# sourceURL=" + options.source_map_options.url;
    }

    if (options.generate_map) {
        return [stream.toString(), source_map.toString()];
    } else {
        return stream.toString();
    }
  })
JS
SourcePath =

UglifyJS source path

File.expand_path("../uglify.js", __FILE__)
ES5FallbackPath =

ES5 shims source path

File.expand_path("../es5.js", __FILE__)
SplitFallbackPath =

String.split shim source path

File.expand_path("../split.js", __FILE__)
DEFAULTS =

Default options for compilation

{
  # rubocop:disable LineLength
  :output => {
    :ascii_only => true, # Escape non-ASCII characterss
    :comments => :copyright, # Preserve comments (:all, :jsdoc, :copyright, :none)
    :inline_script => false, # Escape occurrences of </script in strings
    :quote_keys => false, # Quote keys in object literals
    :max_line_len => 32 * 1024, # Maximum line length in minified code
    :bracketize => false, # Bracketize if, for, do, while or with statements, even if their body is a single statement
    :semicolons => true, # Separate statements with semicolons
    :preserve_line => false, # Preserve line numbers in outputs
    :beautify => false, # Beautify output
    :indent_level => 4, # Indent level in spaces
    :indent_start => 0, # Starting indent level
    :space_colon => false, # Insert space before colons (only with beautifier)
    :width => 80, # Specify line width when beautifier is used (only with beautifier)
    :preamble => nil # Preamble for the generated JS file. Can be used to insert any code or comment.
  },
  :mangle => {
    :eval => false, # Mangle names when eval of when is used in scope
    :except => ["$super"], # Argument names to be excluded from mangling
    :sort => false, # Assign shorter names to most frequently used variables. Often results in bigger output after gzip.
    :toplevel => false # Mangle names declared in the toplevel scope
  }, # Mangle variable and function names, set to false to skip mangling
  :compress => {
    :sequences => true, # Allow statements to be joined by commas
    :properties => true, # Rewrite property access using the dot notation
    :dead_code => true, # Remove unreachable code
    :drop_debugger => true, # Remove debugger; statements
    :unsafe => false, # Apply "unsafe" transformations
    :conditionals => true, # Optimize for if-s and conditional expressions
    :comparisons => true, # Apply binary node optimizations for comparisons
    :evaluate => true, # Attempt to evaluate constant expressions
    :booleans => true, # Various optimizations to boolean contexts
    :loops => true, # Optimize loops when condition can be statically determined
    :unused => true, # Drop unreferenced functions and variables
    :hoist_funs => true, # Hoist function declarations
    :hoist_vars => false, # Hoist var declarations
    :if_return => true, # Optimizations for if/return and if/continue
    :join_vars => true, # Join consecutive var statements
    :cascade => true, # Cascade sequences
    :negate_iife => true, # Negate immediately invoked function expressions to avoid extra parens
    :pure_getters => false, # Assume that object property access does not have any side-effects
    :pure_funcs => nil, # List of functions without side-effects. Can safely discard function calls when the result value is not used
    :drop_console => false, # Drop calls to console.* functions
    :angular => false, # Process @ngInject annotations
    :keep_fargs => false # Preserve unused function arguments
  }, # Apply transformations to code, set to false to skip
  :define => {}, # Define values for symbol replacement
  :enclose => false, # Enclose in output function wrapper, define replacements as key-value pairs
  :source_filename => nil, # The filename of the input file
  :source_root => nil, # The URL of the directory which contains :source_filename
  :output_filename => nil, # The filename or URL where the minified output can be found
  :input_source_map => nil, # The contents of the source map describing the input
  :screw_ie8 => false, # Don't bother to generate safe code for IE8
  :source_map_url => false, # Url for source mapping to be appended in minified source
  :source_url => false # Url for original source to be appended in minified source
}
VERSION =

Current version of Uglifier.

"2.7.2"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Uglifier

Initialize new context for Uglifier with given options

Parameters:

  • options (Hash) (defaults to: {})

    optional overrides to Uglifier::DEFAULTS



166
167
168
169
170
171
172
# File 'lib/uglifier.rb', line 166

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

Class Method Details

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

Minifies JavaScript code using implicit context.

Parameters:

  • source (IO, String)

    valid JS source code.

  • options (Hash) (defaults to: {})

    optional overrides to Uglifier::DEFAULTS

Returns:

  • (String)

    minified code.



150
151
152
# File 'lib/uglifier.rb', line 150

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

.compile_with_map(source, options = {}) ⇒ Array(String, String)

Minifies JavaScript code and generates a source map using implicit context.

Parameters:

  • source (IO, String)

    valid JS source code.

  • options (Hash) (defaults to: {})

    optional overrides to Uglifier::DEFAULTS

Returns:

  • (Array(String, String))

    minified code and source map.



159
160
161
# File 'lib/uglifier.rb', line 159

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

Instance Method Details

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

Minifies JavaScript code

Parameters:

  • source (IO, String)

    valid JS source code.

Returns:

  • (String)

    minified code.



178
179
180
# File 'lib/uglifier.rb', line 178

def compile(source)
  run_uglifyjs(source, false)
end

#compile_with_map(source) ⇒ Array(String, String)

Minifies JavaScript code and generates a source map

Parameters:

  • source (IO, String)

    valid JS source code.

Returns:

  • (Array(String, String))

    minified code and source map.



187
188
189
# File 'lib/uglifier.rb', line 187

def compile_with_map(source)
  run_uglifyjs(source, true)
end