fix: ASI in concatenated module only when necessary

This commit is contained in:
fi3ework 2024-08-26 19:26:46 +08:00
parent 09543e7d8e
commit 362935e340
6 changed files with 21 additions and 2 deletions

View File

@ -146,6 +146,7 @@ if (!ReferencerClass.prototype.PropertyDefinition) {
* @property {string | undefined} interopNamespaceObject2Name
* @property {boolean} interopDefaultAccessUsed
* @property {string | undefined} interopDefaultAccessName
* @property {boolean} prefixAsi
*/
/**
@ -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)
@ -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);
}
@ -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);
@ -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 +=
@ -1873,7 +1885,8 @@ ${defineGetters}`
interopNamespaceObject2Used: false,
interopNamespaceObject2Name: undefined,
interopDefaultAccessUsed: false,
interopDefaultAccessName: undefined
interopDefaultAccessName: undefined,
prefixAsi: false
};
break;
case "external":

View File

@ -0,0 +1,6 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
optimization: {
concatenateModules: true
}
};