Skip to content

Commit 56eb56a

Browse files
feat(enhanced): chunk matcher to federation runtime (#2349)
Co-authored-by: ScriptedAlchemy <[email protected]>
1 parent bc7918a commit 56eb56a

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

.changeset/green-news-leave.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+
add chunk matcher logic to federation runtime module

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
2+
const compileBooleanMatcher = require(
3+
normalizeWebpackPath('webpack/lib/util/compileBooleanMatcher'),
4+
) as typeof import('webpack/lib/util/compileBooleanMatcher');
5+
const { getUndoPath } = require(
6+
normalizeWebpackPath('webpack/lib/util/identifier'),
7+
) as typeof import('webpack/lib/util/identifier');
28
// inspired by react-refresh-webpack-plugin
39
import getFederationGlobal from './getFederationGlobal';
410
import { NormalizedRuntimeInitOptionsWithOutShared } from '../../../types/runtime';
@@ -27,10 +33,50 @@ class FederationRuntimeModule extends RuntimeModule {
2733
* @returns {string | null} runtime code
2834
*/
2935
override generate() {
36+
let matcher: string | boolean = false;
37+
let rootOutputDir: string | undefined;
38+
if (this.compilation && this.chunk) {
39+
const jsModulePlugin =
40+
this.compilation.compiler.webpack?.javascript
41+
?.JavascriptModulesPlugin ||
42+
require(
43+
normalizeWebpackPath(
44+
'webpack/lib/javascript/JavascriptModulesPlugin',
45+
),
46+
);
47+
const { chunkHasJs } = jsModulePlugin;
48+
if (this.runtimeRequirements.has(RuntimeGlobals.ensureChunkHandlers)) {
49+
const conditionMap = this.compilation.chunkGraph.getChunkConditionMap(
50+
this.chunk,
51+
chunkHasJs,
52+
);
53+
const hasJsMatcher = compileBooleanMatcher(conditionMap);
54+
if (typeof hasJsMatcher === 'boolean') {
55+
matcher = hasJsMatcher;
56+
} else {
57+
matcher = hasJsMatcher('chunkId');
58+
}
59+
const outputName = this.compilation.getPath(
60+
jsModulePlugin.getChunkFilenameTemplate(
61+
this.chunk,
62+
this.compilation.outputOptions,
63+
),
64+
{ chunk: this.chunk, contentHashType: 'javascript' },
65+
);
66+
rootOutputDir = getUndoPath(
67+
outputName,
68+
this.compilation.outputOptions.path || '',
69+
false,
70+
);
71+
}
72+
}
73+
3074
return Template.asString([
3175
getFederationGlobal(
3276
Template,
3377
RuntimeGlobals,
78+
matcher,
79+
rootOutputDir,
3480
this.initOptionsWithoutShared,
3581
),
3682
]);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const { Template } = require(
1010
function getFederationGlobal(
1111
template: typeof Template,
1212
runtimeGlobals: typeof RuntimeGlobals,
13+
matcher: string | boolean,
14+
rootOutputDir: string | undefined,
1315
initOptionsWithoutShared: NormalizedRuntimeInitOptionsWithOutShared,
1416
): string {
1517
const federationGlobal = getFederationGlobalScope(runtimeGlobals);
@@ -21,6 +23,8 @@ function getFederationGlobal(
2123
`${federationGlobal} = {`,
2224
template.indent([
2325
`initOptions: ${initOptionsStrWithoutShared},`,
26+
`chunkMatcher: function(chunkId) {return ${matcher}},`,
27+
`rootOutputDir: ${JSON.stringify(rootOutputDir || '')},`,
2428
`initialConsumes: undefined,`,
2529
'bundlerRuntimeOptions: {}',
2630
]),

0 commit comments

Comments
 (0)