class Uglifier
Constants
- DEFAULTS
Default options for compilation
- ES5FallbackPath
- Error
- SourcePath
- SplitFallbackPath
- VERSION
Public Class Methods
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 74 def self.compile(source, options = {}) self.new(options).compile(source) end
Minifies JavaScript code and generates a source map using implicit context.
source should be a String or IO object containing valid JavaScript. options contain optional overrides to Uglifier::DEFAULTS
Returns a pair of [minified code as String, source map as a String]
# File lib/uglifier.rb, line 84 def self.compile_with_map(source, options = {}) self.new(options).compile_with_map(source) end
Initialize new context for Uglifier with given options
options - Hash of options to override Uglifier::DEFAULTS
# File lib/uglifier.rb, line 91 def initialize(options = {}) (options.keys - DEFAULTS.keys - [:comments, :squeeze, :copyright])[0..1].each do |missing| raise ArgumentError.new("Invalid option: #{missing}") end @options = options @context = ExecJS.compile(File.open(ES5FallbackPath, "r:UTF-8").read + File.open(SplitFallbackPath, "r:UTF-8").read + File.open(SourcePath, "r:UTF-8").read) end
Public Instance Methods
Minifies JavaScript code
source should be a String or IO object containing valid JavaScript.
Returns minified code as String
# File lib/uglifier.rb, line 106 def compile(source) really_compile(source, false) end
Minifies JavaScript code and generates a source map
source should be a String or IO object containing valid JavaScript.
Returns a pair of [minified code as String, source map as a String]
# File lib/uglifier.rb, line 116 def compile_with_map(source) really_compile(source, true) end
Private Instance Methods
# File lib/uglifier.rb, line 207 def comment_options val = if @options.has_key?(:output) && @options[:output].has_key?(:comments) @options[:output][:comments] elsif @options.has_key?(:comments) @options[:comments] elsif @options[:copyright] == false :none else DEFAULTS[:output][:comments] end case val when :all, true true when :jsdoc "jsdoc" when :copyright encode_regexp(/Copyright/i) when Regexp encode_regexp(val) else false end end
# File lib/uglifier.rb, line 199 def compressor_options defaults = conditional_option(DEFAULTS[:compress], :global_defs => @options[:define] || {}, :screw_ie8 => @options[:screw_ie8] || DEFAULTS[:screw_ie8] ) conditional_option(@options[:compress] || @options[:squeeze], defaults) end
# File lib/uglifier.rb, line 281 def conditional_option(value, defaults) if value == true || value == nil defaults elsif value defaults.merge(value) else false end end
# File lib/uglifier.rb, line 257 def enclose_options if @options[:enclose] @options[:enclose].map do |pair| pair.first + ':' + pair.last end else false end end
# File lib/uglifier.rb, line 271 def encode_regexp(regexp) modifiers = if regexp.casefold? "i" else "" end [regexp.source, modifiers] end
# File lib/uglifier.rb, line 267 def json_encode(obj) JSON.dump(obj) end
# File lib/uglifier.rb, line 195 def mangle_options conditional_option(@options[:mangle], DEFAULTS[:mangle]) end
# File lib/uglifier.rb, line 232 def output_options screw_ie8 = if (@options[:output] || {}).has_key?(:ie_proof) false else @options[:screw_ie8] || DEFAULTS[:screw_ie8] end DEFAULTS[:output].merge(@options[:output] || {}).merge( :comments => comment_options, :screw_ie8 => screw_ie8 ).reject { |key,value| key == :ie_proof} end
# File lib/uglifier.rb, line 253 def parse_options {:filename => @options[:source_filename]} end
Minifies JavaScript code
source should be a String or IO object containing valid JavaScript.
# File lib/uglifier.rb, line 125 def really_compile(source, generate_map) source = source.respond_to?(:read) ? source.read : source.to_s js = <<-JS 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 options = %s; 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.generate_map) { return [stream.toString(), source_map.toString()]; } else { return stream.toString(); } JS @context.exec(js % json_encode( :source => source, :output => output_options, :compress => compressor_options, :mangle => mangle_options, :parse_options => parse_options, :source_map_options => source_map_options, :generate_map => (!!generate_map), :enclose => enclose_options )) end
# File lib/uglifier.rb, line 245 def source_map_options { :file => @options[:output_filename], :root => @options[:source_root], :orig => @options[:input_source_map] } end