diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2019-05-04 20:25:52 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-04 20:25:52 +0800 |
commit | 354fec8a9c3c8540d7e964ecbfcb38deaae86a3e (patch) | |
tree | fed20f61e163195ad4d8eb632e79212e6dcb5587 /lib | |
parent | 11cdab745d29313a4e1799c65d13f7d6d5a45938 (diff) | |
download | tracifyjs-354fec8a9c3c8540d7e964ecbfcb38deaae86a3e.tar.gz tracifyjs-354fec8a9c3c8540d7e964ecbfcb38deaae86a3e.zip |
make `enclose` & `wrap` work with `sourceMap` (#3396)
fixes #3313
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); |