diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ast.js | 23 | ||||
-rw-r--r-- | lib/minify.js | 13 |
2 files changed, 23 insertions, 13 deletions
@@ -337,18 +337,25 @@ var AST_Toplevel = DEFNODE("Toplevel", "globals", { $propdoc: { globals: "[Object/S] a map of name -> SymbolDef for all undeclared names", }, - wrap_commonjs: function(name) { + wrap: function(name) { var body = this.body; - var wrapped_tl = "(function(exports){'$ORIG';})(typeof " + name + "=='undefined'?(" + name + "={}):" + name + ");"; - wrapped_tl = parse(wrapped_tl); - wrapped_tl = wrapped_tl.transform(new TreeTransformer(function(node) { + return parse([ + "(function(exports){'$ORIG';})(typeof ", + name, + "=='undefined'?(", + name, + "={}):", + name, + ");" + ].join(""), { + filename: "wrap=" + JSON.stringify(name) + }).transform(new TreeTransformer(function(node) { if (node instanceof AST_Directive && node.value == "$ORIG") { return MAP.splice(body); } })); - return wrapped_tl; }, - wrap_enclose: function(args_values) { + enclose: function(args_values) { if (typeof args_values != "string") args_values = ""; var index = args_values.indexOf(":"); if (index < 0) index = args_values.length; @@ -359,7 +366,9 @@ var AST_Toplevel = DEFNODE("Toplevel", "globals", { '){"$ORIG"})(', args_values.slice(index + 1), ")" - ].join("")).transform(new TreeTransformer(function(node) { + ].join(""), { + filename: "enclose=" + JSON.stringify(args_values) + }).transform(new TreeTransformer(function(node) { if (node instanceof AST_Directive && node.value == "$ORIG") { return MAP.splice(body); } diff --git a/lib/minify.js b/lib/minify.js index 1bb6309c..9eebc373 100644 --- a/lib/minify.js +++ b/lib/minify.js @@ -151,12 +151,13 @@ function minify(files, options) { if (quoted_props) { reserve_quoted_keys(toplevel, quoted_props); } - if (options.wrap) { - toplevel = toplevel.wrap_commonjs(options.wrap); - } - if (options.enclose) { - toplevel = toplevel.wrap_enclose(options.enclose); - } + [ "enclose", "wrap" ].forEach(function(action) { + var option = options[action]; + if (!option) return; + var orig = toplevel.print_to_string().slice(0, -1); + toplevel = toplevel[action](option); + files[toplevel.start.file] = toplevel.print_to_string().replace(orig, ""); + }); if (timings) timings.rename = Date.now(); if (options.rename) { toplevel.figure_out_scope(options.mangle); |