/usr/lib/ruby/vendor_ruby/uglifier.rb is in ruby-uglifier 2.7.2+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | # encoding: UTF-8
require "execjs"
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(options) {
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 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.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
def self.find_asset(filename)
[
File.expand_path("../" + filename, __FILE__),
File.join("/usr/share/javascript/ruby-uglifier", filename)
].find { |f| File.exists?(f) }
end
# SourceMap path
SourceMapPath = "/usr/share/javascript/source-map/source-map.js"
# UglifyJS source path
SourcePath = "/usr/share/javascript/uglifyjs/uglify.js"
# ES5 shims source path
ES5FallbackPath = find_asset("es5.js")
# String.split shim source path
SplitFallbackPath = find_asset("split.js")
# Default options for compilation
DEFAULTS = {
# rubocop:disable LineLength
:output => {
:ascii_only => true, # Escape non-ASCII characterss
: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
:semicolons => true, # Separate statements with semicolons
:preserve_line => false, # Preserve line numbers in outputs
:beautify => false, # Beautify output
:indent_level => 4, # Indent level in spaces
:indent_start => 0, # Starting indent level
:space_colon => false, # Insert space before colons (only with beautifier)
:width => 80, # Specify line width when beautifier is used (only with beautifier)
:preamble => nil # Preamble for the generated JS file. Can be used to insert any code or comment.
},
:mangle => {
:eval => false, # Mangle names when eval of when is used in scope
:except => ["$super"], # Argument names to be excluded from mangling
:sort => false, # Assign shorter names to most frequently used variables. Often results in bigger output after gzip.
: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
:drop_debugger => true, # Remove debugger; statements
:unsafe => false, # Apply "unsafe" transformations
:conditionals => true, # Optimize for if-s and conditional expressions
:comparisons => true, # Apply binary node optimizations for comparisons
:evaluate => true, # Attempt to evaluate constant expressions
:booleans => true, # Various optimizations to boolean contexts
:loops => true, # Optimize loops when condition can be statically determined
:unused => true, # Drop unreferenced functions and variables
:hoist_funs => true, # Hoist function declarations
:hoist_vars => false, # Hoist var declarations
:if_return => true, # Optimizations for if/return and if/continue
:join_vars => true, # Join consecutive var statements
:cascade => true, # Cascade sequences
:negate_iife => true, # Negate immediately invoked function expressions to avoid extra parens
:pure_getters => false, # Assume that object property access does not have any side-effects
:pure_funcs => nil, # List of functions without side-effects. Can safely discard function calls when the result value is not used
:drop_console => false, # Drop calls to console.* functions
:angular => false, # Process @ngInject annotations
:keep_fargs => false # Preserve unused function arguments
}, # Apply transformations to code, set to false to skip
:define => {}, # Define values for symbol replacement
: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
: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.
#
# @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.
#
# @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
#
# @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(uglifyjs_source)
end
# Minifies JavaScript code
#
# @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
#
# @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
[
__read__(ES5FallbackPath),
__read__(SplitFallbackPath),
'window = this;',
__read__(SourceMapPath),
"MOZ_SourceMap = sourceMap;",
__read__(SourcePath),
].join("\n")
end
def __read__(file)
File.open(file, "r:UTF-8") { |f| f.read }
end
# Run UglifyJS for given source code
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
def read_source(source)
if source.respond_to?(:read)
source.read
else
source.to_s
end
end
def mangle_options
conditional_option(@options[:mangle], DEFAULTS[:mangle])
end
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
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
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
def output_options
DEFAULTS[:output].merge(@options[:output] || {}).merge(
:comments => comment_options,
:screw_ie8 => screw_ie8?
).reject { |key, _| key == :ie_proof }
end
def screw_ie8?
if (@options[:output] || {}).has_key?(:ie_proof)
false
else
@options[:screw_ie8] || DEFAULTS[:screw_ie8]
end
end
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
def parse_options
{ :filename => @options[:source_filename] }
end
def enclose_options
if @options[:enclose]
@options[:enclose].map do |pair|
pair.first + ':' + pair.last
end
else
false
end
end
def encode_regexp(regexp)
modifiers = if regexp.casefold?
"i"
else
""
end
[regexp.source, modifiers]
end
def conditional_option(value, defaults)
if value == true || value.nil?
defaults
elsif value
defaults.merge(value)
else
false
end
end
end
|