--acorn Use Acorn for parsing. [boolean]
--spidermonkey Assume input fles are SpiderMonkey AST format (as JSON).
[boolean]
+ --self Build itself (UglifyJS2) as a library (implies
+ --wrap=UglifyJS --export-all) [boolean]
+ --wrap Embed everything in a big function, making the “exports”
+ and “global” variables available. You need to pass an
+ argument to this option to specify the name that your
+ module will take when included in, say, a browser.
+ [string]
+ --export-all Only used when --wrap, this tells UglifyJS to add code to
+ automatically export all globals. [boolean]
-v, --verbose Verbose [boolean]
Specify `--output` (`-o`) to declare the output file. Otherwise the output
.describe("stats", "Display operations run time on STDERR.")
.describe("acorn", "Use Acorn for parsing.")
.describe("spidermonkey", "Assume input fles are SpiderMonkey AST format (as JSON).")
+ .describe("self", "Build itself (UglifyJS2) as a library (implies --wrap=UglifyJS --export-all)")
+ .describe("wrap", "Embed everything in a big function, making the “exports” and “global” variables available. \
+You need to pass an argument to this option to specify the name that your module will take when included in, say, a browser.")
+ .describe("export-all", "Only used when --wrap, this tells UglifyJS to add code to automatically export all globals.")
.describe("v", "Verbose")
.alias("p", "prefix")
.string("c")
.string("d")
.string("comments")
+ .string("wrap")
+ .boolean("export-all")
+ .boolean("self")
.boolean("v")
.boolean("stats")
.boolean("acorn")
var files = ARGS._.slice();
+if (ARGS.self) {
+ if (files.length > 0) {
+ sys.error("WARN: Ignoring input files since --self was passed");
+ }
+ files = UglifyJS.FILES;
+ if (!ARGS.wrap) ARGS.wrap = "UglifyJS";
+ ARGS.export_all = true;
+}
+
var ORIG_MAP = ARGS.in_source_map;
if (ORIG_MAP) {
TOPLEVEL = UglifyJS.AST_Node.from_mozilla_ast(TOPLEVEL);
});
+if (ARGS.wrap) {
+ TOPLEVEL = TOPLEVEL.wrap_commonjs(ARGS.wrap, ARGS.export_all);
+}
+
var SCOPE_IS_NEEDED = COMPRESS || MANGLE;
if (SCOPE_IS_NEEDED) {
}, AST_Block);
var AST_Toplevel = DEFNODE("Toplevel", "globals", {
- $documentation: "The toplevel scope"
+ $documentation: "The toplevel scope",
+ wrap_commonjs: function(name, export_all) {
+ var self = this;
+ if (export_all) {
+ self.figure_out_scope();
+ var to_export = [];
+ self.walk(new TreeWalker(function(node){
+ if (node instanceof AST_SymbolDeclaration && node.definition().global) {
+ to_export.push(node.name);
+ }
+ }));
+ }
+ var wrapped_tl = "(function(exports, global){ global['" + name + "'] = exports; '$ORIG'; '$EXPORTS'; }({}, (function(){return this}())))";
+ wrapped_tl = parse(wrapped_tl);
+ wrapped_tl = wrapped_tl.transform(new TreeTransformer(function before(node){
+ if (node instanceof AST_SimpleStatement) {
+ node = node.body;
+ if (node instanceof AST_String) switch (node.getValue()) {
+ case "$ORIG":
+ return new AST_BlockStatement(self);
+ case "$EXPORTS":
+ var body = [];
+ to_export.forEach(function(name){
+ body.push(new AST_SimpleStatement({
+ body: new AST_Assign({
+ left: new AST_Sub({
+ expression: new AST_SymbolRef({ name: "exports" }),
+ property: new AST_String({ value: name }),
+ }),
+ operator: "=",
+ right: new AST_SymbolRef({ name: name }),
+ }),
+ }));
+ });
+ return new AST_BlockStatement({ body: body });
+ }
+ }
+ }));
+ return wrapped_tl;
+ }
}, AST_Scope);
var AST_Lambda = DEFNODE("Lambda", "name argnames uses_arguments", {
}
};
-load_global("../lib/utils.js");
-load_global("../lib/ast.js");
-load_global("../lib/parse.js");
-load_global("../lib/transform.js");
-load_global("../lib/scope.js");
-load_global("../lib/output.js");
-load_global("../lib/compress.js");
-load_global("../lib/sourcemap.js");
-load_global("../lib/mozilla-ast.js");
+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",
+ "../lib/mozilla-ast.js"
+].map(function(file){
+ return path.join(path.dirname(fs.realpathSync(__filename)), file);
+});
+
+FILES.forEach(load_global);
UglifyJS.AST_Node.warn_function = function(txt) {
sys.error("WARN: " + txt);