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

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/.swcrc +29 -0
  2. package/CHANGELOG.md +12 -4
  3. package/LICENSE +1 -1
  4. package/dist/LICENSE +21 -0
  5. package/dist/babel.cjs.d.ts +2 -0
  6. package/dist/babel.cjs.js +50 -0
  7. package/dist/babel.esm.d.ts +2 -0
  8. package/dist/babel.esm.js +48 -0
  9. package/dist/cli.cjs.d.ts +1 -0
  10. package/dist/cli.cjs.js +155 -0
  11. package/dist/cli.esm.d.ts +1 -0
  12. package/dist/cli.esm.js +152 -0
  13. package/dist/index.cjs.d.ts +1 -0
  14. package/dist/index.cjs.js +13 -0
  15. package/dist/index.cjs2.js +7 -0
  16. package/dist/index.esm.d.ts +1 -0
  17. package/dist/index.esm.js +6 -0
  18. package/dist/index.esm2.js +5 -0
  19. package/dist/package.json +100 -0
  20. package/dist/plugin.cjs.d.ts +2 -0
  21. package/dist/plugin.cjs.js +159 -0
  22. package/dist/plugin.esm.d.ts +2 -0
  23. package/dist/plugin.esm.js +154 -0
  24. package/dist/polyfills.cjs.js +14 -0
  25. package/dist/polyfills.esm.js +12 -0
  26. package/dist/prefetch.cjs.js +142 -0
  27. package/dist/prefetch.esm.js +140 -0
  28. package/dist/react.cjs.d.ts +1 -0
  29. package/dist/react.cjs.js +77 -0
  30. package/dist/react.esm.d.ts +1 -0
  31. package/dist/react.esm.js +75 -0
  32. package/dist/runtime-utils.cjs.js +27 -0
  33. package/dist/runtime-utils.esm.js +22 -0
  34. package/dist/shared.cjs.d.ts +2 -0
  35. package/dist/shared.cjs.js +24 -0
  36. package/dist/shared.esm.d.ts +2 -0
  37. package/dist/shared.esm.js +22 -0
  38. package/dist/{cli → src/cli}/babel.d.ts +2 -4
  39. package/dist/{cli → src/cli}/index.d.ts +5 -8
  40. package/dist/src/common/constant.d.ts +1 -0
  41. package/dist/src/common/index.d.ts +1 -0
  42. package/dist/src/common/node-utils.d.ts +2 -0
  43. package/dist/src/common/runtime-utils.d.ts +5 -0
  44. package/dist/src/index.d.ts +2 -0
  45. package/dist/src/logger/index.d.ts +3 -0
  46. package/dist/src/plugin.d.ts +3 -0
  47. package/dist/{prefetch-c08be471.d.ts → src/prefetch.d.ts} +4 -6
  48. package/dist/src/react/hooks.d.ts +11 -0
  49. package/dist/src/react/index.d.ts +1 -0
  50. package/dist/src/react/utils.d.ts +1 -0
  51. package/dist/src/shared/index.d.ts +3 -0
  52. package/dist/src/universal/index.d.ts +2 -0
  53. package/dist/universal.cjs.d.ts +1 -0
  54. package/dist/universal.cjs.js +29 -0
  55. package/dist/universal.esm.d.ts +1 -0
  56. package/dist/universal.esm.js +27 -0
  57. package/package.json +33 -30
  58. package/project.json +48 -6
  59. package/rollup.config.js +46 -0
  60. package/src/cli/index.ts +2 -2
  61. package/tsconfig.json +1 -1
  62. package/tsconfig.lib.json +10 -0
  63. package/dist/cli/babel.js +0 -84
  64. package/dist/cli/index.js +0 -207
  65. package/dist/esm/chunk-AJPO2B2T.js +0 -30
  66. package/dist/esm/chunk-BGNOVRH5.js +0 -166
  67. package/dist/esm/chunk-EZUCZHGV.js +0 -11
  68. package/dist/esm/chunk-ISF7VMBW.js +0 -156
  69. package/dist/esm/chunk-OCDMSRMZ.js +0 -32
  70. package/dist/esm/chunk-TTJJJ2WZ.js +0 -7
  71. package/dist/esm/cli/babel.js +0 -57
  72. package/dist/esm/cli/index.js +0 -180
  73. package/dist/esm/index.js +0 -13
  74. package/dist/esm/plugin.js +0 -12
  75. package/dist/esm/react/index.js +0 -94
  76. package/dist/esm/shared/index.js +0 -27
  77. package/dist/esm/universal/index.js +0 -9
  78. package/dist/index.d.ts +0 -5
  79. package/dist/index.js +0 -351
  80. package/dist/plugin.d.ts +0 -5
  81. package/dist/plugin.js +0 -347
  82. package/dist/react/index.d.ts +0 -16
  83. package/dist/react/index.js +0 -287
  84. package/dist/shared/index.d.ts +0 -5
  85. package/dist/shared/index.js +0 -48
  86. package/dist/universal/index.d.ts +0 -8
  87. package/dist/universal/index.js +0 -205
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": true,
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 CHANGED
@@ -1,6 +1,15 @@
1
1
  # @module-federation/data-prefetch
2
2
 
3
- ## 0.0.0-next-20241010084324
3
+ ## 0.0.0-next-20241011105718
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [32db0ac]
8
+ - Updated dependencies [32db0ac]
9
+ - @module-federation/sdk@0.0.0-next-20241011105718
10
+ - @module-federation/runtime@0.0.0-next-20241011105718
11
+
12
+ ## 0.6.7
4
13
 
5
14
  ### Patch Changes
6
15
 
@@ -14,9 +23,8 @@
14
23
  - Updated dependencies [9e32644]
15
24
  - Updated dependencies [9e32644]
16
25
  - Updated dependencies [9e32644]
17
- - Updated dependencies [3082116]
18
- - @module-federation/runtime@0.0.0-next-20241010084324
19
- - @module-federation/sdk@0.0.0-next-20241010084324
26
+ - @module-federation/runtime@0.6.7
27
+ - @module-federation/sdk@0.6.7
20
28
 
21
29
  ## 0.6.6
22
30
 
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2020 ScriptedAlchemy LLC (Zack Jackson) Zhou Shaw (zhouxiao)
3
+ Copyright (c) 2024-present nieyan(nyqykk)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/dist/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-present nieyan(nyqykk)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ export * from "./src/cli/babel";
2
+ export { default } from "./src/cli/babel";
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ var path = require('path');
4
+
5
+ // no used now
6
+ const attribute = 'id';
7
+ const hookId = 'usePrefetch';
8
+ const importPackage = '@module-federation/data-prefetch/react';
9
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
10
+ var babel = ((babel, options)=>{
11
+ const t = babel.types;
12
+ let shouldHandle = false;
13
+ let scope = '';
14
+ const { name, exposes } = options;
15
+ if (!exposes) {
16
+ return {};
17
+ }
18
+ const exposesKey = Object.keys(exposes);
19
+ const processedExposes = exposesKey.map((expose)=>({
20
+ key: expose.replace('.', ''),
21
+ value: path.resolve(// @ts-ignore
22
+ typeof exposes[expose] === 'string' ? exposes[expose] : exposes[expose].import)
23
+ }));
24
+ return {
25
+ visitor: {
26
+ ImportDeclaration (nodePath, // biome-ignore lint/suspicious/noExplicitAny: <explanation>
27
+ state) {
28
+ const source = nodePath.node.source.value;
29
+ const { specifiers } = nodePath.node;
30
+ const { filename } = state.file.opts;
31
+ if (source === importPackage) {
32
+ shouldHandle = specifiers.some((specifier)=>specifier.imported && specifier.imported.name === hookId && processedExposes.find(// biome-ignore lint/suspicious/noAssignInExpressions: <explanation>
33
+ (expose)=>expose.value === filename && (scope = expose.key)));
34
+ }
35
+ },
36
+ CallExpression (nodePath) {
37
+ if (shouldHandle && t.isIdentifier(nodePath.node.callee, {
38
+ name: hookId
39
+ }) && nodePath.node.arguments.length > 0) {
40
+ const objectExpression = nodePath.node.arguments[0];
41
+ if (objectExpression && t.isObjectExpression(objectExpression) && !objectExpression.properties.find((p)=>p.key.name === attribute)) {
42
+ objectExpression.properties.push(t.objectProperty(t.identifier(attribute), t.stringLiteral(name + scope)));
43
+ }
44
+ }
45
+ }
46
+ }
47
+ };
48
+ });
49
+
50
+ module.exports = babel;
@@ -0,0 +1,2 @@
1
+ export * from "./src/cli/babel";
2
+ export { default } from "./src/cli/babel";
@@ -0,0 +1,48 @@
1
+ import path from 'path';
2
+
3
+ // no used now
4
+ const attribute = 'id';
5
+ const hookId = 'usePrefetch';
6
+ const importPackage = '@module-federation/data-prefetch/react';
7
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
8
+ var babel = ((babel, options)=>{
9
+ const t = babel.types;
10
+ let shouldHandle = false;
11
+ let scope = '';
12
+ const { name, exposes } = options;
13
+ if (!exposes) {
14
+ return {};
15
+ }
16
+ const exposesKey = Object.keys(exposes);
17
+ const processedExposes = exposesKey.map((expose)=>({
18
+ key: expose.replace('.', ''),
19
+ value: path.resolve(// @ts-ignore
20
+ typeof exposes[expose] === 'string' ? exposes[expose] : exposes[expose].import)
21
+ }));
22
+ return {
23
+ visitor: {
24
+ ImportDeclaration (nodePath, // biome-ignore lint/suspicious/noExplicitAny: <explanation>
25
+ state) {
26
+ const source = nodePath.node.source.value;
27
+ const { specifiers } = nodePath.node;
28
+ const { filename } = state.file.opts;
29
+ if (source === importPackage) {
30
+ shouldHandle = specifiers.some((specifier)=>specifier.imported && specifier.imported.name === hookId && processedExposes.find(// biome-ignore lint/suspicious/noAssignInExpressions: <explanation>
31
+ (expose)=>expose.value === filename && (scope = expose.key)));
32
+ }
33
+ },
34
+ CallExpression (nodePath) {
35
+ if (shouldHandle && t.isIdentifier(nodePath.node.callee, {
36
+ name: hookId
37
+ }) && nodePath.node.arguments.length > 0) {
38
+ const objectExpression = nodePath.node.arguments[0];
39
+ if (objectExpression && t.isObjectExpression(objectExpression) && !objectExpression.properties.find((p)=>p.key.name === attribute)) {
40
+ objectExpression.properties.push(t.objectProperty(t.identifier(attribute), t.stringLiteral(name + scope)));
41
+ }
42
+ }
43
+ }
44
+ }
45
+ };
46
+ });
47
+
48
+ export { babel as default };
@@ -0,0 +1 @@
1
+ export * from "./src/cli/index";
@@ -0,0 +1,155 @@
1
+ 'use strict';
2
+
3
+ var path = require('path');
4
+ var fs = require('fs-extra');
5
+ var sdk = require('@module-federation/sdk');
6
+ var normalizeWebpackPath = require('@module-federation/sdk/normalize-webpack-path');
7
+ var runtimeUtils = require('./runtime-utils.cjs.js');
8
+ require('@module-federation/runtime');
9
+
10
+ const TEMP_DIR = '.mf';
11
+
12
+ const fileExistsWithCaseSync = (filepath)=>{
13
+ const dir = path.dirname(filepath);
14
+ if (filepath === '/' || filepath === '.') {
15
+ return true;
16
+ }
17
+ const filenames = fs.readdirSync(dir);
18
+ if (filenames.indexOf(path.basename(filepath)) === -1) {
19
+ return false;
20
+ }
21
+ return fileExistsWithCaseSync(dir);
22
+ };
23
+ const fixPrefetchPath = (exposePath)=>{
24
+ const pathExt = [
25
+ '.js',
26
+ '.ts'
27
+ ];
28
+ const extReg = /\.(ts|js|tsx|jsx)$/;
29
+ if (extReg.test(exposePath)) {
30
+ return pathExt.map((ext)=>exposePath.replace(extReg, `.prefetch${ext}`));
31
+ } else {
32
+ return pathExt.map((ext)=>exposePath + `.prefetch${ext}`);
33
+ }
34
+ };
35
+
36
+ const { RuntimeGlobals, Template } = require(normalizeWebpackPath.normalizeWebpackPath('webpack'));
37
+ function getFederationGlobalScope(runtimeGlobals) {
38
+ return `${runtimeGlobals.require || '__webpack_require__'}.federation`;
39
+ }
40
+ class PrefetchPlugin {
41
+ // eslint-disable-next-line max-lines-per-function
42
+ apply(compiler) {
43
+ var _this_options_runtimePlugins, _this_options_runtimePlugins1;
44
+ const { name, exposes } = this.options;
45
+ if (!exposes) {
46
+ return;
47
+ }
48
+ if (!compiler.options.context) {
49
+ throw new Error('compiler.options.context is not defined');
50
+ }
51
+ const { runtimePlugins } = this.options;
52
+ if (!Array.isArray(runtimePlugins)) {
53
+ this.options.runtimePlugins = [];
54
+ }
55
+ const runtimePath = path.resolve(__dirname, './plugin.esm.js');
56
+ const sharedPath = path.resolve(__dirname, './shared.esm.js');
57
+ if (!((_this_options_runtimePlugins = this.options.runtimePlugins) == null ? void 0 : _this_options_runtimePlugins.includes(runtimePath))) {
58
+ this.options.runtimePlugins.push(runtimePath);
59
+ }
60
+ if (!((_this_options_runtimePlugins1 = this.options.runtimePlugins) == null ? void 0 : _this_options_runtimePlugins1.includes(sharedPath))) {
61
+ this.options.runtimePlugins.push(sharedPath);
62
+ }
63
+ const encodedName = sdk.encodeName(name);
64
+ const asyncEntryPath = path.resolve(compiler.options.context, `node_modules/${TEMP_DIR}/${encodedName}/bootstrap.js`);
65
+ if (fs.existsSync(asyncEntryPath)) {
66
+ fs.unlinkSync(asyncEntryPath);
67
+ }
68
+ if (!this.options.dataPrefetch) {
69
+ return;
70
+ }
71
+ const exposeAlias = Object.keys(exposes);
72
+ exposeAlias.forEach((alias)=>{
73
+ let exposePath;
74
+ // @ts-ignore
75
+ const exposeValue = exposes[alias];
76
+ if (typeof exposeValue === 'string') {
77
+ exposePath = exposeValue;
78
+ } else {
79
+ exposePath = exposeValue.import[0];
80
+ }
81
+ const targetPaths = fixPrefetchPath(exposePath);
82
+ for (const pathItem of targetPaths){
83
+ const absolutePath = path.resolve(compiler.options.context, pathItem);
84
+ if (fileExistsWithCaseSync(absolutePath)) {
85
+ const absoluteAlias = alias.replace('.', '');
86
+ this._reWriteExports += `export * as ${runtimeUtils.getPrefetchId(`${name}${absoluteAlias}`)} from '${absolutePath}';\n`;
87
+ break;
88
+ }
89
+ }
90
+ });
91
+ if (!this._reWriteExports) {
92
+ return;
93
+ }
94
+ const tempDirRealPath = path.resolve(compiler.options.context, 'node_modules', TEMP_DIR);
95
+ if (!fs.existsSync(tempDirRealPath)) {
96
+ fs.mkdirSync(tempDirRealPath);
97
+ }
98
+ if (!fs.existsSync(`${tempDirRealPath}/${encodedName}`)) {
99
+ fs.mkdirSync(`${tempDirRealPath}/${encodedName}`);
100
+ }
101
+ fs.writeFileSync(asyncEntryPath, this._reWriteExports);
102
+ new compiler.webpack.DefinePlugin({
103
+ FederationDataPrefetch: JSON.stringify(asyncEntryPath)
104
+ }).apply(compiler);
105
+ }
106
+ static addRuntime(compiler, options) {
107
+ const encodedName = sdk.encodeName(options.name);
108
+ if (!compiler.options.context) {
109
+ throw new Error('compiler.options.context is not defined');
110
+ }
111
+ const prefetchEntry = path.resolve(compiler.options.context, `node_modules/.mf/${encodedName}/bootstrap.js`);
112
+ const federationGlobal = getFederationGlobalScope(RuntimeGlobals || {});
113
+ return Template.asString([
114
+ fs.existsSync(prefetchEntry) ? Template.indent([
115
+ 'function injectPrefetch() {',
116
+ Template.indent([
117
+ `globalThis.__FEDERATION__ = globalThis.__FEDERATION__ || {};`,
118
+ `globalThis.__FEDERATION__['${sdk.MFPrefetchCommon.globalKey}'] = globalThis.__FEDERATION__['${sdk.MFPrefetchCommon.globalKey}'] || {`,
119
+ `entryLoading: {},`,
120
+ `instance: new Map(),`,
121
+ `__PREFETCH_EXPORTS__: {},`,
122
+ `};`,
123
+ `globalThis.__FEDERATION__['${sdk.MFPrefetchCommon.globalKey}']['${sdk.MFPrefetchCommon.exportsKey}'] = globalThis.__FEDERATION__['${sdk.MFPrefetchCommon.globalKey}']['${sdk.MFPrefetchCommon.exportsKey}'] || {};`,
124
+ `globalThis.__FEDERATION__['${sdk.MFPrefetchCommon.globalKey}']['${sdk.MFPrefetchCommon.exportsKey}']['${options.name}'] = function(){ return import('${prefetchEntry}');}`
125
+ ]),
126
+ '}',
127
+ `${federationGlobal}.prefetch = injectPrefetch`
128
+ ]) : '',
129
+ Template.indent([
130
+ `if(!${federationGlobal}.isMFRemote && ${federationGlobal}.prefetch){`,
131
+ `${federationGlobal}.prefetch()`,
132
+ '}'
133
+ ])
134
+ ]);
135
+ }
136
+ static setRemoteIdentifier() {
137
+ const federationGlobal = getFederationGlobalScope(RuntimeGlobals || {});
138
+ return Template.indent([
139
+ `${federationGlobal}.isMFRemote = true;`
140
+ ]);
141
+ }
142
+ static removeRemoteIdentifier() {
143
+ const federationGlobal = getFederationGlobalScope(RuntimeGlobals || {});
144
+ return Template.indent([
145
+ `${federationGlobal}.isMFRemote = false;`
146
+ ]);
147
+ }
148
+ constructor(options){
149
+ this.options = options;
150
+ this._reWriteExports = '';
151
+ }
152
+ }
153
+
154
+ exports.PrefetchPlugin = PrefetchPlugin;
155
+ exports.getFederationGlobalScope = getFederationGlobalScope;
@@ -0,0 +1 @@
1
+ export * from "./src/cli/index";
@@ -0,0 +1,152 @@
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 };
@@ -0,0 +1 @@
1
+ export * from "./src/index";
@@ -0,0 +1,13 @@
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;
@@ -0,0 +1,7 @@
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;
@@ -0,0 +1 @@
1
+ export * from "./src/index";
@@ -0,0 +1,6 @@
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';
@@ -0,0 +1,5 @@
1
+ import { Logger } from '@module-federation/sdk';
2
+
3
+ var logger = new Logger('[Module Federation Data Prefetch]');
4
+
5
+ export { logger as l };
@@ -0,0 +1,100 @@
1
+ {
2
+ "name": "@module-federation/data-prefetch",
3
+ "description": "Module Federation Data Prefetch",
4
+ "version": "0.6.7",
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
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./src/plugin";
2
+ export { default } from "./src/plugin";