Skip to content

Commit f8ae4f5

Browse files
authored
perf: reuse cached free names instead of re-parsing modules for the entry IIFE rename (#22206)
1 parent 64197bc commit f8ae4f5

17 files changed

Lines changed: 273 additions & 121 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Reuse cached free names instead of re-parsing modules for the entry IIFE rename.

‎lib/javascript/JavascriptModulesPlugin.js‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2140,9 +2140,23 @@ class JavascriptModulesPlugin {
21402140
const nonInlinedModuleThroughIdentifiers = new Set();
21412141
/** @type {Map<Module, Source>} */
21422142
const inlinedModuleSources = new Map();
2143+
const { codeGenerationResults, chunk } = renderContext;
21432144

21442145
for (const m of allModules) {
21452146
const isInlinedModule = inlinedModules && inlinedModules.has(m);
2147+
const result = codeGenerationResults.get(m, chunk.runtime);
2148+
// Reuse cached `freeNames` to avoid re-parse for non-inlined modules
2149+
if (!isInlinedModule) {
2150+
const freeNames = result.data && result.data.get("freeNames");
2151+
2152+
if (freeNames) {
2153+
for (const name of freeNames) {
2154+
nonInlinedModuleThroughIdentifiers.add(name);
2155+
}
2156+
continue;
2157+
}
2158+
}
2159+
21462160
const moduleSource = this.renderModule(
21472161
m,
21482162
{
@@ -2178,16 +2192,20 @@ class JavascriptModulesPlugin {
21782192

21792193
const analysis = analyzeScope(ast);
21802194

2195+
/** @type {Set<string>} */
2196+
const freeNames = new Set();
21812197
for (const ref of analysis.unresolvedReferences) {
2198+
freeNames.add(ref.identifier.name);
21822199
nonInlinedModuleThroughIdentifiers.add(ref.identifier.name);
21832200
}
2201+
if (result.data === undefined) result.data = new Map();
2202+
result.data.set("freeNames", freeNames);
21842203
}
21852204

21862205
// Codegen reports each inlined entry's rendered top-level declarations and
21872206
// free names, so where nothing collides there is no renaming to do and the
21882207
// entries need not be parsed at all.
21892208
fastPath: {
2190-
const { codeGenerationResults, chunk } = renderContext;
21912209
/** @type {Set<string>} */
21922210
const usedNames = new Set(nonInlinedModuleThroughIdentifiers);
21932211
for (const name of RESERVED_NAMES) usedNames.add(name);

‎lib/optimize/ConcatenatedModule.js‎

Lines changed: 99 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,8 +1797,8 @@ class ConcatenatedModule extends Module {
17971797
/** @type {Set<ConcatenatedModuleInfo | ExternalModuleInfo>} */
17981798
const neededEscapeNamespaceObjects = new Set();
17991799

1800-
// #region [1] ANALYSE: run each inner module's own codeGeneration; a
1801-
// non-wrapped body is parsed and scope-analysed so [3] can rename its locals
1800+
// #region [1] ANALYSE: run each inner module's own codeGeneration and
1801+
// scope-analyse the body; a non-wrapped one keeps its scopes for [3]
18021802
for (const info of moduleToInfoMap.values()) {
18031803
this._analyseModule(
18041804
moduleToInfoMap,
@@ -1895,8 +1895,18 @@ class ConcatenatedModule extends Module {
18951895
)) {
18961896
allUsedNames.add(name);
18971897
}
1898-
// A wrapped body has no scope analysis, so its references are found
1899-
// textually. They still have to be resolved here: binding one is what
1898+
// only its free globals reach freeNames; the reference tokens among
1899+
// them are resolved textually below and patched the same way in [4]
1900+
for (const reference of /** @type {Reference[]} */ (
1901+
info.unresolvedReferences
1902+
)) {
1903+
const name = reference.identifier.name;
1904+
if (!ConcatenationScope.isModuleReference(name)) {
1905+
freeNames.add(name);
1906+
}
1907+
}
1908+
// A wrapped body's references are found textually, as [4] patches
1909+
// them. They still have to be resolved here: binding one is what
19001910
// flags the interop objects it needs, and [3] allocates their names.
19011911
/** @type {Set<string>} */
19021912
const visitedReferences = new Set();
@@ -2358,8 +2368,8 @@ class ConcatenatedModule extends Module {
23582368
}
23592369
}
23602370
} else {
2361-
// Wrapped bodies are not re-parsed: find their reference tokens
2362-
// textually. [2] scanned the same source, so this reuses its result.
2371+
// A wrapped body's reference tokens are found textually rather than
2372+
// in its AST. [2] scanned the same source, so this reuses its result.
23632373
/** @type {Map<string, string>} */
23642374
const finalNames = new Map();
23652375
const source = /** @type {ReplaceSource} */ (info.source);
@@ -3058,123 +3068,92 @@ ${defineGetters}`
30583068
codeGenerationResults,
30593069
usedNames
30603070
) {
3061-
if (info.type === "concatenated") {
3062-
const m = info.module;
3063-
if (info.wrapped) {
3064-
// the wrapper isolates the body's top-level declarations, so only a
3065-
// concatenation scope is needed, rewriting in-scope `require()` targets.
3066-
// Stage 2 claims its identifiers textually, in place of that analysis.
3067-
const concatenationScope = new ConcatenationScope(
3068-
modulesMap,
3069-
info,
3070-
usedNames
3071-
);
3072-
info.concatenationScope = concatenationScope;
3073-
const codeGenResult = m.codeGeneration({
3074-
dependencyTemplates,
3075-
runtimeTemplate,
3076-
moduleGraph,
3077-
chunkGraph,
3078-
runtime,
3079-
runtimes,
3080-
concatenationScope,
3081-
codeGenerationResults,
3082-
sourceTypes: JAVASCRIPT_TYPES
3083-
});
3084-
const source =
3085-
/** @type {Source} */
3086-
(codeGenResult.sources.get(JAVASCRIPT_TYPE));
3087-
const data = codeGenResult.data;
3088-
info.runtimeRequirements =
3089-
/** @type {ReadOnlyRuntimeRequirements} */
3090-
(codeGenResult.runtimeRequirements);
3091-
info.internalSource = source;
3092-
info.source = new ReplaceSource(source);
3093-
info.chunkInitFragments = data && data.get("chunkInitFragments");
3094-
} else {
3095-
try {
3096-
// Create a concatenation scope to track and capture information
3097-
const concatenationScope = new ConcatenationScope(
3098-
modulesMap,
3099-
info,
3100-
usedNames
3101-
);
3071+
if (info.type !== "concatenated") return;
3072+
const m = info.module;
3073+
try {
3074+
// Create a concatenation scope to track and capture information
3075+
const concatenationScope = new ConcatenationScope(
3076+
modulesMap,
3077+
info,
3078+
usedNames
3079+
);
31023080

3103-
// Not cached: Compilation memoizes the outer codeGeneration per
3104-
// (module, runtime), so inner generation never recomputes usefully.
3105-
const codeGenResult = m.codeGeneration({
3106-
dependencyTemplates,
3107-
runtimeTemplate,
3108-
moduleGraph,
3109-
chunkGraph,
3110-
runtime,
3111-
runtimes,
3112-
concatenationScope,
3113-
codeGenerationResults,
3114-
sourceTypes: JAVASCRIPT_TYPES
3115-
});
3116-
const source =
3117-
/** @type {Source} */
3118-
(codeGenResult.sources.get(JAVASCRIPT_TYPE));
3119-
const data = codeGenResult.data;
3120-
const chunkInitFragments = data && data.get("chunkInitFragments");
3121-
const code = source.source().toString();
3122-
3123-
/** @type {Program} */
3124-
let ast;
3125-
3126-
try {
3127-
const { experiments } = this.compilation.options;
3128-
3129-
({ ast } = JavascriptParser._parse(
3130-
code,
3131-
{
3132-
sourceType: "module",
3133-
ranges: true,
3134-
// generated code contains phase imports when the experiments are on
3135-
importPhases: Boolean(
3136-
experiments.deferImport || experiments.sourceImport
3137-
)
3138-
},
3139-
JavascriptParser._getModuleParseFunction(this.compilation, m)
3140-
));
3141-
} catch (_err) {
3142-
const err =
3143-
/** @type {Error & { loc?: { line: number, column: number } }} */
3144-
(_err);
3145-
if (
3146-
err.loc &&
3147-
typeof err.loc === "object" &&
3148-
typeof err.loc.line === "number"
3149-
) {
3150-
const lineNumber = err.loc.line;
3151-
const lines = code.split("\n");
3152-
err.message += `\n| ${lines
3153-
.slice(Math.max(0, lineNumber - 3), lineNumber + 2)
3154-
.join("\n| ")}`;
3155-
}
3156-
throw err;
3157-
}
3158-
const analysis = analyzeScope(ast);
3159-
const resultSource = new ReplaceSource(source);
3160-
info.runtimeRequirements =
3161-
/** @type {ReadOnlyRuntimeRequirements} */
3162-
(codeGenResult.runtimeRequirements);
3163-
info.ast = ast;
3164-
info.internalSource = source;
3165-
info.source = resultSource;
3166-
info.chunkInitFragments = chunkInitFragments;
3167-
info.globalScope = analysis.globalScope;
3168-
info.moduleScope = analysis.moduleScope;
3169-
info.unresolvedReferences = analysis.unresolvedReferences;
3170-
info.concatenationScope = concatenationScope;
3171-
} catch (err) {
3172-
/** @type {Error} */
3173-
(err).message +=
3174-
`\nwhile analyzing module ${m.identifier()} for concatenation`;
3175-
throw err;
3081+
// Not cached: Compilation memoizes the outer codeGeneration per
3082+
// (module, runtime), so inner generation never recomputes usefully.
3083+
const codeGenResult = m.codeGeneration({
3084+
dependencyTemplates,
3085+
runtimeTemplate,
3086+
moduleGraph,
3087+
chunkGraph,
3088+
runtime,
3089+
runtimes,
3090+
concatenationScope,
3091+
codeGenerationResults,
3092+
sourceTypes: JAVASCRIPT_TYPES
3093+
});
3094+
const source =
3095+
/** @type {Source} */
3096+
(codeGenResult.sources.get(JAVASCRIPT_TYPE));
3097+
const data = codeGenResult.data;
3098+
const chunkInitFragments = data && data.get("chunkInitFragments");
3099+
const code = source.source().toString();
3100+
3101+
/** @type {Program} */
3102+
let ast;
3103+
3104+
try {
3105+
const { experiments } = this.compilation.options;
3106+
3107+
({ ast } = JavascriptParser._parse(
3108+
code,
3109+
{
3110+
// a wrapped CommonJS body may `return` at its top level
3111+
sourceType: info.wrapped ? "auto" : "module",
3112+
ranges: true,
3113+
// generated code contains phase imports when the experiments are on
3114+
importPhases: Boolean(
3115+
experiments.deferImport || experiments.sourceImport
3116+
)
3117+
},
3118+
JavascriptParser._getModuleParseFunction(this.compilation, m)
3119+
));
3120+
} catch (_err) {
3121+
const err =
3122+
/** @type {Error & { loc?: { line: number, column: number } }} */
3123+
(_err);
3124+
if (
3125+
err.loc &&
3126+
typeof err.loc === "object" &&
3127+
typeof err.loc.line === "number"
3128+
) {
3129+
const lineNumber = err.loc.line;
3130+
const lines = code.split("\n");
3131+
err.message += `\n| ${lines
3132+
.slice(Math.max(0, lineNumber - 3), lineNumber + 2)
3133+
.join("\n| ")}`;
31763134
}
3135+
throw err;
31773136
}
3137+
const analysis = analyzeScope(ast);
3138+
info.runtimeRequirements =
3139+
/** @type {ReadOnlyRuntimeRequirements} */
3140+
(codeGenResult.runtimeRequirements);
3141+
info.internalSource = source;
3142+
info.source = new ReplaceSource(source);
3143+
info.chunkInitFragments = chunkInitFragments;
3144+
info.unresolvedReferences = analysis.unresolvedReferences;
3145+
info.concatenationScope = concatenationScope;
3146+
// the wrapper isolates the body's declarations, so [3] renames nothing in
3147+
// it and its scopes are not kept; [2] claims its identifiers textually
3148+
if (info.wrapped) return;
3149+
info.ast = ast;
3150+
info.globalScope = analysis.globalScope;
3151+
info.moduleScope = analysis.moduleScope;
3152+
} catch (err) {
3153+
/** @type {Error} */
3154+
(err).message +=
3155+
`\nwhile analyzing module ${m.identifier()} for concatenation`;
3156+
throw err;
31783157
}
31793158
}
31803159

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import wrapped from "./wrapped";
2+
3+
export default wrapped;

‎test/configCases/module/iife-entry-module-with-others/test.config.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
return [
66
"module-avoidEntryIife-false.mjs",
77
"module-avoidEntryIife-true.mjs",
8+
"module-avoidEntryIife-wrapped.mjs",
89
"test.js"
910
];
1011
}

‎test/configCases/module/iife-entry-module-with-others/test.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ const path = require("path");
44
it("IIFE should present when `avoidEntryIife` is disabled, and avoided when true", () => {
55
const trueSource = fs.readFileSync(path.join(__dirname, "module-avoidEntryIife-true.mjs"), "utf-8");
66
const falseSource = fs.readFileSync(path.join(__dirname, "module-avoidEntryIife-false.mjs"), "utf-8");
7+
const wrappedSource = fs.readFileSync(path.join(__dirname, "module-avoidEntryIife-wrapped.mjs"), "utf-8");
78
expect(trueSource).not.toContain('This entry needs to be wrapped in an IIFE');
9+
expect(wrappedSource).not.toContain('This entry needs to be wrapped in an IIFE');
10+
// a wrapped body's own local is not one of its free names, so the entry's is kept
11+
expect(wrappedSource).toContain('let wrappedLocal = 1');
812
expect(falseSource).toContain('This entry needs to be wrapped in an IIFE because it needs to be isolated against other modules in the chunk.');
913
});

‎test/configCases/module/iife-entry-module-with-others/webpack.config.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ module.exports = [
3737
avoidEntryIife: true
3838
}
3939
},
40+
{
41+
...base,
42+
name: "module-avoidEntryIife-wrapped",
43+
entry: "./wrapped-entry.js",
44+
output: {
45+
...base.output,
46+
filename: "module-avoidEntryIife-wrapped.mjs"
47+
},
48+
optimization: {
49+
...base.optimization,
50+
avoidEntryIife: true
51+
}
52+
},
4053
{
4154
name: "test-output",
4255
entry: "./test.js",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let value = 42;
2+
let wrappedLocal = 1;
3+
4+
it("should keep a wrapped body's global read apart from the entry's declaration", async () => {
5+
const { default: wrappedValue } = await import(
6+
/* webpackMode: "eager" */ "./concatenated"
7+
);
8+
expect(value).toBe(42);
9+
expect(wrappedLocal).toBe(1);
10+
// `typeof value` inside the wrapped body must not see the entry's `value`
11+
expect(wrappedValue).toBe("undefined");
12+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
// a local of the body, not one of its free names
4+
const wrappedLocal = typeof value;
5+
6+
module.exports = wrappedLocal;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { value as value1 } from "./module1";
2+
3+
const value2 = require("./module2");
4+
const value3 = require("./module3");
5+
const value4 = require("./module4");
6+
7+
let value = 42;
8+
let other = 43;
9+
10+
it("keeps the inlined entry's declarations apart from the modules' global reads", () => {
11+
expect(value1).toBe(undefined);
12+
expect(value).toBe(42);
13+
expect(other).toBe(43);
14+
// module2 reads `value` at step 0 and `other` at step 1
15+
expect(value2).toBe("undefined");
16+
// module3 reads `value` throughout; at step 1 only its cached free names say so
17+
expect(value3).toBe("undefined");
18+
expect(value4).toBe("function");
19+
});

0 commit comments

Comments
 (0)