@module-federation/runtime 0.1.4 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -784
- package/dist/helpers.esm.js +1 -1
- package/dist/index.cjs.js +76 -36
- package/dist/index.esm.js +78 -38
- package/dist/package.json +1 -1
- package/dist/share.cjs.js +45 -10
- package/dist/share.esm.js +44 -11
- package/dist/src/core.d.ts +8 -2
- package/dist/src/type/config.d.ts +8 -4
- package/dist/src/utils/plugin.d.ts +1 -1
- package/dist/src/utils/share.d.ts +12 -6
- package/dist/src/utils/tool.d.ts +1 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,792 +1,13 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @module-federation/runtime
|
|
2
2
|
|
|
3
3
|
- Can be combined with the build plug-in to share basic dependencies according to policies to reduce the number of module downloads and improve the loading speed of modules.
|
|
4
4
|
- Only consume part of the export of the remote module and will not fully download the remote module
|
|
5
5
|
- The runtime calling process can be extended through the module-runtime plug-in mechanism
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## Documentation
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
// Can load modules using only the runtime SDK without relying on build plugins
|
|
11
|
-
// When not using build plugins, shared dependencies cannot be automatically reused
|
|
12
|
-
import { init, loadRemote } from '@module-federation/runtime';
|
|
9
|
+
See [https://module-federation.io/guide/basic/runtime.html](https://module-federation.io/guide/basic/runtime.html) for details.
|
|
13
10
|
|
|
14
|
-
|
|
15
|
-
name: '@demo/app-main',
|
|
16
|
-
remotes: [
|
|
17
|
-
{
|
|
18
|
-
name: "@demo/app2",
|
|
19
|
-
entry: "http://localhost:3006/remoteEntry.js",
|
|
20
|
-
alias: "app2"
|
|
21
|
-
},
|
|
22
|
-
],
|
|
23
|
-
});
|
|
11
|
+
## License
|
|
24
12
|
|
|
25
|
-
|
|
26
|
-
loadRemote<{add: (...args: Array<number>)=> number }>("app2/util").then((md)=>{
|
|
27
|
-
md.add(1,2,3);
|
|
28
|
-
});
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
### init
|
|
32
|
-
|
|
33
|
-
- Type: `init(options: InitOptions): void`
|
|
34
|
-
- Create runtime instance , it can dynamically register remotes by re-call , but only one instance will exist.
|
|
35
|
-
- InitOptions:
|
|
36
|
-
|
|
37
|
-
```ts
|
|
38
|
-
type InitOptions {
|
|
39
|
-
//The name of the current host
|
|
40
|
-
name: string;
|
|
41
|
-
// The version of the current host will use the host version of the corresponding version online.
|
|
42
|
-
// remoteInfo in remotes will use the online version
|
|
43
|
-
version?: string;
|
|
44
|
-
// In the area where the module is deployed, the consumer will transparently transmit this data to the producer
|
|
45
|
-
//After setting, when the producer data fails to be obtained normally, the backup data will be automatically obtained according to this configuration.
|
|
46
|
-
region?: `EnhancedRegion`;
|
|
47
|
-
// List of dependent remote modules
|
|
48
|
-
// tip: The remotes configured at runtime are not completely consistent with the types and data passed in the build plugin, and the passed in * ^ ~ version rules are not supported at runtime.
|
|
49
|
-
remotes: Array<RemoteInfo>;
|
|
50
|
-
// List of dependencies that need to be shared by the current host
|
|
51
|
-
// When using the build plugin, users can configure the dependencies that need to be shared in the build plugin, and the build plugin will inject the dependencies that need to be shared into the runtime shared configuration.
|
|
52
|
-
// Shared must be manually passed in the version instance reference when it is passed in at runtime, because it cannot be directly passed in at runtime.
|
|
53
|
-
shared?: ShareInfos;
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
type RemoteInfo = (RemotesWithEntry | RemotesWithVersion) & {
|
|
57
|
-
alias?: string;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
interface RemotesWithVersion {
|
|
61
|
-
name: string;
|
|
62
|
-
version: string;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
interface RemotesWithEntry {
|
|
66
|
-
name: string;
|
|
67
|
-
entry: string;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
type ShareInfos = {
|
|
71
|
-
// Dependent package name, dependent basic information, and sharing strategy
|
|
72
|
-
[pkgName: string]: Share;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
type Share = {
|
|
76
|
-
// Versions of shared dependencies
|
|
77
|
-
version: string;
|
|
78
|
-
//Which modules consume the current shared dependencies?
|
|
79
|
-
useIn?: Array<string>;
|
|
80
|
-
//Which module does the shared dependency come from?
|
|
81
|
-
from?: string;
|
|
82
|
-
// Get the factory function of the shared dependency instance. When the cached shared instance cannot be loaded, its own shared dependency will be loaded.
|
|
83
|
-
lib: () => Module;
|
|
84
|
-
// Sharing strategy, what strategy will be used to determine the reuse of shared dependencies
|
|
85
|
-
shareConfig?: SharedConfig;
|
|
86
|
-
// Dependencies between shares
|
|
87
|
-
deps?: Array<string>;
|
|
88
|
-
// Under what scope the current shared dependencies are placed, the default is default
|
|
89
|
-
scope?: string | Array<string>;
|
|
90
|
-
};
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
### loadRemote
|
|
94
|
-
|
|
95
|
-
- Type: `loadRemote(id: string)`
|
|
96
|
-
- Used to load initialized remote modules, when used with build plugins, it can be loaded directly through native `import("remoteName/expose")` syntax, and the build plugin will automatically convert it into `loadRemote` usage.
|
|
97
|
-
|
|
98
|
-
- Example
|
|
99
|
-
|
|
100
|
-
```javascript
|
|
101
|
-
import { init, loadRemote } from '@module-federation/runtime';
|
|
102
|
-
|
|
103
|
-
init({
|
|
104
|
-
name: '@demo/main-app',
|
|
105
|
-
remotes: [
|
|
106
|
-
{
|
|
107
|
-
name: '@demo/app2',
|
|
108
|
-
alias: 'app2',
|
|
109
|
-
entry: 'http://localhost:3006/remoteEntry.js',
|
|
110
|
-
},
|
|
111
|
-
],
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
// remoteName + expose
|
|
115
|
-
loadRemote('@demo/app2/util').then((m) => m.add(1, 2, 3));
|
|
116
|
-
|
|
117
|
-
// alias + expose
|
|
118
|
-
loadRemote('app2/util').then((m) => m.add(1, 2, 3));
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
### loadShare
|
|
122
|
-
|
|
123
|
-
- Type: `loadShare(pkgName: string)`
|
|
124
|
-
- Gets the `share` dependency. When there are `share` dependencies that match the current `host` in the global environment, the existing and satisfying `share` dependencies will be reused first. Otherwise, load its own dependencies and store them in the global cache.
|
|
125
|
-
- This `API` is generally not called directly by users, but is used by build plugins to convert their own dependencies.
|
|
126
|
-
|
|
127
|
-
- Example
|
|
128
|
-
|
|
129
|
-
```js
|
|
130
|
-
import { init, loadRemote, loadShare } from '@module-federation/runtime';
|
|
131
|
-
import React from 'react';
|
|
132
|
-
import ReactDOM from 'react-dom';
|
|
133
|
-
|
|
134
|
-
init({
|
|
135
|
-
name: '@demo/main-app',
|
|
136
|
-
remotes: [],
|
|
137
|
-
shared: {
|
|
138
|
-
react: {
|
|
139
|
-
version: '17.0.0',
|
|
140
|
-
scope: 'default',
|
|
141
|
-
lib: () => React,
|
|
142
|
-
shareConfig: {
|
|
143
|
-
singleton: true,
|
|
144
|
-
requiredVersion: '^17.0.0',
|
|
145
|
-
},
|
|
146
|
-
},
|
|
147
|
-
'react-dom': {
|
|
148
|
-
version: '17.0.0',
|
|
149
|
-
scope: 'default',
|
|
150
|
-
lib: () => ReactDOM,
|
|
151
|
-
shareConfig: {
|
|
152
|
-
singleton: true,
|
|
153
|
-
requiredVersion: '^17.0.0',
|
|
154
|
-
},
|
|
155
|
-
},
|
|
156
|
-
},
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
loadShare('react').then((reactFactory) => {
|
|
160
|
-
console.log(reactFactory());
|
|
161
|
-
});
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
### preloadRemote
|
|
165
|
-
|
|
166
|
-
- Type: `preloadRemote(preloadOptions: Array<PreloadRemoteArgs>)`
|
|
167
|
-
- Used to preload remote assets like remoteEntry.js .
|
|
168
|
-
|
|
169
|
-
- Type
|
|
170
|
-
|
|
171
|
-
```typescript
|
|
172
|
-
async function preloadRemote(preloadOptions: Array<PreloadRemoteArgs>) {}
|
|
173
|
-
|
|
174
|
-
type depsPreloadArg = Omit<PreloadRemoteArgs, 'depsRemote'>;
|
|
175
|
-
type PreloadRemoteArgs = {
|
|
176
|
-
// Preload the name and alias of the remote
|
|
177
|
-
nameOrAlias: string;
|
|
178
|
-
// Expose to be preloaded
|
|
179
|
-
// Default to preload all exposes
|
|
180
|
-
// Only the required expose will be loaded when an expose is provided
|
|
181
|
-
exposes?: Array<string>; // Default request
|
|
182
|
-
// Default is sync, only load synchronous code referenced in the expose
|
|
183
|
-
// Set to all to load synchronous and asynchronous references
|
|
184
|
-
resourceCategory?: 'all' | 'sync';
|
|
185
|
-
// Load all dependencies when no value is configured
|
|
186
|
-
// Only the resources needed will be loaded after configuring the dependency
|
|
187
|
-
depsRemote?: boolean | Array<depsPreloadArg>;
|
|
188
|
-
// No filtering of resources when not configured
|
|
189
|
-
// Will filter out unwanted resources after configuring
|
|
190
|
-
filter?: (assetUrl: string) => boolean;
|
|
191
|
-
};
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
- Details
|
|
195
|
-
|
|
196
|
-
**Info**: Only if the entry is manifest can more resources be loaded. The manifest plugin will be supported in 2024.Q1
|
|
197
|
-
|
|
198
|
-
Through `preloadRemote`, module resources can be preloaded at an earlier stage to avoid waterfall requests. `preloadRemote` can preload the following content:
|
|
199
|
-
|
|
200
|
-
- The `remoteEntry` of `remote`
|
|
201
|
-
- `expose` assets of `remote`
|
|
202
|
-
- Synchronous resources or asynchronous resources of `remote`
|
|
203
|
-
- `remote` resources that `remote` depends on
|
|
204
|
-
|
|
205
|
-
* Example
|
|
206
|
-
|
|
207
|
-
```ts
|
|
208
|
-
import { init, preloadRemote } from '@module-federation/runtime';
|
|
209
|
-
|
|
210
|
-
init({
|
|
211
|
-
name: '@demo/preload-remote',
|
|
212
|
-
remotes: [
|
|
213
|
-
{
|
|
214
|
-
name: '@demo/sub1',
|
|
215
|
-
entry: 'http://localhost:2001/mf-manifest.json',
|
|
216
|
-
},
|
|
217
|
-
{
|
|
218
|
-
name: '@demo/sub2',
|
|
219
|
-
entry: 'http://localhost:2001/mf-manifest.json',
|
|
220
|
-
},
|
|
221
|
-
{
|
|
222
|
-
name: '@demo/sub3',
|
|
223
|
-
entry: 'http://localhost:2001/mf-manifest.json',
|
|
224
|
-
},
|
|
225
|
-
],
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
// Preload @demo/sub1 module
|
|
229
|
-
// Filter resource information that contains ignore in the resource name
|
|
230
|
-
// Only preload sub-dependent @demo/sub1-button modules
|
|
231
|
-
preloadRemote([
|
|
232
|
-
{
|
|
233
|
-
nameOrAlias: '@demo/sub1',
|
|
234
|
-
filter(assetUrl) {
|
|
235
|
-
return assetUrl.indexOf('ignore') === -1;
|
|
236
|
-
},
|
|
237
|
-
depsRemote: [{ nameOrAlias: '@demo/sub1-button' }],
|
|
238
|
-
},
|
|
239
|
-
]);
|
|
240
|
-
|
|
241
|
-
// Preload @demo/sub2 module
|
|
242
|
-
// Preload all exposes under @demo/sub2
|
|
243
|
-
// Preload the synchronous resources and asynchronous resources of @demo/sub2
|
|
244
|
-
preloadRemote([
|
|
245
|
-
{
|
|
246
|
-
nameOrAlias: '@demo/sub2',
|
|
247
|
-
resourceCategory: 'all',
|
|
248
|
-
},
|
|
249
|
-
]);
|
|
250
|
-
|
|
251
|
-
// Preload expose of @demo/sub3 module
|
|
252
|
-
preloadRemote([
|
|
253
|
-
{
|
|
254
|
-
nameOrAlias: '@demo/sub3',
|
|
255
|
-
resourceCategory: 'all',
|
|
256
|
-
exposes: ['add'],
|
|
257
|
-
},
|
|
258
|
-
]);
|
|
259
|
-
```
|
|
260
|
-
|
|
261
|
-
### registerRemotes
|
|
262
|
-
|
|
263
|
-
- Type: `registerRemotes(remotes: Remote[], options?: { force?: boolean }): void`
|
|
264
|
-
- Used to register remotes after init .
|
|
265
|
-
|
|
266
|
-
- Type
|
|
267
|
-
|
|
268
|
-
```typescript
|
|
269
|
-
function registerRemotes(remotes: Remote[], options?: { force?: boolean }) {}
|
|
270
|
-
|
|
271
|
-
type Remote = (RemoteWithEntry | RemoteWithVersion) & RemoteInfoCommon;
|
|
272
|
-
|
|
273
|
-
interface RemoteInfoCommon {
|
|
274
|
-
alias?: string;
|
|
275
|
-
shareScope?: string;
|
|
276
|
-
type?: RemoteEntryType;
|
|
277
|
-
entryGlobalName?: string;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
interface RemoteWithEntry {
|
|
281
|
-
name: string;
|
|
282
|
-
entry: string;
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
interface RemoteWithVersion {
|
|
286
|
-
name: string;
|
|
287
|
-
version: string;
|
|
288
|
-
}
|
|
289
|
-
```
|
|
290
|
-
|
|
291
|
-
- Details
|
|
292
|
-
**info**: Please be careful when setting `force:true` !
|
|
293
|
-
|
|
294
|
-
If set `force: true`, it will merge remote(include loaded remote), and remove loaded remote cache , as well as console.warn to tell this action may have risks.
|
|
295
|
-
|
|
296
|
-
* Example
|
|
297
|
-
|
|
298
|
-
```ts
|
|
299
|
-
import { init, registerRemotes } from '@module-federation/runtime';
|
|
300
|
-
|
|
301
|
-
init({
|
|
302
|
-
name: '@demo/register-new-remotes',
|
|
303
|
-
remotes: [
|
|
304
|
-
{
|
|
305
|
-
name: '@demo/sub1',
|
|
306
|
-
entry: 'http://localhost:2001/mf-manifest.json',
|
|
307
|
-
}
|
|
308
|
-
],
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
// add new remote @demo/sub2
|
|
312
|
-
registerRemotes([
|
|
313
|
-
{
|
|
314
|
-
name: '@demo/sub2',
|
|
315
|
-
entry: 'http://localhost:2002/mf-manifest.json',
|
|
316
|
-
}
|
|
317
|
-
]);
|
|
318
|
-
|
|
319
|
-
// override previous remote @demo/sub1
|
|
320
|
-
registerRemotes([
|
|
321
|
-
{
|
|
322
|
-
name: '@demo/sub1',
|
|
323
|
-
entry: 'http://localhost:2003/mf-manifest.json',
|
|
324
|
-
}
|
|
325
|
-
]);
|
|
326
|
-
```
|
|
327
|
-
|
|
328
|
-
### registerPlugins
|
|
329
|
-
|
|
330
|
-
- Type: `registerPlugins(plugins: Array<FederationRuntimePlugin>): void`
|
|
331
|
-
- Used to register remotes after init .
|
|
332
|
-
|
|
333
|
-
- Type
|
|
334
|
-
|
|
335
|
-
```typescript
|
|
336
|
-
import type { FederationRuntimePlugin } from '@module-federation/runtime';
|
|
337
|
-
|
|
338
|
-
function registerPlugins(plugins: Array<FederationRuntimePlugin>) {}
|
|
339
|
-
```
|
|
340
|
-
|
|
341
|
-
- Details:
|
|
342
|
-
Allows plugins to be registered dynamically or lazily. Lazy plugins will be called after normal runtime plugins.
|
|
343
|
-
Similar function as registerRemote, but for runtime plugins.
|
|
344
|
-
* Example
|
|
345
|
-
|
|
346
|
-
```ts
|
|
347
|
-
import { init, registerPlugins } from '@module-federation/runtime';
|
|
348
|
-
|
|
349
|
-
init({
|
|
350
|
-
name: '@demo/register-new-remotes',
|
|
351
|
-
remotes: [
|
|
352
|
-
{
|
|
353
|
-
name: '@demo/sub1',
|
|
354
|
-
entry: 'http://localhost:2001/mf-manifest.json',
|
|
355
|
-
}
|
|
356
|
-
],
|
|
357
|
-
plugins: [
|
|
358
|
-
//normal/eager runtime plugins
|
|
359
|
-
]
|
|
360
|
-
});
|
|
361
|
-
|
|
362
|
-
import('./runtime-plugin').then(plugin => {
|
|
363
|
-
registerPlugins([
|
|
364
|
-
//more plugins loaded lazyily or dynamically
|
|
365
|
-
plugin
|
|
366
|
-
]);
|
|
367
|
-
})
|
|
368
|
-
```
|
|
369
|
-
|
|
370
|
-
## hooks
|
|
371
|
-
|
|
372
|
-
Lifecycle hooks for FederationHost interaction.
|
|
373
|
-
|
|
374
|
-
- example
|
|
375
|
-
|
|
376
|
-
```ts
|
|
377
|
-
import { init } from '@module-federation/runtime';
|
|
378
|
-
import type { FederationRuntimePlugin } from '@module-federation/runtime';
|
|
379
|
-
|
|
380
|
-
const runtimePlugin: () => FederationRuntimePlugin = function () {
|
|
381
|
-
return {
|
|
382
|
-
name: 'my-runtime-plugin',
|
|
383
|
-
beforeInit(args) {
|
|
384
|
-
console.log('beforeInit: ', args);
|
|
385
|
-
return args;
|
|
386
|
-
},
|
|
387
|
-
beforeRequest(args) {
|
|
388
|
-
console.log('beforeRequest: ', args);
|
|
389
|
-
return args;
|
|
390
|
-
},
|
|
391
|
-
afterResolve(args) {
|
|
392
|
-
console.log('afterResolve', args);
|
|
393
|
-
return args;
|
|
394
|
-
},
|
|
395
|
-
onLoad(args) {
|
|
396
|
-
console.log('onLoad: ', args);
|
|
397
|
-
return args;
|
|
398
|
-
},
|
|
399
|
-
async loadShare(args) {
|
|
400
|
-
console.log('loadShare:', args);
|
|
401
|
-
},
|
|
402
|
-
async beforeLoadShare(args) {
|
|
403
|
-
console.log('beforeloadShare:', args);
|
|
404
|
-
return args;
|
|
405
|
-
},
|
|
406
|
-
};
|
|
407
|
-
};
|
|
408
|
-
|
|
409
|
-
init({
|
|
410
|
-
name: '@demo/app-main',
|
|
411
|
-
remotes: [
|
|
412
|
-
{
|
|
413
|
-
name: '@demo/app2',
|
|
414
|
-
entry: 'http://localhost:3006/remoteEntry.js',
|
|
415
|
-
alias: 'app2',
|
|
416
|
-
},
|
|
417
|
-
],
|
|
418
|
-
plugins: [runtimePlugin()],
|
|
419
|
-
});
|
|
420
|
-
```
|
|
421
|
-
|
|
422
|
-
### beforeInit
|
|
423
|
-
|
|
424
|
-
`SyncWaterfallHook`
|
|
425
|
-
|
|
426
|
-
Updates Federation Host configurations before the initialization process of remote containers.
|
|
427
|
-
|
|
428
|
-
- type
|
|
429
|
-
|
|
430
|
-
```ts
|
|
431
|
-
function beforeInit(args: BeforeInitOptions): BeforeInitOptions;
|
|
432
|
-
|
|
433
|
-
type BeforeInitOptions = {
|
|
434
|
-
userOptions: UserOptions;
|
|
435
|
-
options: FederationRuntimeOptions;
|
|
436
|
-
origin: FederationHost;
|
|
437
|
-
shareInfo: ShareInfos;
|
|
438
|
-
};
|
|
439
|
-
|
|
440
|
-
interface FederationRuntimeOptions {
|
|
441
|
-
id?: string;
|
|
442
|
-
name: string;
|
|
443
|
-
version?: string;
|
|
444
|
-
remotes: Array<Remote>;
|
|
445
|
-
shared: ShareInfos;
|
|
446
|
-
plugins: Array<FederationRuntimePlugin>;
|
|
447
|
-
inBrowser: boolean;
|
|
448
|
-
}
|
|
449
|
-
```
|
|
450
|
-
|
|
451
|
-
### init
|
|
452
|
-
|
|
453
|
-
`SyncHook`
|
|
454
|
-
|
|
455
|
-
Called during the initialization of remote containers.
|
|
456
|
-
|
|
457
|
-
- type
|
|
458
|
-
|
|
459
|
-
```ts
|
|
460
|
-
function init(args: InitOptions): void;
|
|
461
|
-
|
|
462
|
-
type InitOptions = {
|
|
463
|
-
options: FederationRuntimeOptions;
|
|
464
|
-
origin: FederationHost;
|
|
465
|
-
};
|
|
466
|
-
```
|
|
467
|
-
|
|
468
|
-
### beforeRequest
|
|
469
|
-
|
|
470
|
-
`AsyncWaterfallHook`
|
|
471
|
-
|
|
472
|
-
Invoked before resolving a remote container, useful for injecting the container or updating something ahead of the lookup.
|
|
473
|
-
|
|
474
|
-
- type
|
|
475
|
-
|
|
476
|
-
```ts
|
|
477
|
-
async function beforeRequest(args: BeforeRequestOptions): Promise<BeforeRequestOptions>;
|
|
478
|
-
|
|
479
|
-
type BeforeRequestOptions = {
|
|
480
|
-
id: string;
|
|
481
|
-
options: FederationRuntimeOptions;
|
|
482
|
-
origin: FederationHost;
|
|
483
|
-
};
|
|
484
|
-
```
|
|
485
|
-
|
|
486
|
-
### afterResolve
|
|
487
|
-
|
|
488
|
-
`AsyncWaterfallHook`
|
|
489
|
-
|
|
490
|
-
Called after resolving a container, allowing redirection or modification of resolved information.
|
|
491
|
-
|
|
492
|
-
- type
|
|
493
|
-
|
|
494
|
-
```ts
|
|
495
|
-
async function afterResolve(args: AfterResolveOptions): Promise<AfterResolveOptions>;
|
|
496
|
-
|
|
497
|
-
type AfterResolveOptions = {
|
|
498
|
-
id: string;
|
|
499
|
-
pkgNameOrAlias: string;
|
|
500
|
-
expose: string;
|
|
501
|
-
remote: Remote;
|
|
502
|
-
options: FederationRuntimeOptions;
|
|
503
|
-
origin: FederationHost;
|
|
504
|
-
remoteInfo: RemoteInfo;
|
|
505
|
-
remoteSnapshot?: ModuleInfo;
|
|
506
|
-
};
|
|
507
|
-
```
|
|
508
|
-
|
|
509
|
-
### onLoad
|
|
510
|
-
|
|
511
|
-
`AsyncHook`
|
|
512
|
-
|
|
513
|
-
Triggered once a federated module is loaded, allowing access and modification to the exports of the loaded file.
|
|
514
|
-
|
|
515
|
-
- type
|
|
516
|
-
|
|
517
|
-
```ts
|
|
518
|
-
async function onLoad(args: OnLoadOptions): Promise<void>;
|
|
519
|
-
|
|
520
|
-
type OnLoadOptions = {
|
|
521
|
-
id: string;
|
|
522
|
-
expose: string;
|
|
523
|
-
pkgNameOrAlias: string;
|
|
524
|
-
remote: Remote;
|
|
525
|
-
options: ModuleOptions;
|
|
526
|
-
origin: FederationHost;
|
|
527
|
-
exposeModule: any;
|
|
528
|
-
exposeModuleFactory: any;
|
|
529
|
-
moduleInstance: Module;
|
|
530
|
-
};
|
|
531
|
-
|
|
532
|
-
type ModuleOptions = {
|
|
533
|
-
remoteInfo: RemoteInfo;
|
|
534
|
-
host: FederationHost;
|
|
535
|
-
};
|
|
536
|
-
|
|
537
|
-
interface RemoteInfo {
|
|
538
|
-
name: string;
|
|
539
|
-
version?: string;
|
|
540
|
-
buildVersion?: string;
|
|
541
|
-
entry: string;
|
|
542
|
-
type: RemoteEntryType;
|
|
543
|
-
entryGlobalName: string;
|
|
544
|
-
shareScope: string;
|
|
545
|
-
}
|
|
546
|
-
```
|
|
547
|
-
|
|
548
|
-
### handlePreloadModule
|
|
549
|
-
|
|
550
|
-
`SyncHook`
|
|
551
|
-
|
|
552
|
-
Handles preloading logic for federated modules.
|
|
553
|
-
|
|
554
|
-
- type
|
|
555
|
-
|
|
556
|
-
```ts
|
|
557
|
-
function handlePreloadModule(args: HandlePreloadModuleOptions): void;
|
|
558
|
-
|
|
559
|
-
type HandlePreloadModuleOptions = {
|
|
560
|
-
id: string;
|
|
561
|
-
name: string;
|
|
562
|
-
remoteSnapshot: ModuleInfo;
|
|
563
|
-
preloadConfig: PreloadRemoteArgs;
|
|
564
|
-
};
|
|
565
|
-
```
|
|
566
|
-
|
|
567
|
-
### errorLoadRemote
|
|
568
|
-
|
|
569
|
-
`AsyncHook`
|
|
570
|
-
|
|
571
|
-
Invoked if loading a federated module fails, enabling custom error handling.
|
|
572
|
-
|
|
573
|
-
- type
|
|
574
|
-
|
|
575
|
-
```ts
|
|
576
|
-
async function errorLoadRemote(args: ErrorLoadRemoteOptions): Promise<void | unknown>;
|
|
577
|
-
|
|
578
|
-
type ErrorLoadRemoteOptions = {
|
|
579
|
-
id: string;
|
|
580
|
-
error: unknown;
|
|
581
|
-
from: 'build' | 'runtime';
|
|
582
|
-
origin: FederationHost;
|
|
583
|
-
};
|
|
584
|
-
```
|
|
585
|
-
|
|
586
|
-
- example
|
|
587
|
-
|
|
588
|
-
```ts
|
|
589
|
-
import { init, loadRemote } from '@module-federation/runtime';
|
|
590
|
-
|
|
591
|
-
import type { FederationRuntimePlugin } from '@module-federation/runtime';
|
|
592
|
-
|
|
593
|
-
const fallbackPlugin: () => FederationRuntimePlugin = function () {
|
|
594
|
-
return {
|
|
595
|
-
name: 'fallback-plugin',
|
|
596
|
-
errorLoadRemote(args) {
|
|
597
|
-
const fallback = 'fallback';
|
|
598
|
-
return fallback;
|
|
599
|
-
},
|
|
600
|
-
};
|
|
601
|
-
};
|
|
602
|
-
|
|
603
|
-
init({
|
|
604
|
-
name: '@demo/app-main',
|
|
605
|
-
remotes: [
|
|
606
|
-
{
|
|
607
|
-
name: '@demo/app2',
|
|
608
|
-
entry: 'http://localhost:3006/remoteEntry.js',
|
|
609
|
-
alias: 'app2',
|
|
610
|
-
},
|
|
611
|
-
],
|
|
612
|
-
plugins: [fallbackPlugin()],
|
|
613
|
-
});
|
|
614
|
-
|
|
615
|
-
loadRemote('app2/un-existed-module').then((mod) => {
|
|
616
|
-
expect(mod).toEqual('fallback');
|
|
617
|
-
});
|
|
618
|
-
```
|
|
619
|
-
|
|
620
|
-
### beforeLoadShare
|
|
621
|
-
|
|
622
|
-
`AsyncWaterfallHook`
|
|
623
|
-
|
|
624
|
-
Called before attempting to load or negotiate shared modules between federated apps.
|
|
625
|
-
|
|
626
|
-
- type
|
|
627
|
-
|
|
628
|
-
```ts
|
|
629
|
-
async function beforeLoadShare(args: BeforeLoadShareOptions): Promise<BeforeLoadShareOptions>;
|
|
630
|
-
|
|
631
|
-
type BeforeLoadShareOptions = {
|
|
632
|
-
pkgName: string;
|
|
633
|
-
shareInfo?: Shared;
|
|
634
|
-
shared: Options['shared'];
|
|
635
|
-
origin: FederationHost;
|
|
636
|
-
};
|
|
637
|
-
```
|
|
638
|
-
|
|
639
|
-
### resolveShare
|
|
640
|
-
|
|
641
|
-
`SyncWaterfallHook`
|
|
642
|
-
|
|
643
|
-
Allows manual resolution of shared module requests.
|
|
644
|
-
|
|
645
|
-
- type
|
|
646
|
-
|
|
647
|
-
```ts
|
|
648
|
-
function resolveShare(args: ResolveShareOptions): ResolveShareOptions;
|
|
649
|
-
|
|
650
|
-
type ResolveShareOptions = {
|
|
651
|
-
shareScopeMap: ShareScopeMap;
|
|
652
|
-
scope: string;
|
|
653
|
-
pkgName: string;
|
|
654
|
-
version: string;
|
|
655
|
-
GlobalFederation: Federation;
|
|
656
|
-
resolver: () => Shared | undefined;
|
|
657
|
-
};
|
|
658
|
-
```
|
|
659
|
-
|
|
660
|
-
- example
|
|
661
|
-
|
|
662
|
-
```ts
|
|
663
|
-
import { init, loadRemote } from '@module-federation/runtime';
|
|
664
|
-
|
|
665
|
-
import type { FederationRuntimePlugin } from '@module-federation/runtime';
|
|
666
|
-
|
|
667
|
-
const customSharedPlugin: () => FederationRuntimePlugin = function () {
|
|
668
|
-
return {
|
|
669
|
-
name: 'custom-shared-plugin',
|
|
670
|
-
resolveShare(args) {
|
|
671
|
-
const { shareScopeMap, scope, pkgName, version, GlobalFederation } = args;
|
|
672
|
-
|
|
673
|
-
if (pkgName !== 'react') {
|
|
674
|
-
return args;
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
args.resolver = function () {
|
|
678
|
-
shareScopeMap[scope][pkgName][version] = window.React; // replace local share scope manually with desired module
|
|
679
|
-
return shareScopeMap[scope][pkgName][version];
|
|
680
|
-
};
|
|
681
|
-
return args;
|
|
682
|
-
},
|
|
683
|
-
};
|
|
684
|
-
};
|
|
685
|
-
|
|
686
|
-
init({
|
|
687
|
-
name: '@demo/app-main',
|
|
688
|
-
shared: {
|
|
689
|
-
react: {
|
|
690
|
-
version: '17.0.0',
|
|
691
|
-
scope: 'default',
|
|
692
|
-
lib: () => React,
|
|
693
|
-
shareConfig: {
|
|
694
|
-
singleton: true,
|
|
695
|
-
requiredVersion: '^17.0.0',
|
|
696
|
-
},
|
|
697
|
-
},
|
|
698
|
-
},
|
|
699
|
-
plugins: [customSharedPlugin()],
|
|
700
|
-
});
|
|
701
|
-
|
|
702
|
-
window.React = () => 'Desired Shared';
|
|
703
|
-
|
|
704
|
-
loadShare('react').then((reactFactory) => {
|
|
705
|
-
expect(reactFactory()).toEqual(window.React());
|
|
706
|
-
});
|
|
707
|
-
```
|
|
708
|
-
|
|
709
|
-
### beforePreloadRemote
|
|
710
|
-
|
|
711
|
-
`AsyncHook`
|
|
712
|
-
|
|
713
|
-
Invoked before any preload logic is executed by the preload handler.
|
|
714
|
-
|
|
715
|
-
- type
|
|
716
|
-
|
|
717
|
-
```ts
|
|
718
|
-
async function beforePreloadRemote(args: BeforePreloadRemoteOptions): BeforePreloadRemoteOptions;
|
|
719
|
-
|
|
720
|
-
type BeforePreloadRemoteOptions = {
|
|
721
|
-
preloadOps: Array<PreloadRemoteArgs>;
|
|
722
|
-
options: Options;
|
|
723
|
-
origin: FederationHost;
|
|
724
|
-
};
|
|
725
|
-
```
|
|
726
|
-
|
|
727
|
-
### generatePreloadAssets
|
|
728
|
-
|
|
729
|
-
`AsyncHook`
|
|
730
|
-
|
|
731
|
-
Called for generating preload assets based on configurations.
|
|
732
|
-
|
|
733
|
-
- type
|
|
734
|
-
|
|
735
|
-
```ts
|
|
736
|
-
async function generatePreloadAssets(args: GeneratePreloadAssetsOptions): Promise<PreloadAssets>;
|
|
737
|
-
|
|
738
|
-
type GeneratePreloadAssetsOptions = {
|
|
739
|
-
origin: FederationHost;
|
|
740
|
-
preloadOptions: PreloadOptions[number];
|
|
741
|
-
remote: Remote;
|
|
742
|
-
remoteInfo: RemoteInfo;
|
|
743
|
-
remoteSnapshot: ModuleInfo;
|
|
744
|
-
globalSnapshot: GlobalModuleInfo;
|
|
745
|
-
};
|
|
746
|
-
|
|
747
|
-
interface PreloadAssets {
|
|
748
|
-
cssAssets: Array<string>;
|
|
749
|
-
jsAssetsWithoutEntry: Array<string>;
|
|
750
|
-
entryAssets: Array<EntryAssets>;
|
|
751
|
-
}
|
|
752
|
-
```
|
|
753
|
-
|
|
754
|
-
## loaderHook
|
|
755
|
-
|
|
756
|
-
Plugin system for module loading operations.
|
|
757
|
-
|
|
758
|
-
### createScript
|
|
759
|
-
|
|
760
|
-
`SyncHook`
|
|
761
|
-
|
|
762
|
-
- type
|
|
763
|
-
|
|
764
|
-
```ts
|
|
765
|
-
function createScript(args: CreateScriptOptions): HTMLScriptElement | void;
|
|
766
|
-
|
|
767
|
-
type CreateScriptOptions = {
|
|
768
|
-
url: string;
|
|
769
|
-
};
|
|
770
|
-
```
|
|
771
|
-
|
|
772
|
-
- example
|
|
773
|
-
|
|
774
|
-
```ts
|
|
775
|
-
import { init } from '@module-federation/runtime';
|
|
776
|
-
import type { FederationRuntimePlugin } from '@module-federation/runtime';
|
|
777
|
-
|
|
778
|
-
const changeScriptAttributePlugin: () => FederationRuntimePlugin = function () {
|
|
779
|
-
return {
|
|
780
|
-
name: 'change-script-attribute',
|
|
781
|
-
createScript({ url }) {
|
|
782
|
-
if (url === testRemoteEntry) {
|
|
783
|
-
let script = document.createElement('script');
|
|
784
|
-
script.src = testRemoteEntry;
|
|
785
|
-
script.setAttribute('loader-hooks', 'isTrue');
|
|
786
|
-
script.setAttribute('crossorigin', 'anonymous');
|
|
787
|
-
return script;
|
|
788
|
-
}
|
|
789
|
-
},
|
|
790
|
-
};
|
|
791
|
-
};
|
|
792
|
-
```
|
|
13
|
+
`@module-federation/runtime` is [MIT licensed](https://github.com/module-federation/universe/blob/main/packages/runtime/LICENSE).
|