Skip to content

Commit 9062cee

Browse files
authored
fix(enhanced): reuse cached runtime entry (#3522)
1 parent 1ef86c2 commit 9062cee

File tree

2 files changed

+36
-34
lines changed

2 files changed

+36
-34
lines changed

.changeset/big-pianos-teach.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/enhanced': patch
3+
---
4+
5+
fix(enhanced): reuse cached runtime entry

packages/enhanced/src/lib/container/runtime/FederationRuntimePlugin.ts

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ const EmbeddedRuntimePath = require.resolve(
5656

5757
const federationGlobal = getFederationGlobalScope(RuntimeGlobals);
5858

59-
const onceForCompler = new WeakSet<Compiler>();
59+
const onceForCompiler = new WeakSet<Compiler>();
60+
const onceForCompilerEntryMap = new WeakMap<Compiler, string>();
6061

6162
class FederationRuntimePlugin {
6263
options?: moduleFederationPlugin.ModuleFederationPluginOptions;
@@ -150,41 +151,31 @@ class FederationRuntimePlugin {
150151
]);
151152
}
152153

153-
static getFilePath(
154-
compiler: Compiler,
155-
options: moduleFederationPlugin.ModuleFederationPluginOptions,
156-
bundlerRuntimePath?: string,
157-
experiments?: moduleFederationPlugin.ModuleFederationPluginOptions['experiments'],
158-
) {
159-
const containerName = options.name;
160-
const hash = createHash(
161-
`${containerName} ${FederationRuntimePlugin.getTemplate(
162-
compiler,
163-
options,
164-
bundlerRuntimePath,
165-
experiments,
166-
)}`,
167-
);
168-
return path.join(TEMP_DIR, `entry.${hash}.js`);
169-
}
170154
getFilePath(compiler: Compiler) {
171-
if (this.entryFilePath) {
172-
return this.entryFilePath;
173-
}
174-
175155
if (!this.options) {
176156
return '';
177157
}
178158

159+
const existedFilePath = onceForCompilerEntryMap.get(compiler);
160+
161+
if (existedFilePath) {
162+
return existedFilePath;
163+
}
164+
165+
let entryFilePath = '';
179166
if (!this.options?.virtualRuntimeEntry) {
180-
this.entryFilePath = FederationRuntimePlugin.getFilePath(
181-
compiler,
182-
this.options,
183-
this.bundlerRuntimePath,
184-
this.options.experiments,
167+
const containerName = this.options.name;
168+
const hash = createHash(
169+
`${containerName} ${FederationRuntimePlugin.getTemplate(
170+
compiler,
171+
this.options,
172+
this.bundlerRuntimePath,
173+
this.options.experiments,
174+
)}`,
185175
);
176+
entryFilePath = path.join(TEMP_DIR, `entry.${hash}.js`);
186177
} else {
187-
this.entryFilePath = `data:text/javascript;charset=utf-8;base64,${pBtoa(
178+
entryFilePath = `data:text/javascript;charset=utf-8;base64,${pBtoa(
188179
FederationRuntimePlugin.getTemplate(
189180
compiler,
190181
this.options,
@@ -193,8 +184,12 @@ class FederationRuntimePlugin {
193184
),
194185
)}`;
195186
}
196-
return this.entryFilePath;
187+
188+
onceForCompilerEntryMap.set(compiler, entryFilePath);
189+
190+
return entryFilePath;
197191
}
192+
198193
ensureFile(compiler: Compiler) {
199194
if (!this.options) {
200195
return;
@@ -203,7 +198,7 @@ class FederationRuntimePlugin {
203198
if (this.options?.virtualRuntimeEntry) {
204199
return;
205200
}
206-
const filePath = this.getFilePath(compiler);
201+
const filePath = this.entryFilePath;
207202
try {
208203
fs.readFileSync(filePath);
209204
} catch (err) {
@@ -227,7 +222,7 @@ class FederationRuntimePlugin {
227222
this.ensureFile(compiler);
228223

229224
this.federationRuntimeDependency = new FederationRuntimeDependency(
230-
this.getFilePath(compiler),
225+
this.entryFilePath,
231226
);
232227
return this.federationRuntimeDependency;
233228
}
@@ -275,7 +270,7 @@ class FederationRuntimePlugin {
275270
},
276271
);
277272
} else {
278-
const entryFilePath = this.getFilePath(compiler);
273+
const entryFilePath = this.entryFilePath;
279274
modifyEntry({
280275
compiler,
281276
prependEntry: (entry: Record<string, EntryDescription>) => {
@@ -432,6 +427,8 @@ class FederationRuntimePlugin {
432427
);
433428
}
434429

430+
this.entryFilePath = this.getFilePath(compiler);
431+
435432
if (this.options?.experiments?.federationRuntime === 'hoisted') {
436433
new EmbedFederationRuntimePlugin().apply(compiler);
437434

@@ -451,11 +448,11 @@ class FederationRuntimePlugin {
451448
).apply(compiler);
452449
}
453450
// dont run multiple times on every apply()
454-
if (!onceForCompler.has(compiler)) {
451+
if (!onceForCompiler.has(compiler)) {
455452
this.prependEntry(compiler);
456453
this.injectRuntime(compiler);
457454
this.setRuntimeAlias(compiler);
458-
onceForCompler.add(compiler);
455+
onceForCompiler.add(compiler);
459456
}
460457
}
461458
}

0 commit comments

Comments
 (0)