Skip to content

Commit 89ae3fe

Browse files
alexander-akaitclaude
authored andcommitted
fix(javascript): enclose a wrapped source, and drop a wrapped hashbang, as terser does
Given both, `enclose` goes around `wrap` rather than being dropped, and a hashbang the wrapper no longer leaves first is blanked, which terser drops too, so every offset after it stays where the source has it. Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC
1 parent a890b28 commit 89ae3fe

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69671,11 +69671,20 @@ const prepareMinify = (text, options) => {
6967169671
if (given.wrap) {
6967269672
prefix = "(function(exports){\n";
6967369673
suffix = `\n})(typeof ${given.wrap}=='undefined'?(${given.wrap}={}):${given.wrap});`;
69674-
} else if (given.enclose) {
69674+
}
69675+
// `enclose` goes around `wrap`, as terser applies it after.
69676+
if (given.enclose) {
6967569677
const enclose = typeof given.enclose === "string" ? given.enclose : "";
6967669678
const colon = enclose.includes(":") ? enclose.indexOf(":") : enclose.length;
69677-
prefix = `(function(${enclose.slice(0, colon)}){\n`;
69678-
suffix = `\n})(${enclose.slice(colon + 1)})`;
69679+
prefix = `(function(${enclose.slice(0, colon)}){\n${prefix}`;
69680+
suffix = `${suffix}\n})(${enclose.slice(colon + 1)})`;
69681+
}
69682+
// A wrapped hashbang is no longer first, and terser drops it: blanked, it
69683+
// leaves every offset after it where the source has it.
69684+
if (prefix !== "" && text.startsWith("#!")) {
69685+
const end = text.search(/[\n\r\u2028\u2029]/);
69686+
const length = end === -1 ? text.length : end;
69687+
text = " ".repeat(length) + text.slice(length);
6967969688
}
6968069689
delete given.wrap;
6968169690
delete given.enclose;

‎test/unitCases/javascript/printer.unittest.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15846,6 +15846,24 @@ describe("JavaScript minifier printing a SyntaxTree", () => {
1584615846
expect(
1584715847
minifyOnePass("window.v = y;", { ...options, enclose: true }).code
1584815848
).toBe("(function(){window.v=y})();");
15849+
// `enclose` goes around `wrap`; a hashbang, no longer first, goes.
15850+
const bare = { compress: false, mangle: false };
15851+
expect(
15852+
minifyOnePass("exports.x = undefined;", {
15853+
...bare,
15854+
wrap: "lib",
15855+
enclose: "window,undefined:window"
15856+
}).code
15857+
).toBe(
15858+
'(function(window,undefined){(function(exports){exports.x=undefined})(typeof lib=="undefined"?lib={}:lib)})(window);'
15859+
);
15860+
expect(
15861+
minifyOnePass("#!/usr/bin/env node\r\nfoo();", { ...bare, enclose: true })
15862+
.code
15863+
).toBe("(function(){foo()})();");
15864+
expect(
15865+
minifyOnePass("#!/usr/bin/env node", { ...bare, wrap: "lib" }).code
15866+
).toBe('(function(exports){})(typeof lib=="undefined"?lib={}:lib);');
1584915867
});
1585015868

1585115869
it("should keep the names a global pattern keeps, however often it is asked", () => {

0 commit comments

Comments
 (0)