Skip to content

Commit 1b6bf0e

Browse files
fix(enhanced): container addInclude wrap in make hook (#3002)
1 parent 39bfbb4 commit 1b6bf0e

File tree

8 files changed

+83
-103
lines changed

8 files changed

+83
-103
lines changed

.changeset/ai-hungry-tiger.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+
ContainerPlugin to use makeHook to addInclude of federation runtime dependency

ai-lint-fix.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,16 @@ async function lintFileContent(fileContent) {
3838
RULES:
3939
-Should preserve uses of normalizeWebpackPath
4040
-Should preserve uses of ts-ignore
41-
-Removed commented out code
41+
-Should improve the source code while ensuing its logic is preserved and functionality is not altered
42+
-Update existing comments for accuracy
4243
-Return only the updated file content with no other response text:
4344
4445
${fileContent}`;
4546

4647
const response = await openai.chat.completions.create({
4748
model: 'gpt-4o',
4849
messages: [{ role: 'user', content: prompt }],
49-
max_tokens: 4096,
50+
max_completion_tokens: 4096,
5051
});
5152

5253
let res = response.choices[0].message.content.trim().split('\n');

packages/enhanced/project.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"parallel": false,
4242
"commands": [
4343
{
44-
"command": "node packages/enhanced/test/script.js",
44+
"command": "node --expose-gc --max-old-space-size=4096 --experimental-vm-modules --trace-deprecation ./node_modules/jest-cli/bin/jest --logHeapUsage --config packages/enhanced/jest.config.ts --silent",
4545
"forwardAllArgs": false
4646
}
4747
]
@@ -53,7 +53,7 @@
5353
"parallel": false,
5454
"commands": [
5555
{
56-
"command": "node packages/enhanced/test/script-experiments.js",
56+
"command": "node --expose-gc --max-old-space-size=4096 --experimental-vm-modules --trace-deprecation ./node_modules/jest-cli/bin/jest --logHeapUsage --config packages/enhanced/jest.embed.ts --silent",
5757
"forwardAllArgs": false
5858
}
5959
]

packages/enhanced/src/lib/container/ContainerPlugin.ts

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,15 @@ class ContainerPlugin {
190190

191191
compiler.hooks.make.tapAsync(
192192
PLUGIN_NAME,
193-
(
193+
async (
194194
compilation: Compilation,
195195
callback: (error?: WebpackError | null | undefined) => void,
196196
) => {
197197
const hasSingleRuntimeChunk =
198198
compilation.options?.optimization?.runtimeChunk;
199199
const hooks = FederationModulesPlugin.getCompilationHooks(compilation);
200+
const federationRuntimeDependency =
201+
federationRuntimePluginInstance.getDependency(compiler);
200202
const dep = new ContainerEntryDependency(
201203
name,
202204
//@ts-ignore
@@ -206,78 +208,96 @@ class ContainerPlugin {
206208
this._options.experiments,
207209
);
208210
dep.loc = { name };
209-
compilation.addEntry(
210-
compilation.options.context || '',
211-
dep,
212-
{
213-
name,
214-
filename,
215-
runtime: hasSingleRuntimeChunk ? false : runtime,
216-
library,
217-
},
218-
(error: WebpackError | null | undefined) => {
219-
if (error) return callback(error);
220-
hooks.addContainerEntryModule.call(dep);
221-
callback();
222-
},
223-
);
211+
212+
await new Promise((resolve, reject) => {
213+
compilation.addEntry(
214+
compilation.options.context || '',
215+
dep,
216+
{
217+
name,
218+
filename,
219+
runtime: hasSingleRuntimeChunk ? false : runtime,
220+
library,
221+
},
222+
(error: WebpackError | null | undefined) => {
223+
if (error) return reject(error);
224+
hooks.addContainerEntryModule.call(dep);
225+
resolve(undefined);
226+
},
227+
);
228+
}).catch(callback);
229+
230+
await new Promise((resolve, reject) => {
231+
compilation.addInclude(
232+
compiler.context,
233+
federationRuntimeDependency,
234+
{ name: undefined },
235+
(err, module) => {
236+
if (err) {
237+
return reject(err);
238+
}
239+
hooks.addFederationRuntimeModule.call(
240+
federationRuntimeDependency,
241+
);
242+
resolve(undefined);
243+
},
244+
);
245+
}).catch(callback);
246+
247+
callback();
224248
},
225249
);
226250

227251
// this will still be copied into child compiler, so it needs a check to avoid running hook on child
228252
// we have to use finishMake in order to check the entries created and see if there are multiple runtime chunks
229253
compiler.hooks.finishMake.tapAsync(
230254
PLUGIN_NAME,
231-
async (compilation, callback) => {
232-
// its a child compiler
255+
(compilation: Compilation, callback) => {
233256
if (
234257
compilation.compiler.parentCompilation &&
235258
compilation.compiler.parentCompilation !== compilation
236259
) {
237-
// dont include dependencies on child compilations
238260
return callback();
239261
}
240262

241263
const hooks = FederationModulesPlugin.getCompilationHooks(compilation);
242-
const createdRuntimes = new Set();
264+
const createdRuntimes = new Set<string>();
265+
243266
for (const entry of compilation.entries.values()) {
244-
if (entry.options.runtime) {
245-
if (createdRuntimes.has(entry.options.runtime)) {
246-
continue;
247-
}
248-
createdRuntimes.add(entry.options.runtime);
267+
const runtime = entry.options.runtime;
268+
if (runtime) {
269+
createdRuntimes.add(runtime);
249270
}
250271
}
251272

252-
// if it has multiple runtime chunks - make another with no name or runtime assigned
253273
if (
254-
createdRuntimes.size !== 0 ||
255-
compilation.options?.optimization?.runtimeChunk
274+
createdRuntimes.size === 0 &&
275+
!compilation.options?.optimization?.runtimeChunk
256276
) {
257-
const dep = new ContainerEntryDependency(
258-
name,
259-
//@ts-ignore
260-
exposes,
261-
shareScope,
262-
federationRuntimePluginInstance.entryFilePath,
263-
this._options.experiments,
264-
);
277+
return callback();
278+
}
279+
280+
const dep = new ContainerEntryDependency(
281+
name,
282+
//@ts-ignore
283+
exposes,
284+
shareScope,
285+
federationRuntimePluginInstance.entryFilePath,
286+
this._options.experiments,
287+
);
265288

266-
dep.loc = { name };
289+
dep.loc = { name };
267290

268-
compilation.addInclude(
269-
compilation.options.context || '',
270-
dep,
271-
{ name: undefined },
272-
(error: WebpackError | null | undefined) => {
273-
if (error) return callback(error);
274-
hooks.addContainerEntryModule.call(dep);
275-
callback();
276-
},
277-
);
278-
} else {
279-
callback();
280-
}
291+
compilation.addInclude(
292+
compilation.options.context || '',
293+
dep,
294+
{ name: undefined },
295+
(error: WebpackError | null | undefined) => {
296+
if (error) return callback(error);
297+
hooks.addContainerEntryModule.call(dep);
298+
callback();
299+
},
300+
);
281301
},
282302
);
283303

@@ -301,11 +321,6 @@ class ContainerPlugin {
301321
compiler.hooks.thisCompilation.tap(
302322
PLUGIN_NAME,
303323
(compilation: Compilation, { normalModuleFactory }) => {
304-
const federationRuntimeDependency =
305-
federationRuntimePluginInstance.getDependency(compiler);
306-
307-
const logger = compilation.getLogger('ContainerPlugin');
308-
const hooks = FederationModulesPlugin.getCompilationHooks(compilation);
309324
compilation.dependencyFactories.set(
310325
FederationRuntimeDependency,
311326
normalModuleFactory,
@@ -314,21 +329,6 @@ class ContainerPlugin {
314329
FederationRuntimeDependency,
315330
new ModuleDependency.Template(),
316331
);
317-
318-
compilation.addInclude(
319-
compiler.context,
320-
federationRuntimeDependency,
321-
{ name: undefined },
322-
(err, module) => {
323-
if (err) {
324-
return logger.error(
325-
'Error adding federation runtime module:',
326-
err,
327-
);
328-
}
329-
hooks.addFederationRuntimeModule.call(federationRuntimeDependency);
330-
},
331-
);
332332
},
333333
);
334334
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ class FederationRuntimePlugin {
218218
getDependency(compiler: Compiler) {
219219
if (this.federationRuntimeDependency)
220220
return this.federationRuntimeDependency;
221+
222+
this.ensureFile(compiler);
223+
221224
this.federationRuntimeDependency = new FederationRuntimeDependency(
222225
this.getFilePath(compiler),
223226
);

packages/enhanced/test/ConfigTestCases.template.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const categories = fs.readdirSync(casesPath).map((cat) => {
2828
.sort(),
2929
};
3030
});
31-
console.log(333, categories);
3231
// .filter((i) => i.name === 'container');
3332
const createLogger = (appendTarget) => {
3433
return {

packages/enhanced/test/script-experiments.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

packages/enhanced/test/script.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)