-
-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathEmbedFederationRuntimeModule.ts
More file actions
86 lines (79 loc) · 2.58 KB
/
EmbedFederationRuntimeModule.ts
File metadata and controls
86 lines (79 loc) · 2.58 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
86
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Zackary Jackson @ScriptedAlchemy
*/
import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
import ContainerEntryDependency from '../ContainerEntryDependency';
import type { NormalModule as NormalModuleType } from 'webpack';
import type FederationRuntimeDependency from './FederationRuntimeDependency';
const { RuntimeModule, Template, RuntimeGlobals } = require(
normalizeWebpackPath('webpack'),
) as typeof import('webpack');
class EmbedFederationRuntimeModule extends RuntimeModule {
private containerEntrySet: Set<
ContainerEntryDependency | FederationRuntimeDependency
>;
public override _cachedGeneratedCode: string | undefined;
constructor(
containerEntrySet: Set<
ContainerEntryDependency | FederationRuntimeDependency
>,
) {
super('embed federation', RuntimeModule.STAGE_ATTACH);
this.containerEntrySet = containerEntrySet;
this._cachedGeneratedCode = undefined;
}
override identifier() {
return 'webpack/runtime/embed/federation';
}
override generate(): string | null {
if (this._cachedGeneratedCode !== undefined) {
return this._cachedGeneratedCode;
}
const { compilation, chunk, chunkGraph } = this;
if (!chunk || !chunkGraph || !compilation) {
return null;
}
let found;
if (chunk.name) {
for (const dep of this.containerEntrySet) {
const mod = compilation.moduleGraph.getModule(dep);
if (mod && compilation.chunkGraph.isModuleInChunk(mod, chunk)) {
found = mod as NormalModuleType;
break;
}
}
}
if (!found) {
return null;
}
const initRuntimeModuleGetter = compilation.runtimeTemplate.moduleRaw({
module: found,
chunkGraph,
request: found.request,
weak: false,
runtimeRequirements: new Set(),
});
const result = Template.asString([
`var prevStartup = ${RuntimeGlobals.startup};`,
`var hasRun = false;`,
`${RuntimeGlobals.startup} = ${compilation.runtimeTemplate.basicFunction(
'',
[
`if (!hasRun) {`,
` hasRun = true;`,
` ${initRuntimeModuleGetter};`,
`}`,
`if (typeof prevStartup === 'function') {`,
` return prevStartup();`,
`} else {`,
` console.warn('[Module Federation] prevStartup is not a function, skipping startup execution');`,
`}`,
],
)};`,
]);
this._cachedGeneratedCode = result;
return result;
}
}
export default EmbedFederationRuntimeModule;