Add support for enclose option. Closes #139.

This commit is contained in:
Jake Harding 2013-02-28 21:21:14 -08:00
parent cb0c576bdd
commit 846498e421
2 changed files with 34 additions and 0 deletions

View File

@ -31,6 +31,7 @@ For example -p 3 will drop 3 directories from file names and ensure they are rel
Pass options like -c hoist_vars=false,if_return=false. \ Pass options like -c hoist_vars=false,if_return=false. \
Use -c with no argument to use the default compression options.") Use -c with no argument to use the default compression options.")
.describe("d", "Global definitions") .describe("d", "Global definitions")
.describe("e", "Embed everything in a big function, with a configurable parameter/argument list.")
.describe("comments", "Preserve copyright comments in the output. \ .describe("comments", "Preserve copyright comments in the output. \
By default this works like Google Closure, keeping JSDoc-style comments that contain \"@license\" or \"@preserve\". \ By default this works like Google Closure, keeping JSDoc-style comments that contain \"@license\" or \"@preserve\". \
@ -61,6 +62,7 @@ You need to pass an argument to this option to specify the name that your module
.alias("d", "define") .alias("d", "define")
.alias("r", "reserved") .alias("r", "reserved")
.alias("V", "version") .alias("V", "version")
.alias("e", "enclose")
.string("source-map") .string("source-map")
.string("source-map-root") .string("source-map-root")
@ -69,6 +71,7 @@ You need to pass an argument to this option to specify the name that your module
.string("m") .string("m")
.string("c") .string("c")
.string("d") .string("d")
.string("e")
.string("comments") .string("comments")
.string("wrap") .string("wrap")
.boolean("export-all") .boolean("export-all")
@ -242,6 +245,16 @@ if (ARGS.wrap) {
TOPLEVEL = TOPLEVEL.wrap_commonjs(ARGS.wrap, ARGS.export_all); TOPLEVEL = TOPLEVEL.wrap_commonjs(ARGS.wrap, ARGS.export_all);
} }
if (ARGS.enclose) {
var arg_parameter_list = ARGS.enclose;
if (!(arg_parameter_list instanceof Array)) {
arg_parameter_list = [arg_parameter_list];
}
TOPLEVEL = TOPLEVEL.wrap_enclose(arg_parameter_list);
}
var SCOPE_IS_NEEDED = COMPRESS || MANGLE || ARGS.lint; var SCOPE_IS_NEEDED = COMPRESS || MANGLE || ARGS.lint;
if (SCOPE_IS_NEEDED) { if (SCOPE_IS_NEEDED) {

View File

@ -285,6 +285,27 @@ var AST_Toplevel = DEFNODE("Toplevel", "globals", {
$propdoc: { $propdoc: {
globals: "[Object/S] a map of name -> SymbolDef for all undeclared names", globals: "[Object/S] a map of name -> SymbolDef for all undeclared names",
}, },
wrap_enclose: function(arg_parameter_pairs) {
var self = this;
var args = [];
var parameters = [];
arg_parameter_pairs.forEach(function(pair) {
var split = pair.split(":");
args.push(split[0]);
parameters.push(split[1]);
});
var wrapped_tl = "(function(" + parameters.join(",") + "){ '$ORIG'; })(" + args.join(",") + ")";
wrapped_tl = parse(wrapped_tl);
wrapped_tl = wrapped_tl.transform(new TreeTransformer(function before(node){
if (node instanceof AST_Directive && node.value == "$ORIG") {
return MAP.splice(self.body);
}
}));
return wrapped_tl;
},
wrap_commonjs: function(name, export_all) { wrap_commonjs: function(name, export_all) {
var self = this; var self = this;
var to_export = []; var to_export = [];