A wrapper around the UglifyJS interface
Default options for compilation
ES5 shims source path
UglifyJS source patch
String.split shim source path
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 136 def self.compile(source, options = {}) 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 146 def self.compile_with_map(source, options = {}) 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 153 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(File.open(ES5FallbackPath, "r:UTF-8").read + File.open(SplitFallbackPath, "r:UTF-8").read + File.open(SourcePath, "r:UTF-8").read) end
Minifies JavaScript code
source should be a String or IO object containing valid JavaScript.
Returns minified code as String
# File lib/uglifier.rb, line 168 def compile(source) run_uglifyjs(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 178 def compile_with_map(source) run_uglifyjs(source, true) end
# File lib/uglifier.rb, line 219 def comment_options case comment_setting when :all, true true when :jsdoc "jsdoc" when :copyright encode_regexp(/Copyright/) when Regexp encode_regexp(comment_setting) else false end end
# File lib/uglifier.rb, line 234 def comment_setting 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 end
# File lib/uglifier.rb, line 210 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 297 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 273 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 287 def encode_regexp(regexp) modifiers = if regexp.casefold? "i" else "" end [regexp.source, modifiers] end
# File lib/uglifier.rb, line 283 def json_encode(obj) JSON.dump(obj) end
# File lib/uglifier.rb, line 206 def mangle_options conditional_option(@options[:mangle], DEFAULTS[:mangle]) end
# File lib/uglifier.rb, line 246 def output_options DEFAULTS[:output].merge(@options[:output] || {}).merge( :comments => comment_options, :screw_ie8 => screw_ie8? ).reject { |key, _| key == :ie_proof } end
# File lib/uglifier.rb, line 269 def parse_options { :filename => @options[:source_filename] } end
# File lib/uglifier.rb, line 198 def read_source(source) if source.respond_to?(:read) source.read else source.to_s end end
Run UglifyJS for given source code
# File lib/uglifier.rb, line 185 def run_uglifyjs(source, generate_map) @context.exec(Uglifier::JS % json_encode( :source => read_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 253 def screw_ie8? if (@options[:output] || {}).has_key?(:ie_proof) false else @options[:screw_ie8] || DEFAULTS[:screw_ie8] end end
# File lib/uglifier.rb, line 261 def source_map_options { :file => @options[:output_filename], :root => @options[:source_root], :orig => @options[:input_source_map] } end