2016-05-01 04:59:29 +00:00
|
|
|
// workaround for tty output truncation upon process.exit()
|
|
|
|
|
[process.stdout, process.stderr].forEach(function(stream){
|
|
|
|
|
if (stream._handle && stream._handle.setBlocking)
|
|
|
|
|
stream._handle.setBlocking(true);
|
|
|
|
|
});
|
|
|
|
|
|
2012-10-20 08:12:21 +00:00
|
|
|
var path = require("path");
|
2012-08-21 09:55:56 +00:00
|
|
|
var fs = require("fs");
|
|
|
|
|
|
2012-10-08 09:55:18 +00:00
|
|
|
var FILES = exports.FILES = [
|
|
|
|
|
"../lib/utils.js",
|
|
|
|
|
"../lib/ast.js",
|
|
|
|
|
"../lib/parse.js",
|
|
|
|
|
"../lib/transform.js",
|
|
|
|
|
"../lib/scope.js",
|
|
|
|
|
"../lib/output.js",
|
|
|
|
|
"../lib/compress.js",
|
|
|
|
|
"../lib/sourcemap.js",
|
Add property name mangler
We only touch properties that are present in an object literal, or which are
assigned to. Example:
x = { foo: 1 };
x.bar = 2;
x["baz"] = 3;
x[cond ? "qwe" : "asd"] = 4;
console.log(x.stuff);
The names "foo", "bar", "baz", "qwe" and "asd" will be mangled, and the
resulting mangled names will be used for the same properties throughout the
code. The "stuff" will not be, since it's just referenced but never
assigned to.
This *will* break most of the code out there, but could work on carefully
written code: do not use eval, do not define methods or properties by
walking an array of names, etc. Also, a comprehensive list of exclusions
needs to be passed, to avoid mangling properties that are standard in
JavaScript, DOM, used in external libraries etc.
2015-03-14 09:22:28 +00:00
|
|
|
"../lib/mozilla-ast.js",
|
2015-09-24 14:57:47 +00:00
|
|
|
"../lib/propmangle.js",
|
|
|
|
|
"./exports.js",
|
2012-10-08 09:55:18 +00:00
|
|
|
].map(function(file){
|
2014-08-29 18:41:13 +00:00
|
|
|
return fs.realpathSync(path.join(path.dirname(__filename), file));
|
2012-10-08 09:55:18 +00:00
|
|
|
});
|
|
|
|
|
|
2015-09-24 14:57:47 +00:00
|
|
|
var UglifyJS = exports;
|
|
|
|
|
|
2016-06-13 19:11:08 +00:00
|
|
|
new Function("MOZ_SourceMap", "exports", "DEBUG", FILES.map(function(file){
|
2015-09-24 14:57:47 +00:00
|
|
|
return fs.readFileSync(file, "utf8");
|
|
|
|
|
}).join("\n\n"))(
|
|
|
|
|
require("source-map"),
|
2016-06-13 19:11:08 +00:00
|
|
|
UglifyJS,
|
|
|
|
|
!!global.UGLIFY_DEBUG
|
2015-09-24 14:57:47 +00:00
|
|
|
);
|
2012-08-21 09:55:56 +00:00
|
|
|
|
|
|
|
|
UglifyJS.AST_Node.warn_function = function(txt) {
|
2015-05-04 12:07:16 +00:00
|
|
|
console.error("WARN: %s", txt);
|
2012-08-21 09:55:56 +00:00
|
|
|
};
|
|
|
|
|
|
2012-10-08 18:15:47 +00:00
|
|
|
exports.minify = function(files, options) {
|
|
|
|
|
options = UglifyJS.defaults(options, {
|
2016-01-18 02:54:09 +00:00
|
|
|
spidermonkey : false,
|
|
|
|
|
outSourceMap : null,
|
2016-10-29 07:21:28 +00:00
|
|
|
outFileName : null,
|
2016-01-18 02:54:09 +00:00
|
|
|
sourceRoot : null,
|
|
|
|
|
inSourceMap : null,
|
2016-07-06 10:02:07 +00:00
|
|
|
sourceMapUrl : null,
|
2016-10-09 06:15:25 +00:00
|
|
|
sourceMapInline : false,
|
2016-01-18 02:54:09 +00:00
|
|
|
fromString : false,
|
|
|
|
|
warnings : false,
|
|
|
|
|
mangle : {},
|
|
|
|
|
mangleProperties : false,
|
|
|
|
|
nameCache : null,
|
|
|
|
|
output : null,
|
2016-02-06 19:46:18 +00:00
|
|
|
compress : {},
|
|
|
|
|
parse : {}
|
2012-10-08 18:15:47 +00:00
|
|
|
});
|
2013-06-28 07:08:13 +00:00
|
|
|
UglifyJS.base54.reset();
|
|
|
|
|
|
2012-10-08 18:15:47 +00:00
|
|
|
// 1. parse
|
2014-01-09 14:12:00 +00:00
|
|
|
var toplevel = null,
|
|
|
|
|
sourcesContent = {};
|
2014-01-07 16:42:48 +00:00
|
|
|
|
|
|
|
|
if (options.spidermonkey) {
|
|
|
|
|
toplevel = UglifyJS.AST_Node.from_mozilla_ast(files);
|
|
|
|
|
} else {
|
2016-06-27 11:01:21 +00:00
|
|
|
function addFile(file, fileUrl) {
|
2014-01-07 16:42:48 +00:00
|
|
|
var code = options.fromString
|
|
|
|
|
? file
|
|
|
|
|
: fs.readFileSync(file, "utf8");
|
2016-06-27 11:01:21 +00:00
|
|
|
sourcesContent[fileUrl] = code;
|
2014-01-07 16:42:48 +00:00
|
|
|
toplevel = UglifyJS.parse(code, {
|
2016-06-27 11:01:21 +00:00
|
|
|
filename: fileUrl,
|
2016-02-06 19:46:18 +00:00
|
|
|
toplevel: toplevel,
|
|
|
|
|
bare_returns: options.parse ? options.parse.bare_returns : undefined
|
2014-01-07 16:42:48 +00:00
|
|
|
});
|
2016-06-27 11:01:21 +00:00
|
|
|
}
|
2016-07-30 01:27:30 +00:00
|
|
|
if (!options.fromString) files = UglifyJS.simple_glob(files);
|
2016-06-27 11:01:21 +00:00
|
|
|
[].concat(files).forEach(function (files, i) {
|
|
|
|
|
if (typeof files === 'string') {
|
|
|
|
|
addFile(files, options.fromString ? i : files);
|
|
|
|
|
} else {
|
|
|
|
|
for (var fileUrl in files) {
|
|
|
|
|
addFile(files[fileUrl], fileUrl);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-10-08 18:15:47 +00:00
|
|
|
});
|
2014-01-07 16:42:48 +00:00
|
|
|
}
|
2015-07-28 12:36:22 +00:00
|
|
|
if (options.wrap) {
|
|
|
|
|
toplevel = toplevel.wrap_commonjs(options.wrap, options.exportAll);
|
|
|
|
|
}
|
2012-10-08 18:15:47 +00:00
|
|
|
|
|
|
|
|
// 2. compress
|
2013-01-04 09:24:29 +00:00
|
|
|
if (options.compress) {
|
|
|
|
|
var compress = { warnings: options.warnings };
|
|
|
|
|
UglifyJS.merge(compress, options.compress);
|
|
|
|
|
toplevel.figure_out_scope();
|
|
|
|
|
var sq = UglifyJS.Compressor(compress);
|
2016-04-12 13:19:38 +00:00
|
|
|
toplevel = sq.compress(toplevel);
|
2013-01-04 09:24:29 +00:00
|
|
|
}
|
2012-10-08 18:15:47 +00:00
|
|
|
|
2016-01-18 02:54:09 +00:00
|
|
|
// 3. mangle properties
|
|
|
|
|
if (options.mangleProperties || options.nameCache) {
|
|
|
|
|
options.mangleProperties.cache = UglifyJS.readNameCache(options.nameCache, "props");
|
|
|
|
|
toplevel = UglifyJS.mangle_properties(toplevel, options.mangleProperties);
|
|
|
|
|
UglifyJS.writeNameCache(options.nameCache, "props", options.mangleProperties.cache);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4. mangle
|
2013-01-04 09:24:29 +00:00
|
|
|
if (options.mangle) {
|
2015-01-05 18:10:32 +00:00
|
|
|
toplevel.figure_out_scope(options.mangle);
|
|
|
|
|
toplevel.compute_char_frequency(options.mangle);
|
2013-01-04 09:24:29 +00:00
|
|
|
toplevel.mangle_names(options.mangle);
|
|
|
|
|
}
|
2012-10-08 18:15:47 +00:00
|
|
|
|
2016-01-18 02:54:09 +00:00
|
|
|
// 5. output
|
2013-02-10 18:06:13 +00:00
|
|
|
var inMap = options.inSourceMap;
|
|
|
|
|
var output = {};
|
|
|
|
|
if (typeof options.inSourceMap == "string") {
|
2016-07-26 07:54:02 +00:00
|
|
|
inMap = JSON.parse(fs.readFileSync(options.inSourceMap, "utf8"));
|
2012-10-09 09:52:28 +00:00
|
|
|
}
|
2016-10-09 06:15:25 +00:00
|
|
|
if (options.outSourceMap || options.sourceMapInline) {
|
2013-02-10 18:06:13 +00:00
|
|
|
output.source_map = UglifyJS.SourceMap({
|
2016-10-29 07:21:28 +00:00
|
|
|
// prefer outFileName, otherwise use outSourceMap without .map suffix
|
|
|
|
|
file: options.outFileName || (typeof options.outSourceMap === 'string' ? options.outSourceMap.replace(/\.map$/i, '') : null),
|
2013-02-10 18:06:13 +00:00
|
|
|
orig: inMap,
|
|
|
|
|
root: options.sourceRoot
|
|
|
|
|
});
|
2014-01-09 14:12:00 +00:00
|
|
|
if (options.sourceMapIncludeSources) {
|
|
|
|
|
for (var file in sourcesContent) {
|
2014-01-09 14:20:05 +00:00
|
|
|
if (sourcesContent.hasOwnProperty(file)) {
|
2014-04-13 09:16:10 +00:00
|
|
|
output.source_map.get().setSourceContent(file, sourcesContent[file]);
|
2014-01-09 14:20:05 +00:00
|
|
|
}
|
2014-01-09 14:12:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-10 18:06:13 +00:00
|
|
|
}
|
2013-01-04 09:24:29 +00:00
|
|
|
if (options.output) {
|
|
|
|
|
UglifyJS.merge(output, options.output);
|
|
|
|
|
}
|
|
|
|
|
var stream = UglifyJS.OutputStream(output);
|
2012-10-08 18:15:47 +00:00
|
|
|
toplevel.print(stream);
|
2014-04-11 15:09:56 +00:00
|
|
|
|
|
|
|
|
|
2015-01-26 11:07:44 +00:00
|
|
|
var source_map = output.source_map;
|
|
|
|
|
if (source_map) {
|
|
|
|
|
source_map = source_map + "";
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-09 06:15:25 +00:00
|
|
|
var mappingUrlPrefix = "\n//# sourceMappingURL=";
|
|
|
|
|
if (options.sourceMapInline) {
|
|
|
|
|
stream += mappingUrlPrefix + "data:application/json;charset=utf-8;base64," + new Buffer(source_map).toString("base64");
|
|
|
|
|
} else if (options.outSourceMap && typeof options.outSourceMap === "string" && options.sourceMapUrl !== false) {
|
|
|
|
|
stream += mappingUrlPrefix + (typeof options.sourceMapUrl === "string" ? options.sourceMapUrl : options.outSourceMap);
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-08 18:15:47 +00:00
|
|
|
return {
|
|
|
|
|
code : stream + "",
|
2015-01-26 11:07:44 +00:00
|
|
|
map : source_map
|
2012-10-08 18:15:47 +00:00
|
|
|
};
|
|
|
|
|
};
|
2012-10-09 10:21:21 +00:00
|
|
|
|
|
|
|
|
// exports.describe_ast = function() {
|
|
|
|
|
// function doitem(ctor) {
|
|
|
|
|
// var sub = {};
|
|
|
|
|
// ctor.SUBCLASSES.forEach(function(ctor){
|
|
|
|
|
// sub[ctor.TYPE] = doitem(ctor);
|
|
|
|
|
// });
|
|
|
|
|
// var ret = {};
|
|
|
|
|
// if (ctor.SELF_PROPS.length > 0) ret.props = ctor.SELF_PROPS;
|
|
|
|
|
// if (ctor.SUBCLASSES.length > 0) ret.sub = sub;
|
|
|
|
|
// return ret;
|
|
|
|
|
// }
|
|
|
|
|
// return doitem(UglifyJS.AST_Node).sub;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
exports.describe_ast = function() {
|
|
|
|
|
var out = UglifyJS.OutputStream({ beautify: true });
|
|
|
|
|
function doitem(ctor) {
|
|
|
|
|
out.print("AST_" + ctor.TYPE);
|
|
|
|
|
var props = ctor.SELF_PROPS.filter(function(prop){
|
|
|
|
|
return !/^\$/.test(prop);
|
|
|
|
|
});
|
|
|
|
|
if (props.length > 0) {
|
|
|
|
|
out.space();
|
|
|
|
|
out.with_parens(function(){
|
|
|
|
|
props.forEach(function(prop, i){
|
|
|
|
|
if (i) out.space();
|
|
|
|
|
out.print(prop);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (ctor.documentation) {
|
|
|
|
|
out.space();
|
|
|
|
|
out.print_string(ctor.documentation);
|
|
|
|
|
}
|
|
|
|
|
if (ctor.SUBCLASSES.length > 0) {
|
|
|
|
|
out.space();
|
|
|
|
|
out.with_block(function(){
|
|
|
|
|
ctor.SUBCLASSES.forEach(function(ctor, i){
|
|
|
|
|
out.indent();
|
|
|
|
|
doitem(ctor);
|
|
|
|
|
out.newline();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
doitem(UglifyJS.AST_Node);
|
|
|
|
|
return out + "";
|
|
|
|
|
};
|
2015-03-17 08:05:49 +00:00
|
|
|
|
2015-03-22 09:04:28 +00:00
|
|
|
function readReservedFile(filename, reserved) {
|
2015-03-18 09:53:17 +00:00
|
|
|
if (!reserved) {
|
|
|
|
|
reserved = { vars: [], props: [] };
|
|
|
|
|
}
|
|
|
|
|
var data = fs.readFileSync(filename, "utf8");
|
|
|
|
|
data = JSON.parse(data);
|
|
|
|
|
if (data.vars) {
|
|
|
|
|
data.vars.forEach(function(name){
|
|
|
|
|
UglifyJS.push_uniq(reserved.vars, name);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (data.props) {
|
|
|
|
|
data.props.forEach(function(name){
|
|
|
|
|
UglifyJS.push_uniq(reserved.props, name);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return reserved;
|
2015-03-22 09:04:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exports.readReservedFile = readReservedFile;
|
|
|
|
|
|
|
|
|
|
exports.readDefaultReservedFile = function(reserved) {
|
|
|
|
|
return readReservedFile(path.join(__dirname, "domprops.json"), reserved);
|
2015-03-18 09:53:17 +00:00
|
|
|
};
|
|
|
|
|
|
2015-03-17 08:05:49 +00:00
|
|
|
exports.readNameCache = function(filename, key) {
|
|
|
|
|
var cache = null;
|
|
|
|
|
if (filename) {
|
|
|
|
|
try {
|
|
|
|
|
var cache = fs.readFileSync(filename, "utf8");
|
|
|
|
|
cache = JSON.parse(cache)[key];
|
|
|
|
|
if (!cache) throw "init";
|
|
|
|
|
cache.props = UglifyJS.Dictionary.fromObject(cache.props);
|
|
|
|
|
} catch(ex) {
|
|
|
|
|
cache = {
|
|
|
|
|
cname: -1,
|
|
|
|
|
props: new UglifyJS.Dictionary()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return cache;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.writeNameCache = function(filename, key, cache) {
|
|
|
|
|
if (filename) {
|
|
|
|
|
var data;
|
|
|
|
|
try {
|
|
|
|
|
data = fs.readFileSync(filename, "utf8");
|
|
|
|
|
data = JSON.parse(data);
|
|
|
|
|
} catch(ex) {
|
|
|
|
|
data = {};
|
|
|
|
|
}
|
|
|
|
|
data[key] = {
|
|
|
|
|
cname: cache.cname,
|
|
|
|
|
props: cache.props.toObject()
|
|
|
|
|
};
|
|
|
|
|
fs.writeFileSync(filename, JSON.stringify(data, null, 2), "utf8");
|
|
|
|
|
}
|
|
|
|
|
};
|
2016-07-29 20:18:56 +00:00
|
|
|
|
|
|
|
|
// A file glob function that only supports "*" and "?" wildcards in the basename.
|
|
|
|
|
// Example: "foo/bar/*baz??.*.js"
|
|
|
|
|
// Argument `glob` may be a string or an array of strings.
|
|
|
|
|
// Returns an array of strings. Garbage in, garbage out.
|
|
|
|
|
exports.simple_glob = function simple_glob(glob) {
|
|
|
|
|
var results = [];
|
|
|
|
|
if (Array.isArray(glob)) {
|
|
|
|
|
glob.forEach(function(elem) {
|
|
|
|
|
results = results.concat(simple_glob(elem));
|
|
|
|
|
});
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
if (glob.match(/\*|\?/)) {
|
|
|
|
|
var dir = path.dirname(glob);
|
|
|
|
|
try {
|
|
|
|
|
var entries = fs.readdirSync(dir);
|
|
|
|
|
} catch (ex) {}
|
|
|
|
|
if (entries) {
|
|
|
|
|
var pattern = "^" + (path.basename(glob)
|
|
|
|
|
.replace(/\(/g, "\\(")
|
|
|
|
|
.replace(/\)/g, "\\)")
|
|
|
|
|
.replace(/\{/g, "\\{")
|
|
|
|
|
.replace(/\}/g, "\\}")
|
|
|
|
|
.replace(/\[/g, "\\[")
|
|
|
|
|
.replace(/\]/g, "\\]")
|
|
|
|
|
.replace(/\+/g, "\\+")
|
|
|
|
|
.replace(/\^/g, "\\^")
|
|
|
|
|
.replace(/\$/g, "\\$")
|
|
|
|
|
.replace(/\*/g, "[^/\\\\]*")
|
|
|
|
|
.replace(/\./g, "\\.")
|
|
|
|
|
.replace(/\?/g, ".")) + "$";
|
|
|
|
|
var mod = process.platform === "win32" ? "i" : "";
|
|
|
|
|
var rx = new RegExp(pattern, mod);
|
|
|
|
|
for (var i in entries) {
|
|
|
|
|
if (rx.test(entries[i]))
|
|
|
|
|
results.push(dir + "/" + entries[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (results.length === 0)
|
|
|
|
|
results = [ glob ];
|
|
|
|
|
return results;
|
|
|
|
|
};
|