Skip to content

Commit

Permalink
fix corner cases in sourceMap (#3397)
Browse files Browse the repository at this point in the history
fixes #3255
fixes #3294
  • Loading branch information
alexlamsl committed May 4, 2019
1 parent a89d424 commit 11cdab7
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 9 deletions.
14 changes: 9 additions & 5 deletions lib/minify.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
}
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions 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


2 changes: 2 additions & 0 deletions test/input/issue-3294/output.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions test/mocha/cli.js
Expand Up @@ -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) {
Expand Down
37 changes: 37 additions & 0 deletions test/mocha/comments.js
Expand Up @@ -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; }';

Expand Down
12 changes: 12 additions & 0 deletions test/mocha/sourcemaps.js
Expand Up @@ -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() {
Expand Down

0 comments on commit 11cdab7

Please sign in to comment.