lib/uglifier.rb in uglifier-2.5.3 vs lib/uglifier.rb in uglifier-2.6.0
- old
+ new
@@ -4,11 +4,13 @@
require "json"
require "uglifier/version"
# A wrapper around the UglifyJS interface
class Uglifier
+ # Error class for compilation errors.
Error = ExecJS::Error
+ # JavaScript code to call UglifyJS
JS = <<-JS
function comments(option) {
if (Object.prototype.toString.call(option) === '[object Array]') {
return new RegExp(option[0], option[1]);
} else if (option == "jsdoc") {
@@ -53,18 +55,27 @@
}
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
- # UglifyJS source patch
+ # UglifyJS source path
SourcePath = File.expand_path("../uglify.js", __FILE__)
# ES5 shims source path
ES5FallbackPath = File.expand_path("../es5.js", __FILE__)
# String.split shim source path
SplitFallbackPath = File.expand_path("../split.js", __FILE__)
@@ -122,68 +133,70 @@
: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
+ :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
}
# rubocop:enable LineLength
# 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
+ # @param source [IO, String] valid JS source code.
+ # @param options [Hash] optional overrides to +Uglifier::DEFAULTS+
+ # @return [String] minified code.
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]
+ # @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.
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
+ # @param options [Hash] optional overrides to +Uglifier::DEFAULTS+
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)
+ @context = ExecJS.compile(uglifyjs_source)
end
# Minifies JavaScript code
#
- # source should be a String or IO object containing valid JavaScript.
- #
- # Returns minified code as String
+ # @param source [IO, String] valid JS source code.
+ # @return [String] minified code.
def compile(source)
run_uglifyjs(source, false)
end
alias_method :compress, :compile
# 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]
+ # @param source [IO, String] valid JS source code.
+ # @return [Array(String, String)] minified code and source map.
def compile_with_map(source)
run_uglifyjs(source, true)
end
private
+ def uglifyjs_source
+ [ES5FallbackPath, SplitFallbackPath, SourcePath].map do |file|
+ File.open(file, "r:UTF-8") { |f| f.read }
+ end.join("\n")
+ end
+
# Run UglifyJS for given source code
def run_uglifyjs(source, generate_map)
@context.exec(Uglifier::JS % json_encode(
:source => read_source(source),
:output => output_options,
@@ -261,10 +274,12 @@
def source_map_options
{
:file => @options[:output_filename],
:root => @options[:source_root],
- :orig => @options[:input_source_map]
+ :orig => @options[:input_source_map],
+ :map_url => @options[:source_map_url],
+ :url => @options[:source_url]
}
end
def parse_options
{ :filename => @options[:source_filename] }