@module-federation/data-prefetch 0.0.0-next-20241015065721 → 0.0.0-next-20241016062654

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 (89) hide show
  1. package/.swcrc +29 -0
  2. package/CHANGELOG.md +41 -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 +156 -0
  11. package/dist/cli.esm.d.ts +1 -0
  12. package/dist/cli.esm.js +153 -0
  13. package/dist/constant.cjs.js +5 -0
  14. package/dist/constant.esm.js +3 -0
  15. package/dist/index.cjs.d.ts +1 -0
  16. package/dist/index.cjs.js +14 -0
  17. package/dist/index.cjs2.js +7 -0
  18. package/dist/index.esm.d.ts +1 -0
  19. package/dist/index.esm.js +7 -0
  20. package/dist/index.esm2.js +5 -0
  21. package/dist/package.json +88 -0
  22. package/dist/plugin.cjs.d.ts +2 -0
  23. package/dist/plugin.cjs.js +159 -0
  24. package/dist/plugin.esm.d.ts +2 -0
  25. package/dist/plugin.esm.js +154 -0
  26. package/dist/polyfills.cjs.js +14 -0
  27. package/dist/polyfills.esm.js +12 -0
  28. package/dist/prefetch.cjs.js +142 -0
  29. package/dist/prefetch.esm.js +140 -0
  30. package/dist/react.cjs.d.ts +1 -0
  31. package/dist/react.cjs.js +77 -0
  32. package/dist/react.esm.d.ts +1 -0
  33. package/dist/react.esm.js +75 -0
  34. package/dist/runtime-utils.cjs.js +27 -0
  35. package/dist/runtime-utils.esm.js +22 -0
  36. package/dist/{cli → src/cli}/babel.d.ts +2 -4
  37. package/dist/{cli → src/cli}/index.d.ts +5 -8
  38. package/dist/src/common/constant.d.ts +1 -0
  39. package/dist/src/common/index.d.ts +1 -0
  40. package/dist/src/common/node-utils.d.ts +2 -0
  41. package/dist/src/common/runtime-utils.d.ts +5 -0
  42. package/dist/src/constant.d.ts +1 -0
  43. package/dist/src/index.d.ts +2 -0
  44. package/dist/src/logger/index.d.ts +3 -0
  45. package/dist/src/plugin.d.ts +3 -0
  46. package/dist/{prefetch-c08be471.d.ts → src/prefetch.d.ts} +4 -6
  47. package/dist/src/react/hooks.d.ts +11 -0
  48. package/dist/src/react/index.d.ts +1 -0
  49. package/dist/src/react/utils.d.ts +1 -0
  50. package/dist/src/universal/index.d.ts +2 -0
  51. package/dist/universal.cjs.d.ts +1 -0
  52. package/dist/universal.cjs.js +29 -0
  53. package/dist/universal.esm.d.ts +1 -0
  54. package/dist/universal.esm.js +27 -0
  55. package/package.json +28 -37
  56. package/project.json +48 -6
  57. package/rollup.config.js +45 -0
  58. package/src/cli/index.ts +7 -4
  59. package/src/constant.ts +1 -0
  60. package/src/plugin.ts +4 -4
  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
  88. package/src/shared/index.ts +0 -26
  89. package/tsup.config.ts +0 -35
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,44 @@
1
1
  # @module-federation/data-prefetch
2
2
 
3
- ## 0.0.0-next-20241015065721
3
+ ## 0.0.0-next-20241016062654
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [ea6d417]
8
+ - Updated dependencies [d5a3072]
9
+ - @module-federation/runtime@0.0.0-next-20241016062654
10
+ - @module-federation/sdk@0.0.0-next-20241016062654
11
+
12
+ ## 0.6.10
13
+
14
+ ### Patch Changes
15
+
16
+ - 22a3b83: fix(data-prefetch): apply DataPrefetchPlugin on demand
17
+ - 22a3b83: fix(data-prefetch): set sharedStrategy in build options instead of using runtime plugin
18
+ - Updated dependencies [b704f30]
19
+ - Updated dependencies [22a3b83]
20
+ - @module-federation/runtime@0.6.10
21
+ - @module-federation/sdk@0.6.10
22
+
23
+ ## 0.6.9
24
+
25
+ ### Patch Changes
26
+
27
+ - @module-federation/runtime@0.6.9
28
+ - @module-federation/sdk@0.6.9
29
+
30
+ ## 0.6.8
31
+
32
+ ### Patch Changes
33
+
34
+ - Updated dependencies [32db0ac]
35
+ - Updated dependencies [32db0ac]
36
+ - Updated dependencies [6c5f444]
37
+ - Updated dependencies [fac6ecf]
38
+ - @module-federation/sdk@0.6.8
39
+ - @module-federation/runtime@0.6.8
40
+
41
+ ## 0.6.7
4
42
 
5
43
  ### Patch Changes
6
44
 
@@ -14,9 +52,8 @@
14
52
  - Updated dependencies [9e32644]
15
53
  - Updated dependencies [9e32644]
16
54
  - Updated dependencies [9e32644]
17
- - Updated dependencies [3082116]
18
- - @module-federation/runtime@0.0.0-next-20241015065721
19
- - @module-federation/sdk@0.0.0-next-20241015065721
55
+ - @module-federation/runtime@0.6.7
56
+ - @module-federation/sdk@0.6.7
20
57
 
21
58
  ## 0.6.6
22
59
 
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,156 @@
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
+ var constant = require('./constant.cjs.js');
9
+ require('@module-federation/runtime');
10
+
11
+ const TEMP_DIR = '.mf';
12
+
13
+ const fileExistsWithCaseSync = (filepath)=>{
14
+ const dir = path.dirname(filepath);
15
+ if (filepath === '/' || filepath === '.') {
16
+ return true;
17
+ }
18
+ const filenames = fs.readdirSync(dir);
19
+ if (filenames.indexOf(path.basename(filepath)) === -1) {
20
+ return false;
21
+ }
22
+ return fileExistsWithCaseSync(dir);
23
+ };
24
+ const fixPrefetchPath = (exposePath)=>{
25
+ const pathExt = [
26
+ '.js',
27
+ '.ts'
28
+ ];
29
+ const extReg = /\.(ts|js|tsx|jsx)$/;
30
+ if (extReg.test(exposePath)) {
31
+ return pathExt.map((ext)=>exposePath.replace(extReg, `.prefetch${ext}`));
32
+ } else {
33
+ return pathExt.map((ext)=>exposePath + `.prefetch${ext}`);
34
+ }
35
+ };
36
+
37
+ const { RuntimeGlobals, Template } = require(normalizeWebpackPath.normalizeWebpackPath('webpack'));
38
+ function getFederationGlobalScope(runtimeGlobals) {
39
+ return `${runtimeGlobals.require || '__webpack_require__'}.federation`;
40
+ }
41
+ class PrefetchPlugin {
42
+ // eslint-disable-next-line max-lines-per-function
43
+ apply(compiler) {
44
+ var _this_options_runtimePlugins;
45
+ const { name, exposes } = this.options;
46
+ if (!exposes) {
47
+ return;
48
+ }
49
+ if (!compiler.options.context) {
50
+ throw new Error('compiler.options.context is not defined');
51
+ }
52
+ const { runtimePlugins } = this.options;
53
+ if (!Array.isArray(runtimePlugins)) {
54
+ this.options.runtimePlugins = [];
55
+ }
56
+ const runtimePath = path.resolve(__dirname, './plugin.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.shareStrategy !== constant.SHARED_STRATEGY) {
61
+ this.options.shareStrategy = constant.SHARED_STRATEGY;
62
+ console.warn(`[Module Federation Data Prefetch]: Your shared strategy is set to '${constant.SHARED_STRATEGY}', this is a necessary condition for data prefetch`);
63
+ }
64
+ const encodedName = sdk.encodeName(name);
65
+ const asyncEntryPath = path.resolve(compiler.options.context, `node_modules/${TEMP_DIR}/${encodedName}/bootstrap.js`);
66
+ if (fs.existsSync(asyncEntryPath)) {
67
+ fs.unlinkSync(asyncEntryPath);
68
+ }
69
+ if (!this.options.dataPrefetch) {
70
+ return;
71
+ }
72
+ const exposeAlias = Object.keys(exposes);
73
+ exposeAlias.forEach((alias)=>{
74
+ let exposePath;
75
+ // @ts-ignore
76
+ const exposeValue = exposes[alias];
77
+ if (typeof exposeValue === 'string') {
78
+ exposePath = exposeValue;
79
+ } else {
80
+ exposePath = exposeValue.import[0];
81
+ }
82
+ const targetPaths = fixPrefetchPath(exposePath);
83
+ for (const pathItem of targetPaths){
84
+ const absolutePath = path.resolve(compiler.options.context, pathItem);
85
+ if (fileExistsWithCaseSync(absolutePath)) {
86
+ const absoluteAlias = alias.replace('.', '');
87
+ this._reWriteExports += `export * as ${runtimeUtils.getPrefetchId(`${name}${absoluteAlias}`)} from '${absolutePath}';\n`;
88
+ break;
89
+ }
90
+ }
91
+ });
92
+ if (!this._reWriteExports) {
93
+ return;
94
+ }
95
+ const tempDirRealPath = path.resolve(compiler.options.context, 'node_modules', TEMP_DIR);
96
+ if (!fs.existsSync(tempDirRealPath)) {
97
+ fs.mkdirSync(tempDirRealPath);
98
+ }
99
+ if (!fs.existsSync(`${tempDirRealPath}/${encodedName}`)) {
100
+ fs.mkdirSync(`${tempDirRealPath}/${encodedName}`);
101
+ }
102
+ fs.writeFileSync(asyncEntryPath, this._reWriteExports);
103
+ new compiler.webpack.DefinePlugin({
104
+ FederationDataPrefetch: JSON.stringify(asyncEntryPath)
105
+ }).apply(compiler);
106
+ }
107
+ static addRuntime(compiler, options) {
108
+ const encodedName = sdk.encodeName(options.name);
109
+ if (!compiler.options.context) {
110
+ throw new Error('compiler.options.context is not defined');
111
+ }
112
+ const prefetchEntry = path.resolve(compiler.options.context, `node_modules/.mf/${encodedName}/bootstrap.js`);
113
+ const federationGlobal = getFederationGlobalScope(RuntimeGlobals || {});
114
+ return Template.asString([
115
+ fs.existsSync(prefetchEntry) ? Template.indent([
116
+ 'function injectPrefetch() {',
117
+ Template.indent([
118
+ `globalThis.__FEDERATION__ = globalThis.__FEDERATION__ || {};`,
119
+ `globalThis.__FEDERATION__['${sdk.MFPrefetchCommon.globalKey}'] = globalThis.__FEDERATION__['${sdk.MFPrefetchCommon.globalKey}'] || {`,
120
+ `entryLoading: {},`,
121
+ `instance: new Map(),`,
122
+ `__PREFETCH_EXPORTS__: {},`,
123
+ `};`,
124
+ `globalThis.__FEDERATION__['${sdk.MFPrefetchCommon.globalKey}']['${sdk.MFPrefetchCommon.exportsKey}'] = globalThis.__FEDERATION__['${sdk.MFPrefetchCommon.globalKey}']['${sdk.MFPrefetchCommon.exportsKey}'] || {};`,
125
+ `globalThis.__FEDERATION__['${sdk.MFPrefetchCommon.globalKey}']['${sdk.MFPrefetchCommon.exportsKey}']['${options.name}'] = function(){ return import('${prefetchEntry}');}`
126
+ ]),
127
+ '}',
128
+ `${federationGlobal}.prefetch = injectPrefetch`
129
+ ]) : '',
130
+ Template.indent([
131
+ `if(!${federationGlobal}.isMFRemote && ${federationGlobal}.prefetch){`,
132
+ `${federationGlobal}.prefetch()`,
133
+ '}'
134
+ ])
135
+ ]);
136
+ }
137
+ static setRemoteIdentifier() {
138
+ const federationGlobal = getFederationGlobalScope(RuntimeGlobals || {});
139
+ return Template.indent([
140
+ `${federationGlobal}.isMFRemote = true;`
141
+ ]);
142
+ }
143
+ static removeRemoteIdentifier() {
144
+ const federationGlobal = getFederationGlobalScope(RuntimeGlobals || {});
145
+ return Template.indent([
146
+ `${federationGlobal}.isMFRemote = false;`
147
+ ]);
148
+ }
149
+ constructor(options){
150
+ this.options = options;
151
+ this._reWriteExports = '';
152
+ }
153
+ }
154
+
155
+ exports.PrefetchPlugin = PrefetchPlugin;
156
+ exports.getFederationGlobalScope = getFederationGlobalScope;
@@ -0,0 +1 @@
1
+ export * from "./src/cli/index";
@@ -0,0 +1,153 @@
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 { S as SHARED_STRATEGY } from './constant.esm.js';
7
+ import '@module-federation/runtime';
8
+
9
+ const TEMP_DIR = '.mf';
10
+
11
+ const fileExistsWithCaseSync = (filepath)=>{
12
+ const dir = path.dirname(filepath);
13
+ if (filepath === '/' || filepath === '.') {
14
+ return true;
15
+ }
16
+ const filenames = fs.readdirSync(dir);
17
+ if (filenames.indexOf(path.basename(filepath)) === -1) {
18
+ return false;
19
+ }
20
+ return fileExistsWithCaseSync(dir);
21
+ };
22
+ const fixPrefetchPath = (exposePath)=>{
23
+ const pathExt = [
24
+ '.js',
25
+ '.ts'
26
+ ];
27
+ const extReg = /\.(ts|js|tsx|jsx)$/;
28
+ if (extReg.test(exposePath)) {
29
+ return pathExt.map((ext)=>exposePath.replace(extReg, `.prefetch${ext}`));
30
+ } else {
31
+ return pathExt.map((ext)=>exposePath + `.prefetch${ext}`);
32
+ }
33
+ };
34
+
35
+ const { RuntimeGlobals, Template } = require(normalizeWebpackPath('webpack'));
36
+ function getFederationGlobalScope(runtimeGlobals) {
37
+ return `${runtimeGlobals.require || '__webpack_require__'}.federation`;
38
+ }
39
+ class PrefetchPlugin {
40
+ // eslint-disable-next-line max-lines-per-function
41
+ apply(compiler) {
42
+ var _this_options_runtimePlugins;
43
+ const { name, exposes } = this.options;
44
+ if (!exposes) {
45
+ return;
46
+ }
47
+ if (!compiler.options.context) {
48
+ throw new Error('compiler.options.context is not defined');
49
+ }
50
+ const { runtimePlugins } = this.options;
51
+ if (!Array.isArray(runtimePlugins)) {
52
+ this.options.runtimePlugins = [];
53
+ }
54
+ const runtimePath = path.resolve(__dirname, './plugin.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.shareStrategy !== SHARED_STRATEGY) {
59
+ this.options.shareStrategy = SHARED_STRATEGY;
60
+ console.warn(`[Module Federation Data Prefetch]: Your shared strategy is set to '${SHARED_STRATEGY}', this is a necessary condition for data prefetch`);
61
+ }
62
+ const encodedName = encodeName(name);
63
+ const asyncEntryPath = path.resolve(compiler.options.context, `node_modules/${TEMP_DIR}/${encodedName}/bootstrap.js`);
64
+ if (fs.existsSync(asyncEntryPath)) {
65
+ fs.unlinkSync(asyncEntryPath);
66
+ }
67
+ if (!this.options.dataPrefetch) {
68
+ return;
69
+ }
70
+ const exposeAlias = Object.keys(exposes);
71
+ exposeAlias.forEach((alias)=>{
72
+ let exposePath;
73
+ // @ts-ignore
74
+ const exposeValue = exposes[alias];
75
+ if (typeof exposeValue === 'string') {
76
+ exposePath = exposeValue;
77
+ } else {
78
+ exposePath = exposeValue.import[0];
79
+ }
80
+ const targetPaths = fixPrefetchPath(exposePath);
81
+ for (const pathItem of targetPaths){
82
+ const absolutePath = path.resolve(compiler.options.context, pathItem);
83
+ if (fileExistsWithCaseSync(absolutePath)) {
84
+ const absoluteAlias = alias.replace('.', '');
85
+ this._reWriteExports += `export * as ${getPrefetchId(`${name}${absoluteAlias}`)} from '${absolutePath}';\n`;
86
+ break;
87
+ }
88
+ }
89
+ });
90
+ if (!this._reWriteExports) {
91
+ return;
92
+ }
93
+ const tempDirRealPath = path.resolve(compiler.options.context, 'node_modules', TEMP_DIR);
94
+ if (!fs.existsSync(tempDirRealPath)) {
95
+ fs.mkdirSync(tempDirRealPath);
96
+ }
97
+ if (!fs.existsSync(`${tempDirRealPath}/${encodedName}`)) {
98
+ fs.mkdirSync(`${tempDirRealPath}/${encodedName}`);
99
+ }
100
+ fs.writeFileSync(asyncEntryPath, this._reWriteExports);
101
+ new compiler.webpack.DefinePlugin({
102
+ FederationDataPrefetch: JSON.stringify(asyncEntryPath)
103
+ }).apply(compiler);
104
+ }
105
+ static addRuntime(compiler, options) {
106
+ const encodedName = encodeName(options.name);
107
+ if (!compiler.options.context) {
108
+ throw new Error('compiler.options.context is not defined');
109
+ }
110
+ const prefetchEntry = path.resolve(compiler.options.context, `node_modules/.mf/${encodedName}/bootstrap.js`);
111
+ const federationGlobal = getFederationGlobalScope(RuntimeGlobals || {});
112
+ return Template.asString([
113
+ fs.existsSync(prefetchEntry) ? Template.indent([
114
+ 'function injectPrefetch() {',
115
+ Template.indent([
116
+ `globalThis.__FEDERATION__ = globalThis.__FEDERATION__ || {};`,
117
+ `globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}'] = globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}'] || {`,
118
+ `entryLoading: {},`,
119
+ `instance: new Map(),`,
120
+ `__PREFETCH_EXPORTS__: {},`,
121
+ `};`,
122
+ `globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}']['${MFPrefetchCommon.exportsKey}'] = globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}']['${MFPrefetchCommon.exportsKey}'] || {};`,
123
+ `globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}']['${MFPrefetchCommon.exportsKey}']['${options.name}'] = function(){ return import('${prefetchEntry}');}`
124
+ ]),
125
+ '}',
126
+ `${federationGlobal}.prefetch = injectPrefetch`
127
+ ]) : '',
128
+ Template.indent([
129
+ `if(!${federationGlobal}.isMFRemote && ${federationGlobal}.prefetch){`,
130
+ `${federationGlobal}.prefetch()`,
131
+ '}'
132
+ ])
133
+ ]);
134
+ }
135
+ static setRemoteIdentifier() {
136
+ const federationGlobal = getFederationGlobalScope(RuntimeGlobals || {});
137
+ return Template.indent([
138
+ `${federationGlobal}.isMFRemote = true;`
139
+ ]);
140
+ }
141
+ static removeRemoteIdentifier() {
142
+ const federationGlobal = getFederationGlobalScope(RuntimeGlobals || {});
143
+ return Template.indent([
144
+ `${federationGlobal}.isMFRemote = false;`
145
+ ]);
146
+ }
147
+ constructor(options){
148
+ this.options = options;
149
+ this._reWriteExports = '';
150
+ }
151
+ }
152
+
153
+ export { PrefetchPlugin, getFederationGlobalScope };
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ const SHARED_STRATEGY = 'loaded-first';
4
+
5
+ exports.SHARED_STRATEGY = SHARED_STRATEGY;
@@ -0,0 +1,3 @@
1
+ const SHARED_STRATEGY = 'loaded-first';
2
+
3
+ export { SHARED_STRATEGY as S };
@@ -0,0 +1 @@
1
+ export * from "./src/index";
@@ -0,0 +1,14 @@
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
+ require('./constant.cjs.js');
10
+
11
+
12
+
13
+ exports.MFDataPrefetch = prefetch.MFDataPrefetch;
14
+ 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,7 @@
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';
7
+ import './constant.esm.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 };