implement UGLIFY_BUG_REPORT (#4516)

This commit is contained in:
Alex Lam S.L
2021-01-07 08:53:29 +00:00
committed by GitHub
parent 25321df959
commit 6c419bc083
4 changed files with 81 additions and 1 deletions

View File

@@ -31,6 +31,15 @@ test case will be closed.
<!--
Command-line or API call to UglifyJS without third party tools or libraries.
For users using bundlers or transpilers, you may be able to gather the required
information through setting the `UGLIFY_BUG_REPORT` environment variable:
UGLIFY_BUG_REPORT=1 (Linux)
set UGLIFY_BUG_REPORT=1 (Windows)
before running your usual build process. The resulting "minified" output should
contain the necessary details for this report.
-->
**JavaScript output or error produced.**

View File

@@ -265,7 +265,7 @@ if (paths.length) {
process.stdin.on("data", function(chunk) {
chunks.push(chunk);
}).on("end", function() {
files = [ chunks.join("") ];
files = { STDIN: chunks.join("") };
run();
});
process.stdin.resume();

40
test/mocha/bug-report.js Normal file
View File

@@ -0,0 +1,40 @@
var assert = require("assert");
var exec = require("child_process").exec;
describe("UGLIFY_BUG_REPORT", function() {
var env = Object.create(process.env);
env.UGLIFY_BUG_REPORT = 1;
it("Should generate bug report via API", function(done) {
exec('"' + process.argv[0] + '"', { env: env }, function(err, stdout) {
if (err) throw err;
assert.strictEqual(stdout, [
"// UGLIFY_BUG_REPORT",
"// <<undefined>>",
"",
"//-------------------------------------------------------------",
"// INPUT CODE",
"...---...",
"",
].join("\n"));
done();
}).stdin.end('console.log(require("./").minify("...---...").code);');
});
it("Should generate bug report via CLI", function(done) {
exec('"' + process.argv[0] + '" bin/uglifyjs -mc', { env: env }, function(err, stdout) {
if (err) throw err;
assert.strictEqual(stdout, [
"// UGLIFY_BUG_REPORT",
"// {",
'// "mangle": {},',
'// "compress": {}',
"// }",
"",
"//-------------------------------------------------------------",
"// STDIN",
"...---...",
"",
].join("\n"));
done();
}).stdin.end("...---...");
});
});

View File

@@ -23,6 +23,37 @@ new Function("exports", function() {
return code.join("\n\n");
}())(exports);
function to_comment(value) {
if (typeof value != "string") value = JSON.stringify(value, function(key, value) {
return typeof value == "function" ? "<[ " + value + " ]>" : value;
}, 2);
return "// " + value.replace(/\n/g, "\n// ");
}
if (+process.env["UGLIFY_BUG_REPORT"]) exports.minify = function(files, options) {
if (typeof options == "undefined") options = "<<undefined>>";
var code = [
"// UGLIFY_BUG_REPORT",
to_comment(options),
];
if (typeof files == "string") {
code.push("");
code.push("//-------------------------------------------------------------")
code.push("// INPUT CODE", files);
} else for (var name in files) {
code.push("");
code.push("//-------------------------------------------------------------")
code.push(to_comment(name), files[name]);
}
if (options.sourceMap && options.sourceMap.url) {
code.push("");
code.push("//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiJ9");
}
var result = { code: code.join("\n") };
if (options.sourceMap) result.map = '{"version":3,"sources":[],"names":[],"mappings":""}';
return result;
};
function describe_ast() {
var out = OutputStream({ beautify: true });
function doitem(ctor) {