diff options
-rw-r--r-- | lib/minify.js | 14 | ||||
-rw-r--r-- | test/input/issue-3040/expect.js (renamed from test/input/pr-3040/expect.js) | 0 | ||||
-rw-r--r-- | test/input/issue-3040/input.js (renamed from test/input/pr-3040/input.js) | 0 | ||||
-rw-r--r-- | test/input/issue-3040/input.js.map (renamed from test/input/pr-3040/input.js.map) | 0 | ||||
-rw-r--r-- | test/input/issue-3294/input.js | 6 | ||||
-rw-r--r-- | test/input/issue-3294/output.js | 2 | ||||
-rw-r--r-- | test/mocha/cli.js | 7 | ||||
-rw-r--r-- | test/mocha/comments.js | 37 | ||||
-rw-r--r-- | test/mocha/sourcemaps.js | 12 |
9 files changed, 69 insertions, 9 deletions
diff --git a/lib/minify.js b/lib/minify.js index 5f865c63..1bb6309c 100644 --- a/lib/minify.js +++ b/lib/minify.js @@ -8,7 +8,7 @@ var to_base64 = typeof btoa == "undefined" ? function(str) { } : btoa; function read_source_map(name, code) { - var match = /\n\/\/# sourceMappingURL=data:application\/json(;.*?)?;base64,(.*)/.exec(code); + var match = /\n\/\/# sourceMappingURL=data:application\/json(;.*?)?;base64,(\S+)\s*$/.exec(code); if (!match) { AST_Node.warn("inline source map not found: " + name); return null; @@ -204,10 +204,14 @@ function minify(files, options) { result.code = stream.get(); if (options.sourceMap) { result.map = options.output.source_map.toString(); - if (options.sourceMap.url == "inline") { - result.code += "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," + to_base64(result.map); - } else if (options.sourceMap.url) { - result.code += "\n//# sourceMappingURL=" + options.sourceMap.url; + var url = options.sourceMap.url; + if (url) { + result.code = result.code.replace(/\n\/\/# sourceMappingURL=\S+\s*$/, ""); + if (url == "inline") { + result.code += "\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," + to_base64(result.map); + } else { + result.code += "\n//# sourceMappingURL=" + url; + } } } } diff --git a/test/input/pr-3040/expect.js b/test/input/issue-3040/expect.js index b911f3f8..b911f3f8 100644 --- a/test/input/pr-3040/expect.js +++ b/test/input/issue-3040/expect.js diff --git a/test/input/pr-3040/input.js b/test/input/issue-3040/input.js index 6515dcbd..6515dcbd 100644 --- a/test/input/pr-3040/input.js +++ b/test/input/issue-3040/input.js diff --git a/test/input/pr-3040/input.js.map b/test/input/issue-3040/input.js.map index f0883e22..f0883e22 100644 --- a/test/input/pr-3040/input.js.map +++ b/test/input/issue-3040/input.js.map diff --git a/test/input/issue-3294/input.js b/test/input/issue-3294/input.js new file mode 100644 index 00000000..09ee40f6 --- /dev/null +++ b/test/input/issue-3294/input.js @@ -0,0 +1,6 @@ +var Foo = function Foo(){console.log(1+2);}; new Foo(); + +//# sourceMappingURL=data:application/json;charset=utf-8;base64,DeadBeef +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjpudWxsLCJzb3VyY2VzIjpbInN0ZGluIl0sInNvdXJjZXNDb250ZW50IjpbImNsYXNzIEZvbyB7IGNvbnN0cnVjdG9yKCl7Y29uc29sZS5sb2coMSsyKTt9IH0gbmV3IEZvbygpO1xuIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLElBQU0sR0FBRyxHQUFDLEFBQUUsWUFBVyxFQUFFLENBQUMsT0FBTyxDQUFDLEdBQUcsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFBLEFBQUUsQ0FBQyxJQUFJLEdBQUcsRUFBRSxDQUFDOyJ9 + + diff --git a/test/input/issue-3294/output.js b/test/input/issue-3294/output.js new file mode 100644 index 00000000..f6d13804 --- /dev/null +++ b/test/input/issue-3294/output.js @@ -0,0 +1,2 @@ +new function(){console.log(3)}; +//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0ZGluIl0sIm5hbWVzIjpbImNvbnNvbGUiLCJsb2ciXSwibWFwcGluZ3MiOiJBQUErQyxJQUFyQyxXQUFnQkEsUUFBUUMsSUFBSSIsInNvdXJjZXNDb250ZW50IjpbImNsYXNzIEZvbyB7IGNvbnN0cnVjdG9yKCl7Y29uc29sZS5sb2coMSsyKTt9IH0gbmV3IEZvbygpO1xuIl19 diff --git a/test/mocha/cli.js b/test/mocha/cli.js index 6d6e1e80..f99a8be4 100644 --- a/test/mocha/cli.js +++ b/test/mocha/cli.js @@ -114,13 +114,12 @@ describe("bin/uglifyjs", function() { var child = exec(command, function(err, stdout) { if (err) throw err; - - assert.strictEqual(stdout, read("test/input/pr-3040/expect.js")); + assert.strictEqual(stdout, read("test/input/issue-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")); + fs.writeFileSync(mapFile, read("test/input/issue-3040/input.js.map")); + child.stdin.end(read("test/input/issue-3040/input.js")); }, 1000); }); it("Should work with --keep-fnames (mangle only)", function(done) { diff --git a/test/mocha/comments.js b/test/mocha/comments.js index 3907f2fa..04dd171e 100644 --- a/test/mocha/comments.js +++ b/test/mocha/comments.js @@ -277,6 +277,43 @@ describe("comments", function() { ].join("\n")); }); + it("Should not duplicate sourceMappingURL", function() { + var code = [ + "//# sourceMappingURL=input.map", + "print(42);", + "//# sourceMappingURL=input.map", + "", + "//# sourceMappingURL=input.map", + "//# sourceMappingURL=input.map", + "", + ].join("\n"); + + var result = UglifyJS.minify(code, { + output: { comments: true }, + }); + if (result.error) throw result.error; + assert.strictEqual(result.code, [ + "//# sourceMappingURL=input.map", + "print(42);", + "//# sourceMappingURL=input.map", + "//# sourceMappingURL=input.map", + "//# sourceMappingURL=input.map", + ].join("\n")); + + result = UglifyJS.minify(code, { + output: { comments: true }, + sourceMap: { url: "output.map" }, + }); + if (result.error) throw result.error; + assert.strictEqual(result.code, [ + "//# sourceMappingURL=input.map", + "print(42);", + "//# sourceMappingURL=input.map", + "//# sourceMappingURL=input.map", + "//# sourceMappingURL=output.map", + ].join("\n")); + }); + describe("comment before constant", function() { var js = 'function f() { /*c1*/ var /*c2*/ foo = /*c3*/ false; return foo; }'; diff --git a/test/mocha/sourcemaps.js b/test/mocha/sourcemaps.js index f625b227..78e100ba 100644 --- a/test/mocha/sourcemaps.js +++ b/test/mocha/sourcemaps.js @@ -169,6 +169,18 @@ describe("sourcemaps", function() { map = JSON.parse(result.map); assert.ok(!("sourcesContent" in map)); }); + it("Should parse the correct sourceMappingURL", function() { + var result = UglifyJS.minify(read("./test/input/issue-3294/input.js"), { + compress: { toplevel: true }, + sourceMap: { + content: "inline", + includeSources: true, + url: "inline" + } + }); + if (result.error) throw result.error; + assert.strictEqual(result.code + "\n", readFileSync("test/input/issue-3294/output.js", "utf8")); + }); }); describe("sourceMapInline", function() { |