UglifyJS/tools/node.js

196 lines
5.4 KiB
JavaScript
Raw Normal View History

var path = require("path");
var fs = require("fs");
var vm = require("vm");
var sys = require("util");
2014-05-19 17:29:01 +00:00
var MOZ_SourceMap = require("source-map");
function load_global(file) {
file = path.resolve(path.dirname(module.filename), file);
try {
var code = fs.readFileSync(file, "utf8");
2014-05-19 17:29:01 +00:00
return eval(code);
} catch(ex) {
// XXX: in case of a syntax error, the message is kinda
// useless. (no location information).
sys.debug("ERROR in file: " + file + " / " + ex);
process.exit(1);
}
};
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",
2014-05-19 17:29:01 +00:00
"../lib/mozilla-ast.js",
"../lib/translate.js",
"../lib/std.js"
].map(function(file){
return path.join(path.dirname(fs.realpathSync(__filename)), file);
});
FILES.forEach(load_global);
2014-05-19 17:29:01 +00:00
Cola.AST_Node.warn_function = function(txt) {
sys.error("WARN: " + txt);
};
2014-05-21 18:01:32 +00:00
Cola.getSource = function(file) {
return fs.readFileSync(path.join(process.cwd(), file), "utf8");
};
2014-05-19 17:29:01 +00:00
// XXX: perhaps we shouldn't export everything but heck, I'm lazy.
for (var i in Cola) {
if (Cola.hasOwnProperty(i)) {
exports[i] = Cola[i];
}
}
exports.minify = function(files, options) {
2014-05-19 17:29:01 +00:00
options = Cola.defaults(options, {
spidermonkey : false,
outSourceMap : null,
2012-10-19 09:35:19 +00:00
sourceRoot : null,
inSourceMap : null,
fromString : false,
warnings : false,
mangle : {},
output : null,
2014-05-19 17:29:01 +00:00
compress : {},
is_js : false,
main_binding : true
});
2014-05-19 17:29:01 +00:00
Cola.base54.reset();
// 1. parse
var toplevel = null,
sourcesContent = {};
if (options.spidermonkey) {
2014-05-19 17:29:01 +00:00
toplevel = Cola.AST_Node.from_mozilla_ast(files);
} else {
if (typeof files == "string")
files = [ files ];
files.forEach(function(file){
var code = options.fromString
? file
: fs.readFileSync(file, "utf8");
sourcesContent[file] = code;
2014-05-19 17:29:01 +00:00
toplevel = Cola.parse(code, {
filename: options.fromString ? "?" : file,
2014-05-19 17:29:01 +00:00
toplevel: toplevel,
is_js : options.is_js
});
2014-05-19 17:29:01 +00:00
2014-05-21 18:01:32 +00:00
if (!options.is_js) toplevel = toplevel.toJavaScript({
main_binding: options.main_binding,
parser: {
filename: options.fromString ? "?" : file,
is_js : options.is_js
}
});
});
}
// 2. compress
if (options.compress) {
2014-05-19 17:29:01 +00:00
var compress = { warnings: options.warnings, is_js : options.is_js };
Cola.merge(compress, options.compress);
toplevel.figure_out_scope();
2014-05-19 17:29:01 +00:00
var sq = new Cola.Compressor(compress);
toplevel = toplevel.transform(sq);
}
// 3. mangle
if (options.mangle) {
toplevel.figure_out_scope();
toplevel.compute_char_frequency();
toplevel.mangle_names(options.mangle);
}
// 4. output
var inMap = options.inSourceMap;
var output = {};
if (typeof options.inSourceMap == "string") {
inMap = fs.readFileSync(options.inSourceMap, "utf8");
}
if (options.outSourceMap) {
2014-05-19 17:29:01 +00:00
output.source_map = new Cola.SourceMap({
file: options.outSourceMap,
orig: inMap,
root: options.sourceRoot
});
if (options.sourceMapIncludeSources) {
for (var file in sourcesContent) {
if (sourcesContent.hasOwnProperty(file)) {
options.source_map.get().setSourceContent(file, sourcesContent[file]);
}
}
}
}
if (options.output) {
2014-05-19 17:29:01 +00:00
Cola.merge(output, options.output);
}
2014-05-19 17:29:01 +00:00
var stream = new Cola.OutputStream(output);
toplevel.print(stream);
return {
code : stream + "",
map : output.source_map + ""
};
};
// 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;
// }
2014-05-19 17:29:01 +00:00
// return doitem(Cola.AST_Node).sub;
// }
exports.describe_ast = function() {
2014-05-19 17:29:01 +00:00
var out = new Cola.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();
});
});
}
};
2014-05-19 17:29:01 +00:00
doitem(Cola.AST_Node);
return out + "";
};