Skip to content

Commit cf62587

Browse files
refactor(enhanced): export getAllReferencedModules as a util (#2937)
1 parent 760cc35 commit cf62587

File tree

4 files changed

+43
-38
lines changed

4 files changed

+43
-38
lines changed

.github/workflows/devtools.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
run: sudo apt-get update && sudo apt-get install xvfb
4343

4444
- name: E2E Chrome Devtools
45-
run: pnpm run app:manifest:dev & echo "done" && sleep 50 && npx nx e2e:devtools chrome-devtools
45+
run: pnpm run app:manifest:dev & echo "done" && npx wait-on tcp:3009 tcp:3010 tcp:3011 tcp:3012 && sleep 10 && npx nx e2e:devtools chrome-devtools
4646

4747
- name: kill port
4848
run: lsof -ti tcp:3008,3009,3010,3011,3012 | xargs kill

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"app:node:dev": "nx run-many --target=serve --parallel=10 --configuration=development -p node-host,node-local-remote,node-remote,node-dynamic-remote-new-version,node-dynamic-remote",
4040
"app:runtime:dev": "nx run-many --target=serve -p 3005-runtime-host,3006-runtime-remote,3007-runtime-remote",
4141
"app:router:dev": "nx run-many --target=serve --parallel=10 --projects='router-*'",
42-
"app:manifest:dev": "kill-port 3009 3010 3011 3012 3013 && nx run-many --target=serve --parallel=100 -p manifest-webpack-host,3009-webpack-provider,3010-rspack-provider,3011-rspack-manifest-provider,3012-rspack-js-entry-provider",
42+
"app:manifest:dev": "nx run-many --target=serve --parallel=100 -p manifest-webpack-host,3009-webpack-provider,3010-rspack-provider,3011-rspack-manifest-provider,3012-rspack-js-entry-provider",
4343
"app:ts:dev": "nx run-many --target=serve -p react_ts_host,react_ts_nested_remote,react_ts_remote",
4444
"app:modern:dev": "nx run-many --target=serve --parallel=10 --configuration=development -p modernjs-ssr-dynamic-nested-remote,modernjs-ssr-dynamic-remote,modernjs-ssr-dynamic-remote-new-version,modernjs-ssr-host,modernjs-ssr-nested-remote,modernjs-ssr-remote,modernjs-ssr-remote-new-version",
4545
"commitlint": "commitlint --edit",

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

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-p
1010
import type { RuntimeSpec } from 'webpack/lib/util/runtime';
1111
import type ExportsInfo from 'webpack/lib/ExportsInfo';
1212
import ContainerEntryModule from './ContainerEntryModule';
13+
import { moduleFederationPlugin } from '@module-federation/sdk';
1314

1415
const { NormalModule, AsyncDependenciesBlock } = require(
1516
normalizeWebpackPath('webpack'),
@@ -29,15 +30,18 @@ export class HoistContainerReferences implements WebpackPluginInstance {
2930
private readonly entryFilePath?: string;
3031
private readonly bundlerRuntimeDep?: string;
3132
private readonly explanation: string;
33+
private readonly experiments: moduleFederationPlugin.ModuleFederationPluginOptions['experiments'];
3234

3335
constructor(
3436
name?: string,
3537
entryFilePath?: string,
3638
bundlerRuntimeDep?: string,
39+
experiments?: moduleFederationPlugin.ModuleFederationPluginOptions['experiments'],
3740
) {
3841
this.containerName = name || 'no known chunk name';
3942
this.entryFilePath = entryFilePath;
4043
this.bundlerRuntimeDep = bundlerRuntimeDep;
44+
this.experiments = experiments;
4145
this.explanation =
4246
'Bundler runtime path module is required for proper functioning';
4347
}
@@ -92,7 +96,7 @@ export class HoistContainerReferences implements WebpackPluginInstance {
9296
module instanceof NormalModule &&
9397
module.resource === this.bundlerRuntimeDep
9498
) {
95-
const allRefs = this.getAllReferencedModules(
99+
const allRefs = getAllReferencedModules(
96100
compilation,
97101
module,
98102
'initial',
@@ -119,40 +123,6 @@ export class HoistContainerReferences implements WebpackPluginInstance {
119123
);
120124
}
121125

122-
// Helper method to collect all referenced modules recursively
123-
private getAllReferencedModules(
124-
compilation: Compilation,
125-
module: Module,
126-
type?: 'all' | 'initial',
127-
): Set<Module> {
128-
const collectedModules = new Set<Module>([module]);
129-
const stack = [module];
130-
131-
while (stack.length > 0) {
132-
const currentModule = stack.pop();
133-
if (!currentModule) continue;
134-
const mgm = compilation.moduleGraph._getModuleGraphModule(currentModule);
135-
if (mgm && mgm.outgoingConnections) {
136-
for (const connection of mgm.outgoingConnections) {
137-
if (type === 'initial') {
138-
const parentBlock = compilation.moduleGraph.getParentBlock(
139-
connection.dependency,
140-
);
141-
if (parentBlock instanceof AsyncDependenciesBlock) {
142-
continue;
143-
}
144-
}
145-
if (connection.module && !collectedModules.has(connection.module)) {
146-
collectedModules.add(connection.module);
147-
stack.push(connection.module);
148-
}
149-
}
150-
}
151-
}
152-
153-
return collectedModules;
154-
}
155-
156126
// Helper method to find a specific module in a chunk
157127
private findModule(
158128
compilation: Compilation,
@@ -226,7 +196,7 @@ export class HoistContainerReferences implements WebpackPluginInstance {
226196
return;
227197
}
228198

229-
const allReferencedModules = this.getAllReferencedModules(
199+
const allReferencedModules = getAllReferencedModules(
230200
compilation,
231201
runtimeModule,
232202
'initial',
@@ -291,4 +261,38 @@ export class HoistContainerReferences implements WebpackPluginInstance {
291261
}
292262
}
293263

264+
// Helper method to collect all referenced modules recursively
265+
export function getAllReferencedModules(
266+
compilation: Compilation,
267+
module: Module,
268+
type?: 'all' | 'initial',
269+
): Set<Module> {
270+
const collectedModules = new Set<Module>([module]);
271+
const stack = [module];
272+
273+
while (stack.length > 0) {
274+
const currentModule = stack.pop();
275+
if (!currentModule) continue;
276+
const mgm = compilation.moduleGraph._getModuleGraphModule(currentModule);
277+
if (mgm && mgm.outgoingConnections) {
278+
for (const connection of mgm.outgoingConnections) {
279+
if (type === 'initial') {
280+
const parentBlock = compilation.moduleGraph.getParentBlock(
281+
connection.dependency,
282+
);
283+
if (parentBlock instanceof AsyncDependenciesBlock) {
284+
continue;
285+
}
286+
}
287+
if (connection.module && !collectedModules.has(connection.module)) {
288+
collectedModules.add(connection.module);
289+
stack.push(connection.module);
290+
}
291+
}
292+
}
293+
}
294+
295+
return collectedModules;
296+
}
297+
294298
export default HoistContainerReferences;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ class FederationRuntimePlugin {
382382
// hoist all modules of federation entry
383383
this.getFilePath(),
384384
this.bundlerRuntimePath,
385+
this.options.experiments,
385386
).apply(compiler);
386387

387388
new compiler.webpack.NormalModuleReplacementPlugin(

0 commit comments

Comments
 (0)