@module-federation/data-prefetch 0.0.0-next-20241010040057 → 0.0.0-next-20241010084324

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 (87) hide show
  1. package/CHANGELOG.md +4 -29
  2. package/LICENSE +1 -1
  3. package/dist/{src/cli → cli}/babel.d.ts +4 -2
  4. package/dist/cli/babel.js +84 -0
  5. package/dist/{src/cli → cli}/index.d.ts +8 -5
  6. package/dist/cli/index.js +207 -0
  7. package/dist/esm/chunk-AJPO2B2T.js +30 -0
  8. package/dist/esm/chunk-BGNOVRH5.js +166 -0
  9. package/dist/esm/chunk-EZUCZHGV.js +11 -0
  10. package/dist/esm/chunk-ISF7VMBW.js +156 -0
  11. package/dist/esm/chunk-OCDMSRMZ.js +32 -0
  12. package/dist/esm/chunk-TTJJJ2WZ.js +7 -0
  13. package/dist/esm/cli/babel.js +57 -0
  14. package/dist/esm/cli/index.js +180 -0
  15. package/dist/esm/index.js +13 -0
  16. package/dist/esm/plugin.js +12 -0
  17. package/dist/esm/react/index.js +94 -0
  18. package/dist/esm/shared/index.js +27 -0
  19. package/dist/esm/universal/index.js +9 -0
  20. package/dist/index.d.ts +5 -0
  21. package/dist/index.js +351 -0
  22. package/dist/plugin.d.ts +5 -0
  23. package/dist/plugin.js +347 -0
  24. package/dist/{src/prefetch.d.ts → prefetch-c08be471.d.ts} +6 -4
  25. package/dist/react/index.d.ts +16 -0
  26. package/dist/react/index.js +287 -0
  27. package/dist/shared/index.d.ts +5 -0
  28. package/dist/shared/index.js +48 -0
  29. package/dist/universal/index.d.ts +8 -0
  30. package/dist/universal/index.js +205 -0
  31. package/package.json +30 -33
  32. package/project.json +6 -48
  33. package/src/cli/index.ts +2 -2
  34. package/tsconfig.json +1 -1
  35. package/.swcrc +0 -29
  36. package/dist/LICENSE +0 -21
  37. package/dist/babel.cjs.d.ts +0 -2
  38. package/dist/babel.cjs.js +0 -50
  39. package/dist/babel.esm.d.ts +0 -2
  40. package/dist/babel.esm.js +0 -48
  41. package/dist/cli.cjs.d.ts +0 -1
  42. package/dist/cli.cjs.js +0 -155
  43. package/dist/cli.esm.d.ts +0 -1
  44. package/dist/cli.esm.js +0 -152
  45. package/dist/index.cjs.d.ts +0 -1
  46. package/dist/index.cjs.js +0 -13
  47. package/dist/index.cjs2.js +0 -7
  48. package/dist/index.esm.d.ts +0 -1
  49. package/dist/index.esm.js +0 -6
  50. package/dist/index.esm2.js +0 -5
  51. package/dist/package.json +0 -100
  52. package/dist/plugin.cjs.d.ts +0 -2
  53. package/dist/plugin.cjs.js +0 -159
  54. package/dist/plugin.esm.d.ts +0 -2
  55. package/dist/plugin.esm.js +0 -154
  56. package/dist/polyfills.cjs.js +0 -14
  57. package/dist/polyfills.esm.js +0 -12
  58. package/dist/prefetch.cjs.js +0 -142
  59. package/dist/prefetch.esm.js +0 -140
  60. package/dist/react.cjs.d.ts +0 -1
  61. package/dist/react.cjs.js +0 -77
  62. package/dist/react.esm.d.ts +0 -1
  63. package/dist/react.esm.js +0 -75
  64. package/dist/runtime-utils.cjs.js +0 -27
  65. package/dist/runtime-utils.esm.js +0 -22
  66. package/dist/shared.cjs.d.ts +0 -2
  67. package/dist/shared.cjs.js +0 -24
  68. package/dist/shared.esm.d.ts +0 -2
  69. package/dist/shared.esm.js +0 -22
  70. package/dist/src/common/constant.d.ts +0 -1
  71. package/dist/src/common/index.d.ts +0 -1
  72. package/dist/src/common/node-utils.d.ts +0 -2
  73. package/dist/src/common/runtime-utils.d.ts +0 -5
  74. package/dist/src/index.d.ts +0 -2
  75. package/dist/src/logger/index.d.ts +0 -3
  76. package/dist/src/plugin.d.ts +0 -3
  77. package/dist/src/react/hooks.d.ts +0 -11
  78. package/dist/src/react/index.d.ts +0 -1
  79. package/dist/src/react/utils.d.ts +0 -1
  80. package/dist/src/shared/index.d.ts +0 -3
  81. package/dist/src/universal/index.d.ts +0 -2
  82. package/dist/universal.cjs.d.ts +0 -1
  83. package/dist/universal.cjs.js +0 -29
  84. package/dist/universal.esm.d.ts +0 -1
  85. package/dist/universal.esm.js +0 -27
  86. package/rollup.config.js +0 -46
  87. package/tsconfig.lib.json +0 -10
package/dist/cli.esm.js DELETED
@@ -1,152 +0,0 @@
1
- import path from 'path';
2
- import fs from 'fs-extra';
3
- import { encodeName, MFPrefetchCommon } from '@module-federation/sdk';
4
- import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
5
- import { b as getPrefetchId } from './runtime-utils.esm.js';
6
- import '@module-federation/runtime';
7
-
8
- const TEMP_DIR = '.mf';
9
-
10
- const fileExistsWithCaseSync = (filepath)=>{
11
- const dir = path.dirname(filepath);
12
- if (filepath === '/' || filepath === '.') {
13
- return true;
14
- }
15
- const filenames = fs.readdirSync(dir);
16
- if (filenames.indexOf(path.basename(filepath)) === -1) {
17
- return false;
18
- }
19
- return fileExistsWithCaseSync(dir);
20
- };
21
- const fixPrefetchPath = (exposePath)=>{
22
- const pathExt = [
23
- '.js',
24
- '.ts'
25
- ];
26
- const extReg = /\.(ts|js|tsx|jsx)$/;
27
- if (extReg.test(exposePath)) {
28
- return pathExt.map((ext)=>exposePath.replace(extReg, `.prefetch${ext}`));
29
- } else {
30
- return pathExt.map((ext)=>exposePath + `.prefetch${ext}`);
31
- }
32
- };
33
-
34
- const { RuntimeGlobals, Template } = require(normalizeWebpackPath('webpack'));
35
- function getFederationGlobalScope(runtimeGlobals) {
36
- return `${runtimeGlobals.require || '__webpack_require__'}.federation`;
37
- }
38
- class PrefetchPlugin {
39
- // eslint-disable-next-line max-lines-per-function
40
- apply(compiler) {
41
- var _this_options_runtimePlugins, _this_options_runtimePlugins1;
42
- const { name, exposes } = this.options;
43
- if (!exposes) {
44
- return;
45
- }
46
- if (!compiler.options.context) {
47
- throw new Error('compiler.options.context is not defined');
48
- }
49
- const { runtimePlugins } = this.options;
50
- if (!Array.isArray(runtimePlugins)) {
51
- this.options.runtimePlugins = [];
52
- }
53
- const runtimePath = path.resolve(__dirname, './plugin.esm.js');
54
- const sharedPath = path.resolve(__dirname, './shared.esm.js');
55
- if (!((_this_options_runtimePlugins = this.options.runtimePlugins) == null ? void 0 : _this_options_runtimePlugins.includes(runtimePath))) {
56
- this.options.runtimePlugins.push(runtimePath);
57
- }
58
- if (!((_this_options_runtimePlugins1 = this.options.runtimePlugins) == null ? void 0 : _this_options_runtimePlugins1.includes(sharedPath))) {
59
- this.options.runtimePlugins.push(sharedPath);
60
- }
61
- const encodedName = encodeName(name);
62
- const asyncEntryPath = path.resolve(compiler.options.context, `node_modules/${TEMP_DIR}/${encodedName}/bootstrap.js`);
63
- if (fs.existsSync(asyncEntryPath)) {
64
- fs.unlinkSync(asyncEntryPath);
65
- }
66
- if (!this.options.dataPrefetch) {
67
- return;
68
- }
69
- const exposeAlias = Object.keys(exposes);
70
- exposeAlias.forEach((alias)=>{
71
- let exposePath;
72
- // @ts-ignore
73
- const exposeValue = exposes[alias];
74
- if (typeof exposeValue === 'string') {
75
- exposePath = exposeValue;
76
- } else {
77
- exposePath = exposeValue.import[0];
78
- }
79
- const targetPaths = fixPrefetchPath(exposePath);
80
- for (const pathItem of targetPaths){
81
- const absolutePath = path.resolve(compiler.options.context, pathItem);
82
- if (fileExistsWithCaseSync(absolutePath)) {
83
- const absoluteAlias = alias.replace('.', '');
84
- this._reWriteExports += `export * as ${getPrefetchId(`${name}${absoluteAlias}`)} from '${absolutePath}';\n`;
85
- break;
86
- }
87
- }
88
- });
89
- if (!this._reWriteExports) {
90
- return;
91
- }
92
- const tempDirRealPath = path.resolve(compiler.options.context, 'node_modules', TEMP_DIR);
93
- if (!fs.existsSync(tempDirRealPath)) {
94
- fs.mkdirSync(tempDirRealPath);
95
- }
96
- if (!fs.existsSync(`${tempDirRealPath}/${encodedName}`)) {
97
- fs.mkdirSync(`${tempDirRealPath}/${encodedName}`);
98
- }
99
- fs.writeFileSync(asyncEntryPath, this._reWriteExports);
100
- new compiler.webpack.DefinePlugin({
101
- FederationDataPrefetch: JSON.stringify(asyncEntryPath)
102
- }).apply(compiler);
103
- }
104
- static addRuntime(compiler, options) {
105
- const encodedName = encodeName(options.name);
106
- if (!compiler.options.context) {
107
- throw new Error('compiler.options.context is not defined');
108
- }
109
- const prefetchEntry = path.resolve(compiler.options.context, `node_modules/.mf/${encodedName}/bootstrap.js`);
110
- const federationGlobal = getFederationGlobalScope(RuntimeGlobals || {});
111
- return Template.asString([
112
- fs.existsSync(prefetchEntry) ? Template.indent([
113
- 'function injectPrefetch() {',
114
- Template.indent([
115
- `globalThis.__FEDERATION__ = globalThis.__FEDERATION__ || {};`,
116
- `globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}'] = globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}'] || {`,
117
- `entryLoading: {},`,
118
- `instance: new Map(),`,
119
- `__PREFETCH_EXPORTS__: {},`,
120
- `};`,
121
- `globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}']['${MFPrefetchCommon.exportsKey}'] = globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}']['${MFPrefetchCommon.exportsKey}'] || {};`,
122
- `globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}']['${MFPrefetchCommon.exportsKey}']['${options.name}'] = function(){ return import('${prefetchEntry}');}`
123
- ]),
124
- '}',
125
- `${federationGlobal}.prefetch = injectPrefetch`
126
- ]) : '',
127
- Template.indent([
128
- `if(!${federationGlobal}.isMFRemote && ${federationGlobal}.prefetch){`,
129
- `${federationGlobal}.prefetch()`,
130
- '}'
131
- ])
132
- ]);
133
- }
134
- static setRemoteIdentifier() {
135
- const federationGlobal = getFederationGlobalScope(RuntimeGlobals || {});
136
- return Template.indent([
137
- `${federationGlobal}.isMFRemote = true;`
138
- ]);
139
- }
140
- static removeRemoteIdentifier() {
141
- const federationGlobal = getFederationGlobalScope(RuntimeGlobals || {});
142
- return Template.indent([
143
- `${federationGlobal}.isMFRemote = false;`
144
- ]);
145
- }
146
- constructor(options){
147
- this.options = options;
148
- this._reWriteExports = '';
149
- }
150
- }
151
-
152
- export { PrefetchPlugin, getFederationGlobalScope };
@@ -1 +0,0 @@
1
- export * from "./src/index";
package/dist/index.cjs.js DELETED
@@ -1,13 +0,0 @@
1
- 'use strict';
2
-
3
- var prefetch = require('./prefetch.cjs.js');
4
- var plugin = require('./plugin.cjs.js');
5
- require('@module-federation/runtime');
6
- require('@module-federation/sdk');
7
- require('./runtime-utils.cjs.js');
8
- require('./index.cjs2.js');
9
-
10
-
11
-
12
- exports.MFDataPrefetch = prefetch.MFDataPrefetch;
13
- exports.prefetchPlugin = plugin.prefetchPlugin;
@@ -1,7 +0,0 @@
1
- 'use strict';
2
-
3
- var sdk = require('@module-federation/sdk');
4
-
5
- var logger = new sdk.Logger('[Module Federation Data Prefetch]');
6
-
7
- exports.logger = logger;
@@ -1 +0,0 @@
1
- export * from "./src/index";
package/dist/index.esm.js DELETED
@@ -1,6 +0,0 @@
1
- export { M as MFDataPrefetch } from './prefetch.esm.js';
2
- export { prefetchPlugin } from './plugin.esm.js';
3
- import '@module-federation/runtime';
4
- import '@module-federation/sdk';
5
- import './runtime-utils.esm.js';
6
- import './index.esm2.js';
@@ -1,5 +0,0 @@
1
- import { Logger } from '@module-federation/sdk';
2
-
3
- var logger = new Logger('[Module Federation Data Prefetch]');
4
-
5
- export { logger as l };
package/dist/package.json DELETED
@@ -1,100 +0,0 @@
1
- {
2
- "name": "@module-federation/data-prefetch",
3
- "description": "Module Federation Data Prefetch",
4
- "version": "0.6.9",
5
- "author": "nieyan <nyqykk@foxmail.com>",
6
- "homepage": "https://github.com/module-federation/core",
7
- "license": "MIT",
8
- "scripts": {
9
- "dev": "cross-env WATCH=true tsup",
10
- "build": "rm -rf dist && tsup",
11
- "test": "jest"
12
- },
13
- "publishConfig": {
14
- "access": "public"
15
- },
16
- "exports": {
17
- ".": {
18
- "import": "./dist/index.esm.js",
19
- "require": "./dist/index.cjs",
20
- "types": "./dist/index.cjs.d.ts"
21
- },
22
- "./react": {
23
- "import": "./dist/react.esm.js",
24
- "require": "./dist/react.cjs.js",
25
- "types": "./dist/react.cjs.d.ts"
26
- },
27
- "./cli": {
28
- "import": "./dist/cli.esm.js",
29
- "require": "./dist/cli.cjs.js",
30
- "types": "./dist/cli.cjs.d.ts"
31
- },
32
- "./babel-plugin": {
33
- "import": "./dist/babel.esm.js",
34
- "require": "./dist/babel.cjs.js",
35
- "types": "./dist/babel.cjs.d.ts"
36
- },
37
- "./universal": {
38
- "import": "./dist/universal.esm.js",
39
- "require": "./dist/universal.cjs.js",
40
- "types": "./dist/universal.cjs.d.ts"
41
- },
42
- "./shared": {
43
- "import": "./dist/shared.esm.js",
44
- "require": "./dist/shared.cjs.js",
45
- "types": "./dist/shared/.cjs.d.ts"
46
- }
47
- },
48
- "typesVersions": {
49
- "*": {
50
- ".": [
51
- "./dist/index.cjs.d.ts"
52
- ],
53
- "react": [
54
- "./dist/react.cjs.d.ts"
55
- ],
56
- "cli": [
57
- "./dist/cli.cjs.d.ts"
58
- ],
59
- "universal": [
60
- "./dist/universal.cjs.d.ts"
61
- ],
62
- "shared": [
63
- "./dist/shared.cjs.d.ts"
64
- ],
65
- "babel-plugin": [
66
- "./dist/babel.cjs.d.ts"
67
- ]
68
- }
69
- },
70
- "main": "./index.cjs.js",
71
- "module": "./index.esm.js",
72
- "peerDependencies": {
73
- "react": ">=16.9.0",
74
- "react-dom": ">=16.9.0"
75
- },
76
- "devDependencies": {
77
- "@testing-library/react-hooks": "^8.0.1",
78
- "@types/fs-extra": "9.0.6",
79
- "@types/jest": "^29.5.11",
80
- "@types/node": "^17.0.45",
81
- "@types/react": "~18.0.38",
82
- "esbuild-plugin-replace": "^1.4.0",
83
- "jest": "^29.7.0",
84
- "jest-environment-jsdom": "^29.7.0",
85
- "minimist": "^1.2.8",
86
- "react": "^18.3.1",
87
- "react-dom": "^18.3.1",
88
- "react-router": "^6.21.3",
89
- "react-test-renderer": "^18.2.0",
90
- "ts-jest": "29.0.1",
91
- "tsup": "6.2.0",
92
- "webpack": "5.75.0"
93
- },
94
- "dependencies": {
95
- "@module-federation/runtime": "workspace:*",
96
- "@module-federation/sdk": "workspace:*",
97
- "fs-extra": "9.1.0"
98
- },
99
- "types": "./index.esm.d.ts"
100
- }
@@ -1,2 +0,0 @@
1
- export * from "./src/plugin";
2
- export { default } from "./src/plugin";
@@ -1,159 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var sdk = require('@module-federation/sdk');
6
- var runtimeUtils = require('./runtime-utils.cjs.js');
7
- var prefetch = require('./prefetch.cjs.js');
8
- var index = require('./index.cjs2.js');
9
- require('@module-federation/runtime');
10
-
11
- const loadingArray = [];
12
- const strategy = 'loaded-first';
13
- let sharedFlag = strategy;
14
- // eslint-disable-next-line max-lines-per-function
15
- const prefetchPlugin = ()=>({
16
- name: 'data-prefetch-runtime-plugin',
17
- initContainer (options) {
18
- const { remoteSnapshot, remoteInfo, id, origin } = options;
19
- const snapshot = remoteSnapshot;
20
- const { name } = remoteInfo;
21
- const prefetchOptions = {
22
- name,
23
- remote: remoteInfo,
24
- origin,
25
- remoteSnapshot: snapshot
26
- };
27
- const signal = runtimeUtils.getSignalFromManifest(snapshot);
28
- if (!signal) {
29
- return options;
30
- }
31
- if (sharedFlag !== strategy) {
32
- throw new Error(`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`);
33
- }
34
- const instance = prefetch.MFDataPrefetch.getInstance(name) || new prefetch.MFDataPrefetch(prefetchOptions);
35
- let prefetchUrl;
36
- // @ts-expect-error
37
- if (snapshot.prefetchEntry) {
38
- // @ts-expect-error
39
- prefetchUrl = sdk.getResourceUrl(snapshot, snapshot.prefetchEntry);
40
- }
41
- const exist = loadingArray.find((loading)=>loading.id === id);
42
- if (exist) {
43
- return options;
44
- }
45
- const promise = instance.loadEntry(prefetchUrl).then(async ()=>{
46
- const projectExports = instance.getProjectExports();
47
- if (projectExports instanceof Promise) {
48
- await projectExports;
49
- }
50
- return Promise.resolve().then(()=>{
51
- const exports = instance.getExposeExports(id);
52
- index.logger.info(`1. Start Prefetch initContainer: ${id} - ${performance.now()}`);
53
- const result = Object.keys(exports).map((k)=>{
54
- const value = instance.prefetch({
55
- id,
56
- functionId: k
57
- });
58
- const functionId = k;
59
- return {
60
- value,
61
- functionId
62
- };
63
- });
64
- return result;
65
- });
66
- });
67
- loadingArray.push({
68
- id,
69
- promise
70
- });
71
- return options;
72
- },
73
- afterResolve (options) {
74
- const { remoteSnapshot, remoteInfo, id, origin } = options;
75
- const snapshot = remoteSnapshot;
76
- const { name } = remoteInfo;
77
- const prefetchOptions = {
78
- name,
79
- remote: remoteInfo,
80
- origin,
81
- remoteSnapshot: snapshot
82
- };
83
- const signal = runtimeUtils.getSignalFromManifest(snapshot);
84
- if (!signal) {
85
- return options;
86
- }
87
- const inited = loadingArray.some((info)=>info.id === id);
88
- if (!inited) {
89
- return options;
90
- }
91
- if (sharedFlag !== strategy) {
92
- throw new Error(`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`);
93
- }
94
- const instance = prefetch.MFDataPrefetch.getInstance(name) || new prefetch.MFDataPrefetch(prefetchOptions);
95
- let prefetchUrl;
96
- // @ts-expect-error
97
- if (snapshot.prefetchEntry) {
98
- // @ts-expect-error
99
- prefetchUrl = sdk.getResourceUrl(snapshot, snapshot.prefetchEntry);
100
- }
101
- const index$1 = loadingArray.findIndex((loading)=>loading.id === id);
102
- // clear cache
103
- if (index$1 !== -1) {
104
- loadingArray.splice(index$1, 1);
105
- }
106
- const promise = instance.loadEntry(prefetchUrl).then(async ()=>{
107
- const projectExports = instance.getProjectExports();
108
- if (projectExports instanceof Promise) {
109
- await projectExports;
110
- }
111
- return Promise.resolve().then(()=>{
112
- const exports = instance.getExposeExports(id);
113
- index.logger.info(`1. Start Prefetch afterResolve: ${id} - ${performance.now()}`);
114
- const result = Object.keys(exports).map((k)=>{
115
- const value = instance.prefetch({
116
- id,
117
- functionId: k
118
- });
119
- const functionId = k;
120
- return {
121
- value,
122
- functionId
123
- };
124
- });
125
- return result;
126
- });
127
- });
128
- loadingArray.push({
129
- id,
130
- promise
131
- });
132
- return options;
133
- },
134
- async onLoad (options) {
135
- var _loadingArray_find;
136
- const { remote, id } = options;
137
- const { name } = remote;
138
- const promise = (_loadingArray_find = loadingArray.find((loading)=>loading.id === id)) == null ? void 0 : _loadingArray_find.promise;
139
- if (promise) {
140
- const prefetch$1 = await promise;
141
- const prefetchValue = prefetch$1.map((result)=>result.value);
142
- await Promise.all(prefetchValue);
143
- const instance = prefetch.MFDataPrefetch.getInstance(name);
144
- prefetch$1.forEach((result)=>{
145
- const { value, functionId } = result;
146
- instance.memorize(id + functionId, value);
147
- });
148
- }
149
- return options;
150
- },
151
- beforeLoadShare (options) {
152
- const shareInfo = options.shareInfo;
153
- sharedFlag = (shareInfo == null ? void 0 : shareInfo.strategy) || sharedFlag;
154
- return options;
155
- }
156
- });
157
-
158
- exports.default = prefetchPlugin;
159
- exports.prefetchPlugin = prefetchPlugin;
@@ -1,2 +0,0 @@
1
- export * from "./src/plugin";
2
- export { default } from "./src/plugin";
@@ -1,154 +0,0 @@
1
- import { getResourceUrl } from '@module-federation/sdk';
2
- import { a as getSignalFromManifest } from './runtime-utils.esm.js';
3
- import { M as MFDataPrefetch } from './prefetch.esm.js';
4
- import { l as logger } from './index.esm2.js';
5
- import '@module-federation/runtime';
6
-
7
- const loadingArray = [];
8
- const strategy = 'loaded-first';
9
- let sharedFlag = strategy;
10
- // eslint-disable-next-line max-lines-per-function
11
- const prefetchPlugin = ()=>({
12
- name: 'data-prefetch-runtime-plugin',
13
- initContainer (options) {
14
- const { remoteSnapshot, remoteInfo, id, origin } = options;
15
- const snapshot = remoteSnapshot;
16
- const { name } = remoteInfo;
17
- const prefetchOptions = {
18
- name,
19
- remote: remoteInfo,
20
- origin,
21
- remoteSnapshot: snapshot
22
- };
23
- const signal = getSignalFromManifest(snapshot);
24
- if (!signal) {
25
- return options;
26
- }
27
- if (sharedFlag !== strategy) {
28
- throw new Error(`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`);
29
- }
30
- const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
31
- let prefetchUrl;
32
- // @ts-expect-error
33
- if (snapshot.prefetchEntry) {
34
- // @ts-expect-error
35
- prefetchUrl = getResourceUrl(snapshot, snapshot.prefetchEntry);
36
- }
37
- const exist = loadingArray.find((loading)=>loading.id === id);
38
- if (exist) {
39
- return options;
40
- }
41
- const promise = instance.loadEntry(prefetchUrl).then(async ()=>{
42
- const projectExports = instance.getProjectExports();
43
- if (projectExports instanceof Promise) {
44
- await projectExports;
45
- }
46
- return Promise.resolve().then(()=>{
47
- const exports = instance.getExposeExports(id);
48
- logger.info(`1. Start Prefetch initContainer: ${id} - ${performance.now()}`);
49
- const result = Object.keys(exports).map((k)=>{
50
- const value = instance.prefetch({
51
- id,
52
- functionId: k
53
- });
54
- const functionId = k;
55
- return {
56
- value,
57
- functionId
58
- };
59
- });
60
- return result;
61
- });
62
- });
63
- loadingArray.push({
64
- id,
65
- promise
66
- });
67
- return options;
68
- },
69
- afterResolve (options) {
70
- const { remoteSnapshot, remoteInfo, id, origin } = options;
71
- const snapshot = remoteSnapshot;
72
- const { name } = remoteInfo;
73
- const prefetchOptions = {
74
- name,
75
- remote: remoteInfo,
76
- origin,
77
- remoteSnapshot: snapshot
78
- };
79
- const signal = getSignalFromManifest(snapshot);
80
- if (!signal) {
81
- return options;
82
- }
83
- const inited = loadingArray.some((info)=>info.id === id);
84
- if (!inited) {
85
- return options;
86
- }
87
- if (sharedFlag !== strategy) {
88
- throw new Error(`[Module Federation Data Prefetch]: If you want to use data prefetch, the shared strategy must be 'loaded-first'`);
89
- }
90
- const instance = MFDataPrefetch.getInstance(name) || new MFDataPrefetch(prefetchOptions);
91
- let prefetchUrl;
92
- // @ts-expect-error
93
- if (snapshot.prefetchEntry) {
94
- // @ts-expect-error
95
- prefetchUrl = getResourceUrl(snapshot, snapshot.prefetchEntry);
96
- }
97
- const index = loadingArray.findIndex((loading)=>loading.id === id);
98
- // clear cache
99
- if (index !== -1) {
100
- loadingArray.splice(index, 1);
101
- }
102
- const promise = instance.loadEntry(prefetchUrl).then(async ()=>{
103
- const projectExports = instance.getProjectExports();
104
- if (projectExports instanceof Promise) {
105
- await projectExports;
106
- }
107
- return Promise.resolve().then(()=>{
108
- const exports = instance.getExposeExports(id);
109
- logger.info(`1. Start Prefetch afterResolve: ${id} - ${performance.now()}`);
110
- const result = Object.keys(exports).map((k)=>{
111
- const value = instance.prefetch({
112
- id,
113
- functionId: k
114
- });
115
- const functionId = k;
116
- return {
117
- value,
118
- functionId
119
- };
120
- });
121
- return result;
122
- });
123
- });
124
- loadingArray.push({
125
- id,
126
- promise
127
- });
128
- return options;
129
- },
130
- async onLoad (options) {
131
- var _loadingArray_find;
132
- const { remote, id } = options;
133
- const { name } = remote;
134
- const promise = (_loadingArray_find = loadingArray.find((loading)=>loading.id === id)) == null ? void 0 : _loadingArray_find.promise;
135
- if (promise) {
136
- const prefetch = await promise;
137
- const prefetchValue = prefetch.map((result)=>result.value);
138
- await Promise.all(prefetchValue);
139
- const instance = MFDataPrefetch.getInstance(name);
140
- prefetch.forEach((result)=>{
141
- const { value, functionId } = result;
142
- instance.memorize(id + functionId, value);
143
- });
144
- }
145
- return options;
146
- },
147
- beforeLoadShare (options) {
148
- const shareInfo = options.shareInfo;
149
- sharedFlag = (shareInfo == null ? void 0 : shareInfo.strategy) || sharedFlag;
150
- return options;
151
- }
152
- });
153
-
154
- export { prefetchPlugin as default, prefetchPlugin };
@@ -1,14 +0,0 @@
1
- 'use strict';
2
-
3
- function _extends() {
4
- _extends = Object.assign || function assign(target) {
5
- for(var i = 1; i < arguments.length; i++){
6
- var source = arguments[i];
7
- for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
8
- }
9
- return target;
10
- };
11
- return _extends.apply(this, arguments);
12
- }
13
-
14
- exports._extends = _extends;
@@ -1,12 +0,0 @@
1
- function _extends() {
2
- _extends = Object.assign || function assign(target) {
3
- for(var i = 1; i < arguments.length; i++){
4
- var source = arguments[i];
5
- for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
6
- }
7
- return target;
8
- };
9
- return _extends.apply(this, arguments);
10
- }
11
-
12
- export { _extends as _ };