class Uglifier
A wrapper around the UglifyJS interface
Constants
Public Class Methods
Minifies JavaScript code using implicit context.
@param source [IO, String] valid JS source
code. @param options [Hash] optional overrides to
Uglifier::DEFAULTS
@return [String] minified code.
# File lib/uglifier.rb, line 149 def self.compile(source, options = {}) new(options).compile(source) end
Minifies JavaScript code and generates a source map using implicit context.
@param source [IO, String] valid JS source
code. @param options [Hash] optional overrides to
Uglifier::DEFAULTS
@return [Array(String, String)] minified
code and source map.
# File lib/uglifier.rb, line 158 def self.compile_with_map(source, options = {}) new(options).compile_with_map(source) end
Initialize new context for Uglifier with given options
@param options [Hash] optional overrides to Uglifier::DEFAULTS
# File lib/uglifier.rb, line 165 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
Public Instance Methods
Minifies JavaScript code
@param source [IO, String] valid JS source code. @return [String] minified code.
# File lib/uglifier.rb, line 177 def compile(source) run_uglifyjs(source, false) end
Minifies JavaScript code and generates a source map
@param source [IO, String] valid JS source code. @return [Array(String, String)] minified code and source map.
# File lib/uglifier.rb, line 186 def compile_with_map(source) run_uglifyjs(source, true) end
Private Instance Methods
# File lib/uglifier.rb, line 235 def comment_options case comment_setting when :all, true true when :jsdoc "jsdoc" when :copyright encode_regexp(/(^!)|Copyright/i) when Regexp encode_regexp(comment_setting) else false end end
# File lib/uglifier.rb, line 250 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 226 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 311 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 291 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 301 def encode_regexp(regexp) modifiers = if regexp.casefold? "i" else "" end [regexp.source, modifiers] end
# File lib/uglifier.rb, line 222 def mangle_options conditional_option(@options[:mangle], DEFAULTS[:mangle]) end
# File lib/uglifier.rb, line 262 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 287 def parse_options { :filename => @options[:source_filename] } end
# File lib/uglifier.rb, line 214 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 199 def run_uglifyjs(source, generate_map) options = { :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 } @context.call(Uglifier::JS, options) end
# File lib/uglifier.rb, line 269 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 277 def source_map_options { :file => @options[:output_filename], :root => @options[:source_root], :orig => @options[:input_source_map], :map_url => @options[:source_map_url], :url => @options[:source_url] } end
# File lib/uglifier.rb, line 192 def uglifyjs_source [ES5FallbackPath, SplitFallbackPath, SourcePath].map do |file| File.open(file, "r:UTF-8") { |f| f.read } end.join("\n") end