-
-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathRemoteRuntimeModule.ts
More file actions
167 lines (154 loc) · 5.99 KB
/
RemoteRuntimeModule.ts
File metadata and controls
167 lines (154 loc) · 5.99 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra, Zackary Jackson @ScriptedAlchemy
*/
import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
import type { Compilation } from 'webpack';
import RemoteModule from './RemoteModule';
import { getFederationGlobalScope } from './runtime/utils';
import type ExternalModule from 'webpack/lib/ExternalModule';
import type FallbackModule from './FallbackModule';
import type {
ModuleIdToRemoteDataMapping,
RemotesOptions,
} from '@module-federation/webpack-bundler-runtime';
const extractUrlAndGlobal = require(
normalizeWebpackPath('webpack/lib/util/extractUrlAndGlobal'),
) as typeof import('webpack/lib/util/extractUrlAndGlobal');
const { Template, RuntimeModule, RuntimeGlobals } = require(
normalizeWebpackPath('webpack'),
) as typeof import('webpack');
class RemoteRuntimeModule extends RuntimeModule {
constructor() {
super('remotes loading');
}
/**
* @returns {string | null} runtime code
*/
override generate(): string | null {
const { compilation, chunkGraph } = this;
const { runtimeTemplate, moduleGraph } = compilation as Compilation;
const chunkToRemotesMapping: Record<string, any> = {};
const idToExternalAndNameMapping: Record<string | number, any> = {};
const idToRemoteMap: RemotesOptions['idToRemoteMap'] = {};
const moduleIdToRemoteDataMapping: ModuleIdToRemoteDataMapping = {};
// let chunkReferences: Set<Chunk> = new Set();
// if (this.chunk && chunkGraph) {
// const requirements = chunkGraph.getTreeRuntimeRequirements(this.chunk);
// if (requirements.has('federation-entry-startup')) {
// chunkReferences = this.chunk.getAllReferencedChunks();
// } else {
// // remote entry doesnt need federation startup, can have async chunk map only
// chunkReferences = this.chunk.getAllAsyncChunks();
// }
// }
const allChunks = [
...Array.from(this.chunk?.getAllReferencedChunks() || []),
];
for (const chunk of allChunks) {
const modules = chunkGraph?.getChunkModulesIterableBySourceType(
chunk,
'remote',
);
if (!modules) {
continue;
}
// @ts-ignore
const remotes = (chunkToRemotesMapping[chunk.id] = []);
for (const m of modules) {
const module: RemoteModule = m as unknown as RemoteModule;
const name = module.internalRequest;
// @ts-ignore
const id = chunkGraph ? chunkGraph.getModuleId(module) : undefined;
const { shareScope } = module;
const dep = module.dependencies[0];
// @ts-ignore
const externalModule = moduleGraph.getModule(dep) as
| ExternalModule
| FallbackModule;
const externalModuleId =
chunkGraph && externalModule
? // @ts-ignore
chunkGraph.getModuleId(externalModule)
: undefined;
if (id !== undefined) {
//@ts-ignore
remotes.push(id);
idToExternalAndNameMapping[id] = [shareScope, name, externalModuleId];
const remoteModules: ExternalModule[] = [];
// FallbackModule has requests
if ('requests' in externalModule && externalModule.requests) {
externalModule.dependencies.forEach((dependency) => {
const remoteModule = moduleGraph.getModule(dependency);
if (remoteModule) {
// @ts-ignore
remoteModules.push(remoteModule as ExternalModule);
}
});
} else {
remoteModules.push(externalModule as ExternalModule);
}
idToRemoteMap[id] = [];
remoteModules.forEach((remoteModule) => {
let remoteName = '';
try {
const [_url, name] = extractUrlAndGlobal(
remoteModule.request as string,
);
remoteName = name;
} catch (err) {
//noop
}
const externalModuleId =
chunkGraph &&
remoteModule &&
// @ts-ignore
chunkGraph.getModuleId(remoteModule);
idToRemoteMap[id].push({
externalType: remoteModule.externalType,
name: remoteModule.externalType === 'script' ? remoteName : '',
});
moduleIdToRemoteDataMapping[id] = {
shareScope: shareScope as string,
name,
externalModuleId: externalModuleId as string,
// Preserve the extracted remote name so lazy updates can
// rebuild idToRemoteMap via updateRemoteOptions.
remoteName: remoteName,
};
});
}
}
}
const federationGlobal = getFederationGlobalScope(
RuntimeGlobals || ({} as typeof RuntimeGlobals),
);
return Template.asString([
`var chunkMapping = ${JSON.stringify(
chunkToRemotesMapping,
null,
'\t',
)};`,
`var idToExternalAndNameMapping = ${JSON.stringify(
idToExternalAndNameMapping,
null,
'\t',
)};`,
`var idToRemoteMap = ${JSON.stringify(idToRemoteMap, null, '\t')};`,
`${federationGlobal}.bundlerRuntimeOptions.remotes.chunkMapping = chunkMapping;`,
`${federationGlobal}.bundlerRuntimeOptions.remotes.idToExternalAndNameMapping = idToExternalAndNameMapping;`,
`${federationGlobal}.bundlerRuntimeOptions.remotes.idToRemoteMap = idToRemoteMap;`,
`${RuntimeGlobals.require}.remotesLoadingData.moduleIdToRemoteDataMapping = ${JSON.stringify(
moduleIdToRemoteDataMapping,
null,
'\t',
)};`,
`${
RuntimeGlobals.ensureChunkHandlers
}.remotes = ${runtimeTemplate.basicFunction('chunkId, promises', [
`${federationGlobal}.bundlerRuntime.remotes({idToRemoteMap,chunkMapping, idToExternalAndNameMapping, chunkId, promises, webpackRequire:${RuntimeGlobals.require}});`,
])}`,
]);
}
}
export default RemoteRuntimeModule;