@module-federation/data-prefetch 0.6.6 → 0.6.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.swcrc +29 -0
- package/CHANGELOG.md +28 -0
- package/LICENSE +1 -1
- package/__tests__/prefetch.spec.ts +3 -2
- package/__tests__/react.spec.ts +1 -1
- package/dist/LICENSE +21 -0
- package/dist/babel.cjs.d.ts +2 -0
- package/dist/babel.cjs.js +50 -0
- package/dist/babel.esm.d.ts +2 -0
- package/dist/babel.esm.js +48 -0
- package/dist/cli.cjs.d.ts +1 -0
- package/dist/cli.cjs.js +155 -0
- package/dist/cli.esm.d.ts +1 -0
- package/dist/cli.esm.js +152 -0
- package/dist/index.cjs.d.ts +1 -0
- package/dist/index.cjs.js +13 -0
- package/dist/index.cjs2.js +7 -0
- package/dist/index.esm.d.ts +1 -0
- package/dist/index.esm.js +6 -0
- package/dist/index.esm2.js +5 -0
- package/dist/package.json +100 -0
- package/dist/plugin.cjs.d.ts +2 -0
- package/dist/plugin.cjs.js +159 -0
- package/dist/plugin.esm.d.ts +2 -0
- package/dist/plugin.esm.js +154 -0
- package/dist/polyfills.cjs.js +14 -0
- package/dist/polyfills.esm.js +12 -0
- package/dist/prefetch.cjs.js +142 -0
- package/dist/prefetch.esm.js +140 -0
- package/dist/react.cjs.d.ts +1 -0
- package/dist/react.cjs.js +77 -0
- package/dist/react.esm.d.ts +1 -0
- package/dist/react.esm.js +75 -0
- package/dist/runtime-utils.cjs.js +27 -0
- package/dist/runtime-utils.esm.js +22 -0
- package/dist/shared.cjs.d.ts +2 -0
- package/dist/shared.cjs.js +24 -0
- package/dist/shared.esm.d.ts +2 -0
- package/dist/shared.esm.js +22 -0
- package/dist/{cli → src/cli}/babel.d.ts +2 -4
- package/dist/{cli → src/cli}/index.d.ts +5 -8
- package/dist/src/common/constant.d.ts +1 -0
- package/dist/src/common/index.d.ts +1 -0
- package/dist/src/common/node-utils.d.ts +2 -0
- package/dist/src/common/runtime-utils.d.ts +5 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/logger/index.d.ts +3 -0
- package/dist/src/plugin.d.ts +3 -0
- package/dist/{prefetch-4e9646e4.d.ts → src/prefetch.d.ts} +5 -7
- package/dist/src/react/hooks.d.ts +11 -0
- package/dist/src/react/index.d.ts +1 -0
- package/dist/src/react/utils.d.ts +1 -0
- package/dist/src/shared/index.d.ts +3 -0
- package/dist/src/universal/index.d.ts +2 -0
- package/dist/universal.cjs.d.ts +1 -0
- package/dist/universal.cjs.js +29 -0
- package/dist/universal.esm.d.ts +1 -0
- package/dist/universal.esm.js +27 -0
- package/jest.config.js +2 -2
- package/package.json +33 -30
- package/project.json +48 -6
- package/rollup.config.js +46 -0
- package/src/cli/index.ts +12 -10
- package/src/prefetch.ts +6 -2
- package/tsconfig.json +1 -1
- package/tsconfig.lib.json +10 -0
- package/dist/cli/babel.js +0 -84
- package/dist/cli/index.js +0 -205
- package/dist/esm/chunk-AJPO2B2T.js +0 -30
- package/dist/esm/chunk-EZUCZHGV.js +0 -11
- package/dist/esm/chunk-ISLXMAIA.js +0 -155
- package/dist/esm/chunk-KI4QGPIS.js +0 -32
- package/dist/esm/chunk-TTJJJ2WZ.js +0 -7
- package/dist/esm/chunk-VVZ6XIM6.js +0 -166
- package/dist/esm/cli/babel.js +0 -57
- package/dist/esm/cli/index.js +0 -178
- package/dist/esm/index.js +0 -13
- package/dist/esm/plugin.js +0 -12
- package/dist/esm/react/index.js +0 -94
- package/dist/esm/shared/index.js +0 -27
- package/dist/esm/universal/index.js +0 -9
- package/dist/index.d.ts +0 -5
- package/dist/index.js +0 -350
- package/dist/plugin.d.ts +0 -5
- package/dist/plugin.js +0 -346
- package/dist/react/index.d.ts +0 -16
- package/dist/react/index.js +0 -286
- package/dist/shared/index.d.ts +0 -5
- package/dist/shared/index.js +0 -48
- package/dist/universal/index.d.ts +0 -8
- package/dist/universal/index.js +0 -204
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,5 +1,33 @@
|
|
|
1
1
|
# @module-federation/data-prefetch
|
|
2
2
|
|
|
3
|
+
## 0.6.8
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [32db0ac]
|
|
8
|
+
- Updated dependencies [32db0ac]
|
|
9
|
+
- Updated dependencies [6c5f444]
|
|
10
|
+
- Updated dependencies [fac6ecf]
|
|
11
|
+
- @module-federation/sdk@0.6.8
|
|
12
|
+
- @module-federation/runtime@0.6.8
|
|
13
|
+
|
|
14
|
+
## 0.6.7
|
|
15
|
+
|
|
16
|
+
### Patch Changes
|
|
17
|
+
|
|
18
|
+
- 9e32644: Refactored the way prefetch entries are imported for improved dynamic loading handling.
|
|
19
|
+
|
|
20
|
+
- Changed the import of prefetch entries to use a function wrapper for more dynamic control.
|
|
21
|
+
- Updated data types to ensure consistency with the new function-based import approach.
|
|
22
|
+
- Modified the `injectPrefetch` function structure to incorporate the new import method.
|
|
23
|
+
- Modified the `MFDataPrefetch` class to handle the new import function when resolving exports.
|
|
24
|
+
|
|
25
|
+
- Updated dependencies [9e32644]
|
|
26
|
+
- Updated dependencies [9e32644]
|
|
27
|
+
- Updated dependencies [9e32644]
|
|
28
|
+
- @module-federation/runtime@0.6.7
|
|
29
|
+
- @module-federation/sdk@0.6.7
|
|
30
|
+
|
|
3
31
|
## 0.6.6
|
|
4
32
|
|
|
5
33
|
### Patch Changes
|
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
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
|
|
@@ -65,13 +65,14 @@ describe('MF Data Prefetch', () => {
|
|
|
65
65
|
[encodeName(exposeId)]: exposeExport,
|
|
66
66
|
};
|
|
67
67
|
globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__[options.name] =
|
|
68
|
-
Promise.resolve(projectExport);
|
|
68
|
+
() => Promise.resolve(projectExport);
|
|
69
69
|
|
|
70
70
|
await prefetch.getProjectExports();
|
|
71
71
|
expect(prefetch.getExposeExports(`${options.name}/button`)).toEqual(
|
|
72
72
|
exposeExport,
|
|
73
73
|
);
|
|
74
74
|
});
|
|
75
|
+
|
|
75
76
|
// Prefetching with memory and executing prefetch function
|
|
76
77
|
it('executes prefetch using prefetch function with and without memory', async () => {
|
|
77
78
|
const id = options.name;
|
|
@@ -84,7 +85,7 @@ describe('MF Data Prefetch', () => {
|
|
|
84
85
|
const prefetchExports = { [functionId]: executePrefetch };
|
|
85
86
|
|
|
86
87
|
// Mock Project Exports
|
|
87
|
-
globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__[id] =
|
|
88
|
+
globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__[id] = () =>
|
|
88
89
|
Promise.resolve({
|
|
89
90
|
[encodeName(exposeId)]: prefetchExports,
|
|
90
91
|
});
|
package/__tests__/react.spec.ts
CHANGED
|
@@ -71,7 +71,7 @@ describe('usePrefetch', () => {
|
|
|
71
71
|
[ModuleFederationSDK.encodeName(exposeId)]: exposeExport,
|
|
72
72
|
};
|
|
73
73
|
globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__[options.name] =
|
|
74
|
-
Promise.resolve(projectExport);
|
|
74
|
+
() => Promise.resolve(projectExport);
|
|
75
75
|
});
|
|
76
76
|
|
|
77
77
|
afterEach(() => {
|
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,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,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";
|
package/dist/cli.cjs.js
ADDED
|
@@ -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";
|
package/dist/cli.esm.js
ADDED
|
@@ -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 @@
|
|
|
1
|
+
export * from "./src/index";
|