allow literal string as souce map input on CLI

fixes #123
This commit is contained in:
alexlamsl 2017-04-15 19:01:53 +08:00
parent 169cb802a9
commit 4da6b564db
2 changed files with 37 additions and 6 deletions

View File

@ -111,7 +111,7 @@ if (program.parse) {
var convert_path = function(name) { var convert_path = function(name) {
return name; return name;
}; };
if (program.sourceMap && "base" in program.sourceMap) { if (typeof program.sourceMap == "object" && "base" in program.sourceMap) {
convert_path = function() { convert_path = function() {
var base = program.sourceMap.base; var base = program.sourceMap.base;
delete options.sourceMap.base; delete options.sourceMap.base;
@ -291,7 +291,9 @@ function parse_js(flag, constants) {
if (!(node instanceof UglifyJS.AST_Sequence)) throw node; if (!(node instanceof UglifyJS.AST_Sequence)) throw node;
function to_string(value) { function to_string(value) {
return value instanceof UglifyJS.AST_Constant ? value.getValue() : value.print_to_string(); return value instanceof UglifyJS.AST_Constant ? value.getValue() : value.print_to_string({
quote_keys: true
});
} }
})); }));
} catch(ex) { } catch(ex) {
@ -308,7 +310,7 @@ function parse_source_map() {
var settings = parse(value, options); var settings = parse(value, options);
if (!hasContent && settings.content && settings.content != "inline") { if (!hasContent && settings.content && settings.content != "inline") {
console.error("INFO: Using input source map:", settings.content); console.error("INFO: Using input source map:", settings.content);
settings.content = read_file(settings.content); settings.content = read_file(settings.content, settings.content);
} }
return settings; return settings;
} }

View File

@ -2,6 +2,10 @@ var assert = require("assert");
var exec = require("child_process").exec; var exec = require("child_process").exec;
var readFileSync = require("fs").readFileSync; var readFileSync = require("fs").readFileSync;
function read(path) {
return readFileSync(path, "utf8");
}
describe("bin/uglifyjs", function () { describe("bin/uglifyjs", function () {
var uglifyjscmd = '"' + process.argv[0] + '" bin/uglifyjs'; var uglifyjscmd = '"' + process.argv[0] + '" bin/uglifyjs';
it("should produce a functional build when using --self", function (done) { it("should produce a functional build when using --self", function (done) {
@ -137,7 +141,7 @@ describe("bin/uglifyjs", function () {
exec(command, function (err, stdout) { exec(command, function (err, stdout) {
if (err) throw err; if (err) throw err;
assert.strictEqual(stdout, readFileSync("test/input/issue-1482/default.js", "utf8")); assert.strictEqual(stdout, read("test/input/issue-1482/default.js"));
done(); done();
}); });
}); });
@ -147,7 +151,7 @@ describe("bin/uglifyjs", function () {
exec(command, function (err, stdout) { exec(command, function (err, stdout) {
if (err) throw err; if (err) throw err;
assert.strictEqual(stdout, readFileSync("test/input/issue-1482/bracketize.js", "utf8")); assert.strictEqual(stdout, read("test/input/issue-1482/bracketize.js"));
done(); done();
}); });
}); });
@ -157,7 +161,7 @@ describe("bin/uglifyjs", function () {
exec(command, function (err, stdout) { exec(command, function (err, stdout) {
if (err) throw err; if (err) throw err;
assert.strictEqual(stdout, readFileSync("test/input/issue-520/output.js", "utf8")); assert.strictEqual(stdout, read("test/input/issue-520/output.js"));
done(); done();
}); });
}); });
@ -300,4 +304,29 @@ describe("bin/uglifyjs", function () {
done(); done();
}); });
}); });
it("Should handle literal string as source map input", function(done) {
var command = [
uglifyjscmd,
"test/input/issue-1236/simple.js",
"--source-map",
'content="' + read_map() + '",url=inline'
].join(" ");
exec(command, function (err, stdout) {
if (err) throw err;
assert.strictEqual(stdout, [
'"use strict";var foo=function foo(x){return"foo "+x};console.log(foo("bar"));',
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImluZGV4LmpzIl0sIm5hbWVzIjpbImZvbyIsIngiLCJjb25zb2xlIiwibG9nIl0sIm1hcHBpbmdzIjoiWUFBQSxJQUFJQSxLQUFNLFFBQU5BLEtBQU1DLEdBQUEsTUFBSyxPQUFTQSxFQUN4QkMsU0FBUUMsSUFBSUgsSUFBSSJ9",
""
].join("\n"));
done();
});
function read_map() {
var map = JSON.parse(read("./test/input/issue-1236/simple.js.map"));
delete map.sourcesContent;
return JSON.stringify(map).replace(/"/g, '\\"');
}
});
}); });