Don't load source map until the JS source is fully received (#3040)
authorb-fuze <b-fuze.root@live.com>
Sat, 31 Mar 2018 11:26:40 +0000 (07:26 -0400)
committerAlex Lam S.L <alexlamsl@gmail.com>
Sat, 31 Mar 2018 11:26:40 +0000 (20:26 +0900)
bin/uglifyjs
test/input/pr-3040/expect.js [new file with mode: 0644]
test/input/pr-3040/input.js [new file with mode: 0644]
test/input/pr-3040/input.js.map [new file with mode: 0644]
test/mocha/cli.js

index 2ff1feb..7617d78 100755 (executable)
@@ -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 (file)
index 0000000..b911f3f
--- /dev/null
@@ -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 (file)
index 0000000..6515dcb
--- /dev/null
@@ -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 (file)
index 0000000..f0883e2
--- /dev/null
@@ -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
index 7f0bd79..7385e47 100644 (file)
@@ -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';