-
-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathFederationRuntimeModule.ts
More file actions
85 lines (80 loc) · 2.82 KB
/
FederationRuntimeModule.ts
File metadata and controls
85 lines (80 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
const compileBooleanMatcher = require(
normalizeWebpackPath('webpack/lib/util/compileBooleanMatcher'),
) as typeof import('webpack/lib/util/compileBooleanMatcher');
const { getUndoPath } = require(
normalizeWebpackPath('webpack/lib/util/identifier'),
) as typeof import('webpack/lib/util/identifier');
// inspired by react-refresh-webpack-plugin
import getFederationGlobal from './getFederationGlobal';
import { NormalizedRuntimeInitOptionsWithOutShared } from '../../../types/runtime';
const { RuntimeModule, RuntimeGlobals, Template } = require(
normalizeWebpackPath('webpack'),
) as typeof import('webpack');
class FederationRuntimeModule extends RuntimeModule {
runtimeRequirements: ReadonlySet<string>;
containerName: string;
initOptionsWithoutShared: NormalizedRuntimeInitOptionsWithOutShared;
constructor(
runtimeRequirements: ReadonlySet<string>,
containerName: string,
initOptionsWithoutShared: NormalizedRuntimeInitOptionsWithOutShared,
) {
super('federation runtime', RuntimeModule.STAGE_NORMAL - 1);
this.runtimeRequirements = runtimeRequirements;
this.containerName = containerName;
this.initOptionsWithoutShared = initOptionsWithoutShared;
}
/**
* @returns {string | null} runtime code
*/
override generate() {
let matcher: string | boolean = false;
let rootOutputDir: string | undefined;
if (this.compilation && this.chunk) {
const jsModulePlugin =
this.compilation.compiler.webpack?.javascript
?.JavascriptModulesPlugin ||
require(
normalizeWebpackPath(
'webpack/lib/javascript/JavascriptModulesPlugin',
),
);
const { chunkHasJs } = jsModulePlugin;
if (this.runtimeRequirements.has(RuntimeGlobals.ensureChunkHandlers)) {
const conditionMap = this.compilation.chunkGraph.getChunkConditionMap(
this.chunk,
chunkHasJs,
);
const hasJsMatcher = compileBooleanMatcher(conditionMap);
if (typeof hasJsMatcher === 'boolean') {
matcher = hasJsMatcher;
} else {
matcher = hasJsMatcher('chunkId');
}
const outputName = this.compilation.getPath(
jsModulePlugin.getChunkFilenameTemplate(
this.chunk,
this.compilation.outputOptions,
),
{ chunk: this.chunk, contentHashType: 'javascript' },
);
rootOutputDir = getUndoPath(
outputName,
this.compilation.outputOptions.path || '',
false,
);
}
}
return Template.asString([
getFederationGlobal(
Template,
RuntimeGlobals,
matcher,
rootOutputDir,
this.initOptionsWithoutShared,
),
]);
}
}
export default FederationRuntimeModule;