lib/uglifier.rb in uglifier-4.2.0 vs lib/uglifier.rb in uglifier-4.2.1
- old
+ new
@@ -1,9 +1,8 @@
# encoding: UTF-8
require "json"
-require "base64"
require "execjs"
require "uglifier/version"
# A wrapper around the UglifyJS interface
class Uglifier
@@ -23,13 +22,13 @@
# UglifyJS wrapper path
UglifyJSWrapperPath = File.expand_path("../uglifier.js", __FILE__)
# Default options for compilation
DEFAULTS = {
- # rubocop:disable LineLength
+ # rubocop:disable Layout/LineLength
:output => {
- :ascii_only => true, # Escape non-ASCII characterss
+ :ascii_only => true, # Escape non-ASCII characters
: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
@@ -47,11 +46,11 @@
},
:mangle => {
:eval => false, # Mangle names when eval of when is used in scope
:reserved => ["$super"], # Argument names to be excluded from mangling
:properties => false, # Mangle property names
- :toplevel => false, # Mangle names declared in the toplevel scope
+ :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
@@ -82,11 +81,11 @@
:keep_fargs => false, # Preserve unused function arguments
:keep_fnames => false, # Do not drop names in function definitions
:passes => 1, # Number of times to run compress. Raising the number of passes will increase compress time, but can produce slightly smaller code.
:keep_infinity => false, # Prevent compression of Infinity to 1/0
:side_effects => true, # Pass false to disable potentially dropping functions marked as "pure" using pure comment annotation. See UglifyJS documentation for details.
- :switches => true, # de-duplicate and remove unreachable switch branches
+ :switches => true # de-duplicate and remove unreachable switch branches
}, # Apply transformations to code, set to false to skip
:parse => {
:bare_returns => false, # Allow top-level return statements.
:expression => false, # Parse a single expression, rather than a program (for parsing JSON).
:html5_comments => true, # Ignore HTML5 comments in input
@@ -99,19 +98,20 @@
:ie8 => true, # Generate safe code for IE8
:source_map => false, # Generate source map
:error_context_lines => 8, # How many lines surrounding the error line
:harmony => false # Enable ES6/Harmony mode (experimental). Disabling mangling and compressing is recommended with Harmony mode.
}
+ # rubocop:enable Layout/LineLength
EXTRA_OPTIONS = [:comments, :mangle_properties]
MANGLE_PROPERTIES_DEFAULTS = {
:debug => false, # Add debug prefix and suffix to mangled properties
:regex => nil, # A regular expression to filter property names to be mangled
:keep_quoted => false, # Keep quoted property names
:reserved => [], # List of properties that should not be mangled
- :builtins => false, # Mangle properties that overlap with standard JS globals
+ :builtins => false # Mangle properties that overlap with standard JS globals
}
SOURCE_MAP_DEFAULTS = {
:map_url => false, # Url for source mapping to be appended in minified source
:url => false, # Url for original source to be appended in minified source
@@ -120,12 +120,10 @@
:root => nil, # The URL of the directory which contains :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
}
- # rubocop:enable LineLength
-
# 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.
@@ -144,24 +142,26 @@
# Initialize new context for Uglifier with given options
#
# @param options [Hash] optional overrides to +Uglifier::DEFAULTS+
def initialize(options = {})
+ # rubocop:disable Lint/UnreachableLoop
(options.keys - DEFAULTS.keys - EXTRA_OPTIONS)[0..1].each do |missing|
raise ArgumentError, "Invalid option: #{missing}"
end
+ # rubocop:enable Lint/UnreachableLoop
@options = options
end
# Minifies JavaScript code
#
# @param source [IO, String] valid JS source code.
# @return [String] minified code.
def compile(source)
if @options[:source_map]
compiled, source_map = run_uglifyjs(source, true)
- source_map_uri = Base64.strict_encode64(source_map)
+ source_map_uri = [source_map].pack('m0')
source_map_mime = "application/json;charset=utf-8;base64"
compiled + "\n//# sourceMappingURL=data:#{source_map_mime},#{source_map_uri}"
else
run_uglifyjs(source, false)
end
@@ -505,13 +505,14 @@
match && match[1]
end
def input_source_map(source, generate_map)
return nil unless generate_map
+
source_map_options = @options[:source_map].is_a?(Hash) ? @options[:source_map] : {}
sanitize_map_root(source_map_options.fetch(:input_source_map) do
url = extract_source_mapping_url(source)
- Base64.strict_decode64(url.split(",", 2)[-1]) if url && url.start_with?("data:")
+ url.split(",", 2)[-1].unpack1('m0') if url && url.start_with?("data:")
end)
rescue ArgumentError, JSON::ParserError
nil
end
end