@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.
Files changed (129) hide show
  1. package/.eslintrc.json +23 -0
  2. package/.swcrc +29 -0
  3. package/CHANGELOG.md +8 -0
  4. package/README.md +301 -0
  5. package/__tests__/__snapshots__/preload-remote.spec.ts.snap +111 -0
  6. package/__tests__/api.spec.ts +153 -0
  7. package/__tests__/globa.spec.ts +19 -0
  8. package/__tests__/hooks.spec.ts +269 -0
  9. package/__tests__/instance.spec.ts +12 -0
  10. package/__tests__/is-static-resources-equal.spec.ts +43 -0
  11. package/__tests__/load-remote.spec.ts +566 -0
  12. package/__tests__/mock/env.ts +1 -0
  13. package/__tests__/mock/handlers.ts +41 -0
  14. package/__tests__/mock/mock-script.ts +127 -0
  15. package/__tests__/mock/moduleInfo.ts +13 -0
  16. package/__tests__/mock/server.ts +4 -0
  17. package/__tests__/mock/utils.ts +128 -0
  18. package/__tests__/preload-remote.spec.ts +352 -0
  19. package/__tests__/resources/app2/federation-remote-entry.js +11 -0
  20. package/__tests__/resources/hooks/app2/federation-remote-entry.js +11 -0
  21. package/__tests__/resources/hooks/app3/federation-remote-entry.js +11 -0
  22. package/__tests__/resources/load-remote/app1/federation-remote-entry.js +11 -0
  23. package/__tests__/resources/load-remote/app2/federation-remote-entry.js +11 -0
  24. package/__tests__/resources/load-remote/app2/say.sync.js +1 -0
  25. package/__tests__/resources/load-remote/circulate-dep-app1/federation-manifest.json +51 -0
  26. package/__tests__/resources/load-remote/circulate-dep-app1/federation-remote-entry.js +13 -0
  27. package/__tests__/resources/load-remote/circulate-dep-app2/federation-manifest.json +51 -0
  28. package/__tests__/resources/load-remote/circulate-dep-app2/federation-remote-entry.js +34 -0
  29. package/__tests__/resources/load-remote/diff-instance/federation-remote-entry.js +16 -0
  30. package/__tests__/resources/main/federation-manifest.json +38 -0
  31. package/__tests__/resources/main/federation-remote-entry.js +14 -0
  32. package/__tests__/resources/preload/preload-resource/federation-remote-entry.js +16 -0
  33. package/__tests__/semver.spec.ts +228 -0
  34. package/__tests__/setup.ts +18 -0
  35. package/__tests__/share.ts +163 -0
  36. package/__tests__/shares.spec.ts +463 -0
  37. package/dist/LICENSE +21 -0
  38. package/{helpers.cjs.js → dist/helpers.cjs.js} +1 -1
  39. package/{helpers.esm.js → dist/helpers.esm.js} +2 -2
  40. package/{index.cjs.js → dist/index.cjs.js} +85 -1086
  41. package/{index.esm.js → dist/index.esm.js} +85 -1085
  42. package/dist/package.json +46 -0
  43. package/{share.cjs.js → dist/share.cjs.js} +46 -71
  44. package/{share.esm.js → dist/share.esm.js} +46 -71
  45. package/{src → dist/src}/core.d.ts +6 -15
  46. package/{src → dist/src}/global.d.ts +3 -2
  47. package/{src → dist/src}/helpers.d.ts +2 -2
  48. package/{src → dist/src}/index.d.ts +0 -1
  49. package/{src → dist/src}/plugins/snapshot/SnapshotHandler.d.ts +1 -0
  50. package/{src → dist/src}/type/config.d.ts +2 -5
  51. package/{src → dist/src}/utils/hooks/pluginSystem.d.ts +0 -1
  52. package/dist/src/utils/semver/constants.d.ts +10 -0
  53. package/dist/src/utils/share.d.ts +7 -0
  54. package/global.d.ts +3 -0
  55. package/jest.config.ts +30 -0
  56. package/package.json +21 -16
  57. package/project.json +76 -0
  58. package/rollup.config.js +31 -0
  59. package/src/constant.ts +2 -0
  60. package/src/core.md +110 -0
  61. package/src/core.ts +746 -0
  62. package/src/global.ts +286 -0
  63. package/src/helpers.ts +76 -0
  64. package/src/index.ts +66 -0
  65. package/src/module/index.ts +131 -0
  66. package/src/plugins/generate-preload-assets.ts +343 -0
  67. package/src/plugins/snapshot/SnapshotHandler.ts +378 -0
  68. package/src/plugins/snapshot/index.ts +71 -0
  69. package/src/type/config.ts +126 -0
  70. package/src/type/index.ts +3 -0
  71. package/src/type/plugin.ts +25 -0
  72. package/src/type/preload.ts +32 -0
  73. package/src/types.ts +1 -0
  74. package/src/utils/env.ts +17 -0
  75. package/src/utils/hooks/asyncHook.ts +29 -0
  76. package/src/utils/hooks/asyncWaterfallHooks.ts +53 -0
  77. package/src/utils/hooks/index.ts +6 -0
  78. package/src/utils/hooks/pluginSystem.ts +73 -0
  79. package/src/utils/hooks/syncHook.ts +48 -0
  80. package/src/utils/hooks/syncWaterfallHook.ts +54 -0
  81. package/src/utils/index.ts +5 -0
  82. package/src/utils/load.ts +112 -0
  83. package/src/utils/logger.ts +16 -0
  84. package/src/utils/manifest.ts +81 -0
  85. package/src/utils/plugin.ts +31 -0
  86. package/src/utils/preload.ts +141 -0
  87. package/src/utils/semver/compare.ts +116 -0
  88. package/src/utils/semver/constants.ts +31 -0
  89. package/src/utils/semver/index.ts +143 -0
  90. package/src/utils/semver/parser.ts +240 -0
  91. package/src/utils/semver/utils.ts +76 -0
  92. package/src/utils/share.ts +192 -0
  93. package/src/utils/tool.ts +75 -0
  94. package/tsconfig.json +23 -0
  95. package/tsconfig.lib.json +10 -0
  96. package/tsconfig.spec.json +14 -0
  97. package/vitest.config.ts +19 -0
  98. package/src/utils/semver/constants.d.ts +0 -10
  99. package/src/utils/share.d.ts +0 -7
  100. /package/{helpers.cjs.d.ts → dist/helpers.cjs.d.ts} +0 -0
  101. /package/{index.cjs.d.ts → dist/index.cjs.d.ts} +0 -0
  102. /package/{src → dist/src}/constant.d.ts +0 -0
  103. /package/{src → dist/src}/module/index.d.ts +0 -0
  104. /package/{src → dist/src}/plugins/generate-preload-assets.d.ts +0 -0
  105. /package/{src → dist/src}/plugins/snapshot/index.d.ts +0 -0
  106. /package/{src → dist/src}/type/index.d.ts +0 -0
  107. /package/{src → dist/src}/type/plugin.d.ts +0 -0
  108. /package/{src → dist/src}/type/preload.d.ts +0 -0
  109. /package/{src → dist/src}/types.d.ts +0 -0
  110. /package/{src → dist/src}/utils/env.d.ts +0 -0
  111. /package/{src → dist/src}/utils/hooks/asyncHook.d.ts +0 -0
  112. /package/{src → dist/src}/utils/hooks/asyncWaterfallHooks.d.ts +0 -0
  113. /package/{src → dist/src}/utils/hooks/index.d.ts +0 -0
  114. /package/{src → dist/src}/utils/hooks/syncHook.d.ts +0 -0
  115. /package/{src → dist/src}/utils/hooks/syncWaterfallHook.d.ts +0 -0
  116. /package/{src → dist/src}/utils/index.d.ts +0 -0
  117. /package/{src → dist/src}/utils/load.d.ts +0 -0
  118. /package/{src → dist/src}/utils/logger.d.ts +0 -0
  119. /package/{src → dist/src}/utils/manifest.d.ts +0 -0
  120. /package/{src → dist/src}/utils/plugin.d.ts +0 -0
  121. /package/{src → dist/src}/utils/preload.d.ts +0 -0
  122. /package/{src → dist/src}/utils/semver/compare.d.ts +0 -0
  123. /package/{src → dist/src}/utils/semver/index.d.ts +0 -0
  124. /package/{src → dist/src}/utils/semver/parser.d.ts +0 -0
  125. /package/{src → dist/src}/utils/semver/utils.d.ts +0 -0
  126. /package/{src → dist/src}/utils/tool.d.ts +0 -0
  127. /package/{type.cjs.d.ts → dist/type.cjs.d.ts} +0 -0
  128. /package/{type.cjs.js → dist/type.cjs.js} +0 -0
  129. /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
@@ -0,0 +1,8 @@
1
+ # @module-federation/runtime
2
+
3
+ ## 0.0.0-next-20231219093556
4
+
5
+ ### Patch Changes
6
+
7
+ - 7d3f3d9: fix: add runtime api
8
+ - @module-federation/sdk@0.0.0-next-20231219093556
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
+ });