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;
|
|
|
|
|
|
|
|
|
|
new Function("MOZ_SourceMap", "exports", FILES.map(function(file){
|
|
|
|
|
return fs.readFileSync(file, "utf8");
|
|
|
|
|
}).join("\n\n"))(
|
|
|
|
|
require("source-map"),
|
|
|
|
|
UglifyJS
|
|
|
|
|
);
|
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,
|
|
|
|
|
sourceRoot : null,
|
|
|
|
|
inSourceMap : null,
|
|
|
|
|
fromString : false,
|
|
|
|
|
warnings : false,
|
|
|
|
|
mangle : {},
|
|
|
|
|
mangleProperties : false,
|
|
|
|
|
nameCache : null,
|
|
|
|
|
output : null,
|
|
|
|
|
compress : {}
|
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 {
|
|
|
|
|
if (typeof files == "string")
|
|
|
|
|
files = [ files ];
|
2015-06-15 16:03:06 +00:00
|
|
|
files.forEach(function(file, i){
|
2014-01-07 16:42:48 +00:00
|
|
|
var code = options.fromString
|
|
|
|
|
? file
|
|
|
|
|
: fs.readFileSync(file, "utf8");
|
2014-01-09 14:12:00 +00:00
|
|
|
sourcesContent[file] = code;
|
2014-01-07 16:42:48 +00:00
|
|
|
toplevel = UglifyJS.parse(code, {
|
2015-06-15 16:03:06 +00:00
|
|
|
filename: options.fromString ? i : file,
|
2014-01-07 16:42:48 +00:00
|
|
|
toplevel: toplevel
|
|
|
|
|
});
|
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);
|
|
|
|
|
toplevel = toplevel.transform(sq);
|
|
|
|
|
}
|
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") {
|
2012-10-09 09:52:28 +00:00
|
|
|
inMap = fs.readFileSync(options.inSourceMap, "utf8");
|
|
|
|
|
}
|
2013-02-10 18:06:13 +00:00
|
|
|
if (options.outSourceMap) {
|
|
|
|
|
output.source_map = UglifyJS.SourceMap({
|
|
|
|
|
file: options.outSourceMap,
|
|
|
|
|
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-08-27 17:38:33 +00:00
|
|
|
if (options.outSourceMap && "string" === typeof options.outSourceMap) {
|
2014-04-11 15:09:56 +00:00
|
|
|
stream += "\n//# sourceMappingURL=" + options.outSourceMap;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-26 11:07:44 +00:00
|
|
|
var source_map = output.source_map;
|
|
|
|
|
if (source_map) {
|
|
|
|
|
source_map = source_map + "";
|
|
|
|
|
}
|
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
};
|