@module-federation/runtime 0.0.0-feat-node-support-1702930274548 → 0.0.0-next-20231219093556
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/.eslintrc.json +23 -0
- package/.swcrc +29 -0
- package/CHANGELOG.md +8 -0
- package/README.md +301 -0
- package/__tests__/__snapshots__/preload-remote.spec.ts.snap +111 -0
- package/__tests__/api.spec.ts +153 -0
- package/__tests__/globa.spec.ts +19 -0
- package/__tests__/hooks.spec.ts +269 -0
- package/__tests__/instance.spec.ts +12 -0
- package/__tests__/is-static-resources-equal.spec.ts +43 -0
- package/__tests__/load-remote.spec.ts +566 -0
- package/__tests__/mock/env.ts +1 -0
- package/__tests__/mock/handlers.ts +41 -0
- package/__tests__/mock/mock-script.ts +127 -0
- package/__tests__/mock/moduleInfo.ts +13 -0
- package/__tests__/mock/server.ts +4 -0
- package/__tests__/mock/utils.ts +128 -0
- package/__tests__/preload-remote.spec.ts +352 -0
- package/__tests__/resources/app2/federation-remote-entry.js +11 -0
- package/__tests__/resources/hooks/app2/federation-remote-entry.js +11 -0
- package/__tests__/resources/hooks/app3/federation-remote-entry.js +11 -0
- package/__tests__/resources/load-remote/app1/federation-remote-entry.js +11 -0
- package/__tests__/resources/load-remote/app2/federation-remote-entry.js +11 -0
- package/__tests__/resources/load-remote/app2/say.sync.js +1 -0
- package/__tests__/resources/load-remote/circulate-dep-app1/federation-manifest.json +51 -0
- package/__tests__/resources/load-remote/circulate-dep-app1/federation-remote-entry.js +13 -0
- package/__tests__/resources/load-remote/circulate-dep-app2/federation-manifest.json +51 -0
- package/__tests__/resources/load-remote/circulate-dep-app2/federation-remote-entry.js +34 -0
- package/__tests__/resources/load-remote/diff-instance/federation-remote-entry.js +16 -0
- package/__tests__/resources/main/federation-manifest.json +38 -0
- package/__tests__/resources/main/federation-remote-entry.js +14 -0
- package/__tests__/resources/preload/preload-resource/federation-remote-entry.js +16 -0
- package/__tests__/semver.spec.ts +228 -0
- package/__tests__/setup.ts +18 -0
- package/__tests__/share.ts +163 -0
- package/__tests__/shares.spec.ts +463 -0
- package/dist/LICENSE +21 -0
- package/{helpers.cjs.js → dist/helpers.cjs.js} +1 -1
- package/{helpers.esm.js → dist/helpers.esm.js} +2 -2
- package/{index.cjs.js → dist/index.cjs.js} +85 -1086
- package/{index.esm.js → dist/index.esm.js} +85 -1085
- package/dist/package.json +46 -0
- package/{share.cjs.js → dist/share.cjs.js} +46 -71
- package/{share.esm.js → dist/share.esm.js} +46 -71
- package/{src → dist/src}/core.d.ts +6 -15
- package/{src → dist/src}/global.d.ts +3 -2
- package/{src → dist/src}/helpers.d.ts +2 -2
- package/{src → dist/src}/index.d.ts +0 -1
- package/{src → dist/src}/plugins/snapshot/SnapshotHandler.d.ts +1 -0
- package/{src → dist/src}/type/config.d.ts +2 -5
- package/{src → dist/src}/utils/hooks/pluginSystem.d.ts +0 -1
- package/dist/src/utils/semver/constants.d.ts +10 -0
- package/dist/src/utils/share.d.ts +7 -0
- package/global.d.ts +3 -0
- package/jest.config.ts +30 -0
- package/package.json +21 -16
- package/project.json +76 -0
- package/rollup.config.js +31 -0
- package/src/constant.ts +2 -0
- package/src/core.md +110 -0
- package/src/core.ts +746 -0
- package/src/global.ts +286 -0
- package/src/helpers.ts +76 -0
- package/src/index.ts +66 -0
- package/src/module/index.ts +131 -0
- package/src/plugins/generate-preload-assets.ts +343 -0
- package/src/plugins/snapshot/SnapshotHandler.ts +378 -0
- package/src/plugins/snapshot/index.ts +71 -0
- package/src/type/config.ts +126 -0
- package/src/type/index.ts +3 -0
- package/src/type/plugin.ts +25 -0
- package/src/type/preload.ts +32 -0
- package/src/types.ts +1 -0
- package/src/utils/env.ts +17 -0
- package/src/utils/hooks/asyncHook.ts +29 -0
- package/src/utils/hooks/asyncWaterfallHooks.ts +53 -0
- package/src/utils/hooks/index.ts +6 -0
- package/src/utils/hooks/pluginSystem.ts +73 -0
- package/src/utils/hooks/syncHook.ts +48 -0
- package/src/utils/hooks/syncWaterfallHook.ts +54 -0
- package/src/utils/index.ts +5 -0
- package/src/utils/load.ts +112 -0
- package/src/utils/logger.ts +16 -0
- package/src/utils/manifest.ts +81 -0
- package/src/utils/plugin.ts +31 -0
- package/src/utils/preload.ts +141 -0
- package/src/utils/semver/compare.ts +116 -0
- package/src/utils/semver/constants.ts +31 -0
- package/src/utils/semver/index.ts +143 -0
- package/src/utils/semver/parser.ts +240 -0
- package/src/utils/semver/utils.ts +76 -0
- package/src/utils/share.ts +192 -0
- package/src/utils/tool.ts +75 -0
- package/tsconfig.json +23 -0
- package/tsconfig.lib.json +10 -0
- package/tsconfig.spec.json +14 -0
- package/vitest.config.ts +19 -0
- package/src/utils/semver/constants.d.ts +0 -10
- package/src/utils/share.d.ts +0 -7
- /package/{helpers.cjs.d.ts → dist/helpers.cjs.d.ts} +0 -0
- /package/{index.cjs.d.ts → dist/index.cjs.d.ts} +0 -0
- /package/{src → dist/src}/constant.d.ts +0 -0
- /package/{src → dist/src}/module/index.d.ts +0 -0
- /package/{src → dist/src}/plugins/generate-preload-assets.d.ts +0 -0
- /package/{src → dist/src}/plugins/snapshot/index.d.ts +0 -0
- /package/{src → dist/src}/type/index.d.ts +0 -0
- /package/{src → dist/src}/type/plugin.d.ts +0 -0
- /package/{src → dist/src}/type/preload.d.ts +0 -0
- /package/{src → dist/src}/types.d.ts +0 -0
- /package/{src → dist/src}/utils/env.d.ts +0 -0
- /package/{src → dist/src}/utils/hooks/asyncHook.d.ts +0 -0
- /package/{src → dist/src}/utils/hooks/asyncWaterfallHooks.d.ts +0 -0
- /package/{src → dist/src}/utils/hooks/index.d.ts +0 -0
- /package/{src → dist/src}/utils/hooks/syncHook.d.ts +0 -0
- /package/{src → dist/src}/utils/hooks/syncWaterfallHook.d.ts +0 -0
- /package/{src → dist/src}/utils/index.d.ts +0 -0
- /package/{src → dist/src}/utils/load.d.ts +0 -0
- /package/{src → dist/src}/utils/logger.d.ts +0 -0
- /package/{src → dist/src}/utils/manifest.d.ts +0 -0
- /package/{src → dist/src}/utils/plugin.d.ts +0 -0
- /package/{src → dist/src}/utils/preload.d.ts +0 -0
- /package/{src → dist/src}/utils/semver/compare.d.ts +0 -0
- /package/{src → dist/src}/utils/semver/index.d.ts +0 -0
- /package/{src → dist/src}/utils/semver/parser.d.ts +0 -0
- /package/{src → dist/src}/utils/semver/utils.d.ts +0 -0
- /package/{src → dist/src}/utils/tool.d.ts +0 -0
- /package/{type.cjs.d.ts → dist/type.cjs.d.ts} +0 -0
- /package/{type.cjs.js → dist/type.cjs.js} +0 -0
- /package/{type.esm.js → dist/type.esm.js} +0 -0
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": ["../../.eslintrc.json"],
|
|
3
|
+
"ignorePatterns": ["!**/*"],
|
|
4
|
+
"overrides": [
|
|
5
|
+
{
|
|
6
|
+
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
|
|
7
|
+
"rules": {}
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"files": ["*.ts", "*.tsx"],
|
|
11
|
+
"rules": {}
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"files": ["*.js", "*.jsx"],
|
|
15
|
+
"rules": {}
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"files": ["*.json"],
|
|
19
|
+
"parser": "jsonc-eslint-parser",
|
|
20
|
+
"rules": {}
|
|
21
|
+
}
|
|
22
|
+
]
|
|
23
|
+
}
|
package/.swcrc
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"jsc": {
|
|
3
|
+
"target": "es2017",
|
|
4
|
+
"parser": {
|
|
5
|
+
"syntax": "typescript",
|
|
6
|
+
"decorators": true,
|
|
7
|
+
"dynamicImport": true
|
|
8
|
+
},
|
|
9
|
+
"transform": {
|
|
10
|
+
"decoratorMetadata": true,
|
|
11
|
+
"legacyDecorator": true
|
|
12
|
+
},
|
|
13
|
+
"keepClassNames": true,
|
|
14
|
+
"externalHelpers": false,
|
|
15
|
+
"loose": true
|
|
16
|
+
},
|
|
17
|
+
"module": {
|
|
18
|
+
"type": "es6"
|
|
19
|
+
},
|
|
20
|
+
"sourceMaps": true,
|
|
21
|
+
"exclude": [
|
|
22
|
+
"jest.config.ts",
|
|
23
|
+
".*\\.spec.tsx?$",
|
|
24
|
+
".*\\.test.tsx?$",
|
|
25
|
+
"./src/jest-setup.ts$",
|
|
26
|
+
"./**/jest-setup.ts$",
|
|
27
|
+
".*.js$"
|
|
28
|
+
]
|
|
29
|
+
}
|
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
# `@module-federation/runtime`
|
|
2
|
+
|
|
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
|
+
- Only consume part of the export of the remote module and will not fully download the remote module
|
|
5
|
+
- The runtime calling process can be extended through the module-runtime plug-in mechanism
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
```javascript
|
|
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';
|
|
13
|
+
|
|
14
|
+
init({
|
|
15
|
+
name: '@demo/app-main',
|
|
16
|
+
remotes: [
|
|
17
|
+
{
|
|
18
|
+
name: "@demo/app2",
|
|
19
|
+
entry: "http://localhost:3006/remoteEntry.js"
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
name: "@demo/app3",
|
|
23
|
+
alias: "app3",
|
|
24
|
+
entry: "http://localhost:2001/module-federation-manifest.json"
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Load by alias
|
|
30
|
+
loadRemote<{add: (...args: Array<number>)=> number }>("app3/util").then((md)=>{
|
|
31
|
+
md.add(1,2,3);
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### init
|
|
36
|
+
|
|
37
|
+
- Type: `init(options: InitOptions): void`
|
|
38
|
+
- It is used to dynamically register the module at runtime
|
|
39
|
+
- InitOptions:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
type InitOptions {
|
|
43
|
+
name: string;
|
|
44
|
+
version?: string;
|
|
45
|
+
shared?: ShareInfos;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
type RemoteInfo = (RemotesWithEntry | RemotesWithVersion) & {
|
|
49
|
+
alias?: string;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
interface RemotesWithVersion {
|
|
53
|
+
name: string;
|
|
54
|
+
version: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface RemotesWithEntry {
|
|
58
|
+
name: string;
|
|
59
|
+
entry: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
type ShareInfos = {
|
|
63
|
+
// Package name and dependency basic information and sharing strategy
|
|
64
|
+
[pkgName: string]: Share;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
type Share = {
|
|
68
|
+
// Versions of shared dependencies
|
|
69
|
+
version: string;
|
|
70
|
+
// Which modules consume the current shared dependencies?
|
|
71
|
+
useIn?: Array<string>;
|
|
72
|
+
// Which module does the shared dependency come from?
|
|
73
|
+
from?: string;
|
|
74
|
+
// Get the factory function of the shared dependency instance. When the cached shared instance cannot be loaded, its own shared dependency will be loaded.
|
|
75
|
+
lib: () => Module;
|
|
76
|
+
// Sharing strategy, what strategy will be used to determine the reuse of shared dependencies
|
|
77
|
+
shareConfig?: SharedConfig;
|
|
78
|
+
// Dependencies between shares
|
|
79
|
+
deps?: Array<string>;
|
|
80
|
+
// The scope under which the current shared dependencies are placed, the default is default
|
|
81
|
+
scope?: string | Array<string>;
|
|
82
|
+
};
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
- Example
|
|
86
|
+
|
|
87
|
+
```js
|
|
88
|
+
import { init, loadRemote } from '@module-federation/runtime';
|
|
89
|
+
|
|
90
|
+
init({
|
|
91
|
+
name: '@demo/main-app',
|
|
92
|
+
remotes: [
|
|
93
|
+
{
|
|
94
|
+
name: '@demo/app2',
|
|
95
|
+
entry: 'http://localhost:3006/remoteEntry.js',
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
name: '@demo/app3',
|
|
99
|
+
alias: 'app3',
|
|
100
|
+
entry: 'http://localhost:2001/module-federation-manifest.json',
|
|
101
|
+
},
|
|
102
|
+
],
|
|
103
|
+
});
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### loadRemote
|
|
107
|
+
|
|
108
|
+
- Type: `loadRemote(id: string)`
|
|
109
|
+
- 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.
|
|
110
|
+
|
|
111
|
+
- Example
|
|
112
|
+
|
|
113
|
+
```javascript
|
|
114
|
+
import { init, loadRemote } from '@module-federation/runtime';
|
|
115
|
+
|
|
116
|
+
init({
|
|
117
|
+
name: '@demo/main-app',
|
|
118
|
+
remotes: [
|
|
119
|
+
{
|
|
120
|
+
name: '@demo/app3',
|
|
121
|
+
alias: 'app3',
|
|
122
|
+
entry: 'http://localhost:2001/module-federation-manifest.json',
|
|
123
|
+
},
|
|
124
|
+
],
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// remoteName + expose
|
|
128
|
+
loadRemote('@demo/app3/util').then((m) => m.add(1, 2, 3));
|
|
129
|
+
|
|
130
|
+
// alias + expose
|
|
131
|
+
loadRemote('app3/util').then((m) => m.add(1, 2, 3));
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### loadShare
|
|
135
|
+
|
|
136
|
+
- Type: `loadShare(pkgName: string)`
|
|
137
|
+
- 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.
|
|
138
|
+
- This `API` is generally not called directly by users, but is used by build plugins to convert their own dependencies.
|
|
139
|
+
|
|
140
|
+
- Example
|
|
141
|
+
|
|
142
|
+
```js
|
|
143
|
+
import { init, loadRemote, loadShare } from '@module-federation/runtime';
|
|
144
|
+
import React from 'react';
|
|
145
|
+
import ReactDOM from 'react-dom';
|
|
146
|
+
|
|
147
|
+
init({
|
|
148
|
+
name: '@demo/main-app',
|
|
149
|
+
remotes: [],
|
|
150
|
+
shared: {
|
|
151
|
+
react: {
|
|
152
|
+
version: '17.0.0',
|
|
153
|
+
scope: 'default',
|
|
154
|
+
lib: () => React,
|
|
155
|
+
shareConfig: {
|
|
156
|
+
singleton: true,
|
|
157
|
+
requiredVersion: '^17.0.0',
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
'react-dom': {
|
|
161
|
+
version: '17.0.0',
|
|
162
|
+
scope: 'default',
|
|
163
|
+
lib: () => ReactDOM,
|
|
164
|
+
shareConfig: {
|
|
165
|
+
singleton: true,
|
|
166
|
+
requiredVersion: '^17.0.0',
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
loadShare('react').then((reactFactory) => {
|
|
173
|
+
console.log(reactFactory());
|
|
174
|
+
});
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### usePlugin
|
|
178
|
+
|
|
179
|
+
Used to extend the internal loading process of `ModuleFederation`, affecting the entire loading process through hook triggers and return values.
|
|
180
|
+
|
|
181
|
+
- Example
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
import { init } from '@module-federation/runtime';
|
|
185
|
+
|
|
186
|
+
// mock get remote data remotes config
|
|
187
|
+
function getDataConfig() {
|
|
188
|
+
return new Promise((resolve) => {
|
|
189
|
+
setTimeout(() => {
|
|
190
|
+
resolve({
|
|
191
|
+
remotes: [
|
|
192
|
+
{
|
|
193
|
+
name: '@demo/sub',
|
|
194
|
+
alias: 'sub',
|
|
195
|
+
entry: 'http://localhost:2001/module-federation-manifest.json',
|
|
196
|
+
},
|
|
197
|
+
],
|
|
198
|
+
});
|
|
199
|
+
}, 2000);
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function RemotesDataPlugin() {
|
|
204
|
+
return {
|
|
205
|
+
name: 'data-config',
|
|
206
|
+
async beforeRequest(args) {
|
|
207
|
+
const remotes = await getDataConfig();
|
|
208
|
+
origin.initOptions({
|
|
209
|
+
remotes,
|
|
210
|
+
});
|
|
211
|
+
return args;
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
init({
|
|
217
|
+
name: '@demo/micro-app',
|
|
218
|
+
remotes: [],
|
|
219
|
+
pluigns: [RemotesDataPlugin()],
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
loadRemote('sub/utils').then((m) => {
|
|
223
|
+
m.add(1, 2, 3, 4);
|
|
224
|
+
});
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
- Type
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
type Plugin = {
|
|
231
|
+
name: string;
|
|
232
|
+
core: {
|
|
233
|
+
beforeRequest: AsyncWaterfallHook<{
|
|
234
|
+
id: string;
|
|
235
|
+
options: Options;
|
|
236
|
+
origin: VmokHost;
|
|
237
|
+
}>;
|
|
238
|
+
};
|
|
239
|
+
snapshot: {};
|
|
240
|
+
};
|
|
241
|
+
type usePlugin = (plugin: Plugin) => void;
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### preloadRemote
|
|
245
|
+
|
|
246
|
+
- Type
|
|
247
|
+
|
|
248
|
+
```typescript
|
|
249
|
+
async function preloadRemote(preloadOptions: Array<PreloadRemoteArgs>) {}
|
|
250
|
+
|
|
251
|
+
type depsPreloadArg = Omit<PreloadRemoteArgs, 'depsRemote'>;
|
|
252
|
+
type PreloadRemoteArgs = {
|
|
253
|
+
// Preload the name and alias of the remote
|
|
254
|
+
nameOrAlias: string;
|
|
255
|
+
// Expose to be preloaded
|
|
256
|
+
// Default to preload all exposes
|
|
257
|
+
// Only the required expose will be loaded when an expose is provided
|
|
258
|
+
exposes?: Array<string>; // Default request
|
|
259
|
+
// Default is sync, only load synchronous code referenced in the expose
|
|
260
|
+
// Set to all to load synchronous and asynchronous references
|
|
261
|
+
resourceCategory?: 'all' | 'sync';
|
|
262
|
+
// Load all dependencies when no value is configured
|
|
263
|
+
// Only the resources needed will be loaded after configuring the dependency
|
|
264
|
+
depsRemote?: boolean | Array<depsPreloadArg>;
|
|
265
|
+
// No filtering of resources when not configured
|
|
266
|
+
// Will filter out unwanted resources after configuring
|
|
267
|
+
filter?: (assetUrl: string) => boolean;
|
|
268
|
+
};
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
- Details
|
|
272
|
+
|
|
273
|
+
Through `preloadRemote`, module resources can be preloaded at an earlier stage to avoid waterfall requests. `preloadRemote` can preload the following content:
|
|
274
|
+
|
|
275
|
+
- The `remoteEntry` of `remote`
|
|
276
|
+
- `expose` of `remote`
|
|
277
|
+
- Synchronous resources or asynchronous resources of `remote`
|
|
278
|
+
- `remote` resources that `remote` depends on
|
|
279
|
+
|
|
280
|
+
* Example
|
|
281
|
+
|
|
282
|
+
```ts
|
|
283
|
+
import { init, preloadRemote } from '@module-federation/runtime';
|
|
284
|
+
init({
|
|
285
|
+
name: '@demo/preload-remote',
|
|
286
|
+
remotes: [
|
|
287
|
+
{
|
|
288
|
+
name: '@demo/sub1',
|
|
289
|
+
entry: 'http://localhost:2001/vmok-manifest.json',
|
|
290
|
+
},
|
|
291
|
+
{
|
|
292
|
+
name: '@demo/sub2',
|
|
293
|
+
entry: 'http://localhost:2001/vmok-manifest.json',
|
|
294
|
+
},
|
|
295
|
+
{
|
|
296
|
+
name: '@demo/sub3',
|
|
297
|
+
entry: 'http://localhost:2001/vmok-manifest.json',
|
|
298
|
+
},
|
|
299
|
+
],
|
|
300
|
+
});
|
|
301
|
+
```
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
+
|
|
3
|
+
exports[`preload-remote inBrowser > 1 preload with default config 1`] = `
|
|
4
|
+
{
|
|
5
|
+
"links": [
|
|
6
|
+
{
|
|
7
|
+
"href": "http://localhost:1111/resources/preload/preload-resource/button.sync.css",
|
|
8
|
+
"type": "style",
|
|
9
|
+
},
|
|
10
|
+
],
|
|
11
|
+
"scripts": [
|
|
12
|
+
{
|
|
13
|
+
"crossorigin": "",
|
|
14
|
+
"src": "http://localhost:1111/resources/preload/preload-resource/federation-remote-entry.js",
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"crossorigin": "",
|
|
18
|
+
"src": "http://localhost:1111/resources/preload/preload-resource/sub1-button/federation-remote-entry.js",
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"crossorigin": "",
|
|
22
|
+
"src": "http://localhost:1111/resources/preload/preload-resource/button.sync.js",
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"crossorigin": "",
|
|
26
|
+
"src": "http://localhost:1111/resources/preload/preload-resource/sub1-button/button.sync.js",
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
}
|
|
30
|
+
`;
|
|
31
|
+
|
|
32
|
+
exports[`preload-remote inBrowser > 2 preload with all config 1`] = `
|
|
33
|
+
{
|
|
34
|
+
"links": [
|
|
35
|
+
{
|
|
36
|
+
"href": "http://localhost:1111/resources/preload/preload-resource/sub2/button.async.css",
|
|
37
|
+
"type": "style",
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"href": "http://localhost:1111/resources/preload/preload-resource/sub2/button.sync.css",
|
|
41
|
+
"type": "style",
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
"scripts": [
|
|
45
|
+
{
|
|
46
|
+
"crossorigin": "",
|
|
47
|
+
"src": "http://localhost:1111/resources/preload/preload-resource/sub2/federation-remote-entry.js",
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
"crossorigin": "",
|
|
51
|
+
"src": "http://localhost:1111/resources/preload/preload-resource/sub2-button/federation-remote-entry.js",
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"crossorigin": "",
|
|
55
|
+
"src": "http://localhost:1111/resources/preload/preload-resource/sub2/button.async.js",
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
"crossorigin": "",
|
|
59
|
+
"src": "http://localhost:1111/resources/preload/preload-resource/sub2/button.sync.js",
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
"crossorigin": "",
|
|
63
|
+
"src": "http://localhost:1111/resources/preload/preload-resource/sub2-button/button.async.js",
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
"crossorigin": "",
|
|
67
|
+
"src": "http://localhost:1111/resources/preload/preload-resource/sub2-button/button.sync.js",
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
"crossorigin": "",
|
|
71
|
+
"src": "http://localhost:1111/resources/preload/preload-resource/sub2-add/add.async.js",
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
"crossorigin": "",
|
|
75
|
+
"src": "http://localhost:1111/resources/preload/preload-resource/sub2-add/add.sync.js",
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
}
|
|
79
|
+
`;
|
|
80
|
+
|
|
81
|
+
exports[`preload-remote inBrowser > 3 preload with expose config 1`] = `
|
|
82
|
+
{
|
|
83
|
+
"links": [],
|
|
84
|
+
"scripts": [
|
|
85
|
+
{
|
|
86
|
+
"crossorigin": "",
|
|
87
|
+
"src": "http://localhost:1111/resources/preload/preload-resource/sub3/federation-remote-entry.js",
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
"crossorigin": "",
|
|
91
|
+
"src": "http://localhost:1111/resources/preload/preload-resource/sub3/add.sync.js",
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
}
|
|
95
|
+
`;
|
|
96
|
+
|
|
97
|
+
exports[`preload-remote inBrowser > 3 preload with expose config 2`] = `
|
|
98
|
+
{
|
|
99
|
+
"links": [],
|
|
100
|
+
"scripts": [
|
|
101
|
+
{
|
|
102
|
+
"crossorigin": "",
|
|
103
|
+
"src": "http://localhost:1111/resources/preload/preload-resource/sub3/federation-remote-entry.js",
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
"crossorigin": "",
|
|
107
|
+
"src": "http://localhost:1111/resources/preload/preload-resource/sub3/add.sync.js",
|
|
108
|
+
},
|
|
109
|
+
],
|
|
110
|
+
}
|
|
111
|
+
`;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { describe, it } from 'vitest';
|
|
2
|
+
import { init } from '../src';
|
|
3
|
+
|
|
4
|
+
// eslint-disable-next-line max-lines-per-function
|
|
5
|
+
describe('api', () => {
|
|
6
|
+
it('apis', () => {
|
|
7
|
+
const FM = init({
|
|
8
|
+
name: '@federation/name',
|
|
9
|
+
remotes: [],
|
|
10
|
+
});
|
|
11
|
+
expect(FM.loadShare).not.toBe(null);
|
|
12
|
+
expect(FM.loadRemote).not.toBe(null);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('init with same name', () => {
|
|
16
|
+
// get same instance
|
|
17
|
+
const FM1 = init({
|
|
18
|
+
name: '@federation/same-name',
|
|
19
|
+
remotes: [],
|
|
20
|
+
});
|
|
21
|
+
const FM2 = init({
|
|
22
|
+
name: '@federation/same-name',
|
|
23
|
+
remotes: [],
|
|
24
|
+
});
|
|
25
|
+
expect(FM1).toBe(FM2);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('init with same name with diffrent version', () => {
|
|
29
|
+
// get same instance
|
|
30
|
+
const FM1 = init({
|
|
31
|
+
name: '@federation/same-name-with-version',
|
|
32
|
+
version: '1.0.1',
|
|
33
|
+
remotes: [],
|
|
34
|
+
});
|
|
35
|
+
const FM2 = init({
|
|
36
|
+
name: '@federation/same-name-with-version',
|
|
37
|
+
version: '1.0.2',
|
|
38
|
+
remotes: [],
|
|
39
|
+
});
|
|
40
|
+
expect(FM1).not.toBe(FM2);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('init merge remotes', () => {
|
|
44
|
+
// get same instance
|
|
45
|
+
const FM1 = init({
|
|
46
|
+
name: '@federation/merge-remotes',
|
|
47
|
+
remotes: [
|
|
48
|
+
{
|
|
49
|
+
name: '@federation/sub2',
|
|
50
|
+
entry: 'xxx',
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
});
|
|
54
|
+
const FM2 = init({
|
|
55
|
+
name: '@federation/merge-remotes',
|
|
56
|
+
remotes: [
|
|
57
|
+
{
|
|
58
|
+
name: '@federation/sub3',
|
|
59
|
+
entry: 'xxx',
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
});
|
|
63
|
+
// merge remotes
|
|
64
|
+
expect(FM1.options.remotes).toEqual(
|
|
65
|
+
expect.arrayContaining([
|
|
66
|
+
{
|
|
67
|
+
name: '@federation/sub2',
|
|
68
|
+
entry: new URL('xxx', location.origin).href,
|
|
69
|
+
shareScope: 'default',
|
|
70
|
+
type: 'global',
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
name: '@federation/sub3',
|
|
74
|
+
entry: new URL('xxx', location.origin).href,
|
|
75
|
+
shareScope: 'default',
|
|
76
|
+
type: 'global',
|
|
77
|
+
},
|
|
78
|
+
]),
|
|
79
|
+
);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('init with diffrent same name', () => {
|
|
83
|
+
// get different instance
|
|
84
|
+
const FM3 = init({
|
|
85
|
+
name: '@federation/main3',
|
|
86
|
+
remotes: [],
|
|
87
|
+
});
|
|
88
|
+
const FM4 = init({
|
|
89
|
+
name: '@federation/main4',
|
|
90
|
+
remotes: [],
|
|
91
|
+
});
|
|
92
|
+
expect(FM3).not.toBe(FM4);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('alias check', () => {
|
|
96
|
+
// 校验 alias 是否等于 remote.name 和 remote.alias 的前缀,如果是则报错
|
|
97
|
+
// 因为引用支持多级路径的引用时无法保证名称是否唯一,所以不支持 alias 为 remote.name 的前缀
|
|
98
|
+
// 需要注意的是不要将 alias 和 name 的前缀相等,例如:
|
|
99
|
+
|
|
100
|
+
// ```js
|
|
101
|
+
// remotes: [
|
|
102
|
+
// {
|
|
103
|
+
// name: "@scope/button",
|
|
104
|
+
// version: "1.0.2"
|
|
105
|
+
// },
|
|
106
|
+
// {
|
|
107
|
+
// name: "@scope/component",
|
|
108
|
+
// alias: "@scope",
|
|
109
|
+
// version: "1.0.1"
|
|
110
|
+
// }
|
|
111
|
+
// ]
|
|
112
|
+
|
|
113
|
+
// 因为引用支持多级路径的引用,在使用 `@scope/button` 时内部无法判断是从 `"@scope/button"` 获取的还是从 `"@scope/component"` 获取的
|
|
114
|
+
expect(() => {
|
|
115
|
+
init({
|
|
116
|
+
name: '@federation/init-alias',
|
|
117
|
+
remotes: [
|
|
118
|
+
{
|
|
119
|
+
name: '@scope/button',
|
|
120
|
+
version: '1.0.2',
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
name: '@scope/component',
|
|
124
|
+
alias: '@scope',
|
|
125
|
+
version: '1.0.1',
|
|
126
|
+
},
|
|
127
|
+
],
|
|
128
|
+
});
|
|
129
|
+
}).toThrow(
|
|
130
|
+
/The alias @scope of remote @scope\/component is not allowed to be the prefix of @scope\/button name or alias/,
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
expect(() => {
|
|
134
|
+
init({
|
|
135
|
+
name: '@federation/init-alias1',
|
|
136
|
+
remotes: [
|
|
137
|
+
{
|
|
138
|
+
name: '@federation/button',
|
|
139
|
+
alias: '@scope/button',
|
|
140
|
+
version: '1.0.2',
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
name: '@scope/component',
|
|
144
|
+
alias: '@scope',
|
|
145
|
+
version: '1.0.1',
|
|
146
|
+
},
|
|
147
|
+
],
|
|
148
|
+
});
|
|
149
|
+
}).toThrow(
|
|
150
|
+
/The alias @scope of remote @scope\/component is not allowed to be the prefix of @federation\/button name or alias/,
|
|
151
|
+
);
|
|
152
|
+
});
|
|
153
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { assert, describe, test, it, vi } from 'vitest';
|
|
2
|
+
import { init } from '../src/index';
|
|
3
|
+
|
|
4
|
+
describe('global', () => {
|
|
5
|
+
it('inject mode', () => {
|
|
6
|
+
globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__ = vi.fn() as any;
|
|
7
|
+
const injectArgs = {
|
|
8
|
+
name: '@federation/inject-mode',
|
|
9
|
+
remotes: [],
|
|
10
|
+
};
|
|
11
|
+
const GM = init(injectArgs);
|
|
12
|
+
expect(GM.constructor).toBe(
|
|
13
|
+
globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__,
|
|
14
|
+
);
|
|
15
|
+
expect(globalThis.__FEDERATION__.__DEBUG_CONSTRUCTOR__).toBeCalledWith(
|
|
16
|
+
injectArgs,
|
|
17
|
+
);
|
|
18
|
+
});
|
|
19
|
+
});
|