aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorb-fuze <b-fuze.root@live.com>2018-03-31 07:26:40 -0400
committerAlex Lam S.L <alexlamsl@gmail.com>2018-03-31 20:26:40 +0900
commit8adfc29f914efd2af62638491b6c3034e1cc8712 (patch)
tree8857313f6c50f96972ea274f16bb1565a2a59f2c
parent02f47e1713cb413ac0d2602039c18292f43db863 (diff)
downloadtracifyjs-8adfc29f914efd2af62638491b6c3034e1cc8712.tar.gz
tracifyjs-8adfc29f914efd2af62638491b6c3034e1cc8712.zip
Don't load source map until the JS source is fully received (#3040)
-rwxr-xr-xbin/uglifyjs20
-rw-r--r--test/input/pr-3040/expect.js2
-rw-r--r--test/input/pr-3040/input.js11
-rw-r--r--test/input/pr-3040/input.js.map1
-rw-r--r--test/mocha/cli.js33
5 files changed, 51 insertions, 16 deletions
diff --git a/bin/uglifyjs b/bin/uglifyjs
index 2ff1feb7..7617d781 100755
--- a/bin/uglifyjs
+++ b/bin/uglifyjs
@@ -46,7 +46,7 @@ program.option("--name-cache <file>", "File to hold mangled name mappings.");
program.option("--rename", "Force symbol expansion.");
program.option("--no-rename", "Disable symbol expansion.");
program.option("--self", "Build UglifyJS as a library (implies --wrap UglifyJS)");
-program.option("--source-map [options]", "Enable source map/specify source map options.", parse_source_map());
+program.option("--source-map [options]", "Enable source map/specify source map options.", parse_js());
program.option("--timings", "Display operations run time on STDERR.")
program.option("--toplevel", "Compress and/or mangle variables in toplevel scope.");
program.option("--verbose", "Print diagnostic messages.");
@@ -176,6 +176,11 @@ function run() {
UglifyJS.AST_Node.warn_function = function(msg) {
print_error("WARN: " + msg);
};
+ var content = program.sourceMap && program.sourceMap.content;
+ if (content && content != "inline") {
+ print_error("INFO: Using input source map: " + content);
+ options.sourceMap.content = read_file(content, content);
+ }
if (program.timings) options.timings = true;
try {
if (program.parse) {
@@ -377,19 +382,6 @@ function parse_js(flag) {
}
}
-function parse_source_map() {
- var parse = parse_js();
- return function(value, options) {
- var hasContent = options && "content" in options;
- var settings = parse(value, options);
- if (!hasContent && settings.content && settings.content != "inline") {
- print_error("INFO: Using input source map: " + settings.content);
- settings.content = read_file(settings.content, settings.content);
- }
- return settings;
- }
-}
-
function skip_key(key) {
return skip_keys.indexOf(key) >= 0;
}
diff --git a/test/input/pr-3040/expect.js b/test/input/pr-3040/expect.js
new file mode 100644
index 00000000..b911f3f8
--- /dev/null
+++ b/test/input/pr-3040/expect.js
@@ -0,0 +1,2 @@
+function _toConsumableArray(arr){if(Array.isArray(arr)){for(var i=0,arr2=Array(arr.length);i<arr.length;i++){arr2[i]=arr[i]}return arr2}else{return Array.from(arr)}}var _require=require("bar"),foo=_require.foo;var _require2=require("world"),hello=_require2.hello;foo.x.apply(foo,_toConsumableArray(foo.y(hello.z)));
+//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImlucHV0Mi5qcyJdLCJuYW1lcyI6WyJyZXF1aXJlIiwiYXJyIl0sIm1hcHBpbmdzIjoiMEpBQWNBLEtBQVFDIiwic291cmNlc0NvbnRlbnQiOlsiY29uc3Qge2Zvb30gPSByZXF1aXJlKFwiYmFyXCIpO1xuY29uc3Qge2hlbGxvfSA9IHJlcXVpcmUoXCJ3b3JsZFwiKTtcblxuZm9vLngoLi4uZm9vLnkoaGVsbG8ueikpO1xuIl19
diff --git a/test/input/pr-3040/input.js b/test/input/pr-3040/input.js
new file mode 100644
index 00000000..6515dcbd
--- /dev/null
+++ b/test/input/pr-3040/input.js
@@ -0,0 +1,11 @@
+function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
+
+var _require = require("bar"),
+ foo = _require.foo;
+
+var _require2 = require("world"),
+ hello = _require2.hello;
+
+foo.x.apply(foo, _toConsumableArray(foo.y(hello.z)));
+
+//# sourceMappingURL=input.js.map \ No newline at end of file
diff --git a/test/input/pr-3040/input.js.map b/test/input/pr-3040/input.js.map
new file mode 100644
index 00000000..f0883e22
--- /dev/null
+++ b/test/input/pr-3040/input.js.map
@@ -0,0 +1 @@
+{"version":3,"sources":["input2.js"],"names":["require","foo","hello","x","apply","_toConsumableArray","y","z"],"mappings":"kLAAcA,QAAQ,OAAfC,aAAAA,kBACSD,QAAQ,SAAjBE,gBAAAA,MAEPD,IAAIE,EAAJC,MAAAH,IAAAI,mBAASJ,IAAIK,EAAEJ,MAAMK","sourcesContent":["const {foo} = require(\"bar\");\nconst {hello} = require(\"world\");\n\nfoo.x(...foo.y(hello.z));\n"]} \ No newline at end of file
diff --git a/test/mocha/cli.js b/test/mocha/cli.js
index 7f0bd79f..7385e477 100644
--- a/test/mocha/cli.js
+++ b/test/mocha/cli.js
@@ -1,9 +1,9 @@
var assert = require("assert");
var exec = require("child_process").exec;
-var readFileSync = require("fs").readFileSync;
+var fs = require("fs");
function read(path) {
- return readFileSync(path, "utf8");
+ return fs.readFileSync(path, "utf8");
}
describe("bin/uglifyjs", function () {
@@ -106,6 +106,35 @@ describe("bin/uglifyjs", function () {
done();
});
});
+ it("Should not load source map before finish reading from STDIN", function(done) {
+ var mapFile = "tmp/input.js.map";
+ try {
+ fs.mkdirSync("./tmp");
+ } catch (e) {
+ if (e.code != "EEXIST") throw e;
+ }
+ try {
+ fs.unlinkSync(mapFile);
+ } catch (e) {
+ if (e.code != "ENOENT") throw e;
+ }
+ var command = [
+ uglifyjscmd,
+ "--source-map", "content=" + mapFile,
+ "--source-map", "url=inline"
+ ].join(" ");
+
+ var child = exec(command, function(err, stdout) {
+ if (err) throw err;
+
+ assert.strictEqual(stdout, read("test/input/pr-3040/expect.js"));
+ done();
+ });
+ setTimeout(function() {
+ fs.writeFileSync(mapFile, read("test/input/pr-3040/input.js.map"));
+ child.stdin.end(read("test/input/pr-3040/input.js"));
+ }, 1000);
+ });
it("Should work with --keep-fnames (mangle only)", function (done) {
var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m';