Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ASI in concatenated module only when necessary #18709

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions lib/optimize/ConcatenatedModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ if (!ReferencerClass.prototype.PropertyDefinition) {
* @property {string | undefined} interopNamespaceObject2Name
* @property {boolean} interopDefaultAccessUsed
* @property {string | undefined} interopDefaultAccessName
* @property {boolean} prefixAsi
*/

/**
Expand All @@ -172,6 +173,7 @@ if (!ReferencerClass.prototype.PropertyDefinition) {

/** @typedef {Set<string>} UsedNames */

const CHARS_REQUIRING_SEMICOLON = ["[", "(", "+", "-", "/"];
const RESERVED_NAMES = new Set(
[
// internal names (should always be renamed)
Expand Down Expand Up @@ -1467,6 +1469,12 @@ class ConcatenatedModule extends Module {
);
const r = /** @type {Range} */ (reference.identifier.range);
const source = info.source;

// in case finalName starts with a character that requires a semicolon
if (CHARS_REQUIRING_SEMICOLON.includes(finalName[0])) {
info.prefixAsi = true;
}

// range is extended by 2 chars to cover the appended "._"
source.replace(r[0], r[1] + 1, finalName);
}
Expand Down Expand Up @@ -1660,10 +1668,13 @@ ${defineGetters}`
switch (info.type) {
case "concatenated": {
result.add(
`\n;// CONCATENATED MODULE: ${info.module.readableIdentifier(
`\n// CONCATENATED MODULE: ${info.module.readableIdentifier(
requestShortener
)}\n`
);
if (/** @type {ConcatenatedModuleInfo} */ (rawInfo).prefixAsi) {
result.add(";");
}
result.add(info.source);
if (info.chunkInitFragments) {
for (const f of info.chunkInitFragments) chunkInitFragments.push(f);
Expand Down Expand Up @@ -1825,6 +1836,7 @@ ${defineGetters}`
info.chunkInitFragments = chunkInitFragments;
info.globalScope = globalScope;
info.moduleScope = moduleScope;
info.prefixAsi = CHARS_REQUIRING_SEMICOLON.includes(code[0]);
} catch (err) {
/** @type {Error} */
(err).message +=
Expand Down Expand Up @@ -1873,7 +1885,8 @@ ${defineGetters}`
interopNamespaceObject2Used: false,
interopNamespaceObject2Name: undefined,
interopDefaultAccessUsed: false,
interopDefaultAccessName: undefined
interopDefaultAccessName: undefined,
prefixAsi: false
};
break;
case "external":
Expand Down
6 changes: 6 additions & 0 deletions test/configCases/scope-hoisting/issue-11897/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
optimization: {
concatenateModules: true
}
};
Loading