@module-federation/data-prefetch 1.0.16 → 2.0.0
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/LICENSE +1 -1
- package/README.md +3 -1
- package/dist/LICENSE +21 -0
- package/dist/babel.cjs.cjs +54 -0
- package/dist/babel.cjs.cjs.map +1 -0
- package/dist/babel.cjs.d.ts +2 -0
- package/dist/babel.d.ts +2 -0
- package/dist/babel.esm.js +52 -0
- package/dist/babel.esm.js.map +1 -0
- package/dist/cli.cjs.cjs +156 -0
- package/dist/cli.cjs.cjs.map +1 -0
- package/dist/cli.cjs.d.ts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.esm.js +153 -0
- package/dist/cli.esm.js.map +1 -0
- package/dist/constant.cjs.cjs +6 -0
- package/dist/constant.cjs.cjs.map +1 -0
- package/dist/constant.esm.js +4 -0
- package/dist/constant.esm.js.map +1 -0
- package/dist/index.cjs.cjs +10 -0
- package/dist/index.cjs.cjs.map +1 -0
- package/dist/index.cjs.d.ts +1 -0
- package/dist/index.cjs2.cjs +8 -0
- package/dist/index.cjs2.cjs.map +1 -0
- package/dist/index.d.ts +1 -5
- package/dist/index.esm.js +3 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.esm2.js +6 -0
- package/dist/index.esm2.js.map +1 -0
- package/dist/plugin.cjs.cjs +158 -0
- package/dist/plugin.cjs.cjs.map +1 -0
- package/dist/plugin.cjs.d.ts +2 -0
- package/dist/plugin.d.ts +2 -5
- package/dist/plugin.esm.js +153 -0
- package/dist/plugin.esm.js.map +1 -0
- package/dist/prefetch.cjs.cjs +182 -0
- package/dist/prefetch.cjs.cjs.map +1 -0
- package/dist/prefetch.esm.js +179 -0
- package/dist/prefetch.esm.js.map +1 -0
- package/dist/react.cjs.cjs +76 -0
- package/dist/react.cjs.cjs.map +1 -0
- package/dist/react.cjs.d.ts +1 -0
- package/dist/react.d.ts +1 -0
- package/dist/react.esm.js +74 -0
- package/dist/react.esm.js.map +1 -0
- package/dist/runtime-utils.cjs.cjs +29 -0
- package/dist/runtime-utils.cjs.cjs.map +1 -0
- package/dist/runtime-utils.esm.js +24 -0
- package/dist/runtime-utils.esm.js.map +1 -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/node-utils.d.ts +2 -0
- package/dist/src/common/runtime-utils.d.ts +5 -0
- package/dist/src/constant.d.ts +1 -0
- package/dist/src/logger/index.d.ts +2 -0
- package/dist/src/plugin.d.ts +3 -0
- package/dist/{prefetch-4e9646e4.d.ts → src/prefetch.d.ts} +14 -17
- package/dist/src/react/hooks.d.ts +11 -0
- package/dist/src/react/utils.d.ts +1 -0
- package/dist/src/universal/index.d.ts +2 -0
- package/dist/universal.cjs.cjs +30 -0
- package/dist/universal.cjs.cjs.map +1 -0
- package/dist/universal.cjs.d.ts +1 -0
- package/dist/universal.d.ts +1 -0
- package/dist/universal.esm.js +28 -0
- package/dist/universal.esm.js.map +1 -0
- package/package.json +49 -39
- package/CHANGELOG.md +0 -9
- package/__tests__/babel.spec.ts +0 -75
- package/__tests__/prefetch.spec.ts +0 -138
- package/__tests__/react.spec.ts +0 -119
- 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.js +0 -350
- 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/jest.config.js +0 -29
- package/project.json +0 -21
- package/src/cli/babel.ts +0 -94
- package/src/cli/index.ts +0 -171
- package/src/common/constant.ts +0 -1
- package/src/common/node-utils.ts +0 -24
- package/src/common/runtime-utils.ts +0 -34
- package/src/logger/index.ts +0 -3
- package/src/plugin.ts +0 -191
- package/src/prefetch.ts +0 -207
- package/src/react/hooks.ts +0 -95
- package/src/react/utils.ts +0 -11
- package/src/shared/index.ts +0 -26
- package/src/universal/index.ts +0 -27
- package/tsconfig.json +0 -27
- package/tsup.config.ts +0 -35
- /package/{src/common/index.ts → dist/src/common/index.d.ts} +0 -0
- /package/{src/index.ts → dist/src/index.d.ts} +0 -0
- /package/{src/react/index.ts → dist/src/react/index.d.ts} +0 -0
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { p as prefetchOptions } from '../prefetch-4e9646e4.js';
|
|
2
|
-
import '@module-federation/runtime';
|
|
3
|
-
import '@module-federation/sdk';
|
|
4
|
-
import '@module-federation/runtime/types';
|
|
5
|
-
|
|
6
|
-
declare function prefetch(options: prefetchOptions): Promise<any>;
|
|
7
|
-
|
|
8
|
-
export { prefetch };
|
package/dist/universal/index.js
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/universal/index.ts
|
|
21
|
-
var universal_exports = {};
|
|
22
|
-
__export(universal_exports, {
|
|
23
|
-
prefetch: () => prefetch
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(universal_exports);
|
|
26
|
-
|
|
27
|
-
// src/prefetch.ts
|
|
28
|
-
var import_runtime2 = require("@module-federation/runtime");
|
|
29
|
-
var import_sdk2 = require("@module-federation/sdk");
|
|
30
|
-
|
|
31
|
-
// src/common/runtime-utils.ts
|
|
32
|
-
var import_runtime = require("@module-federation/runtime");
|
|
33
|
-
var import_sdk = require("@module-federation/sdk");
|
|
34
|
-
var getScope = () => {
|
|
35
|
-
return (0, import_runtime.getInstance)().options.name;
|
|
36
|
-
};
|
|
37
|
-
var getPrefetchId = (id) => (0, import_sdk.encodeName)(`${id}/${import_sdk.MFPrefetchCommon.identifier}`);
|
|
38
|
-
var compatGetPrefetchId = (id) => (0, import_sdk.encodeName)(`${id}/VmokPrefetch`);
|
|
39
|
-
|
|
40
|
-
// src/prefetch.ts
|
|
41
|
-
globalThis.__FEDERATION__ ?? (globalThis.__FEDERATION__ = {});
|
|
42
|
-
var _a;
|
|
43
|
-
(_a = globalThis.__FEDERATION__).__PREFETCH__ ?? (_a.__PREFETCH__ = {
|
|
44
|
-
entryLoading: {},
|
|
45
|
-
instance: /* @__PURE__ */ new Map(),
|
|
46
|
-
__PREFETCH_EXPORTS__: {}
|
|
47
|
-
});
|
|
48
|
-
var MFDataPrefetch = class {
|
|
49
|
-
constructor(options) {
|
|
50
|
-
this.prefetchMemory = /* @__PURE__ */ new Map();
|
|
51
|
-
this.recordOutdate = {};
|
|
52
|
-
this._exports = {};
|
|
53
|
-
this._options = options;
|
|
54
|
-
this.global.instance.set(options.name, this);
|
|
55
|
-
}
|
|
56
|
-
get global() {
|
|
57
|
-
return globalThis.__FEDERATION__.__PREFETCH__;
|
|
58
|
-
}
|
|
59
|
-
static getInstance(id) {
|
|
60
|
-
return globalThis.__FEDERATION__.__PREFETCH__.instance.get(id);
|
|
61
|
-
}
|
|
62
|
-
async loadEntry(entry) {
|
|
63
|
-
const { name, remoteSnapshot, remote, origin } = this._options;
|
|
64
|
-
if (entry) {
|
|
65
|
-
const { buildVersion, globalName } = remoteSnapshot;
|
|
66
|
-
const uniqueKey = globalName || `${name}:${buildVersion}`;
|
|
67
|
-
if (!this.global.entryLoading[uniqueKey]) {
|
|
68
|
-
this.global.entryLoading[uniqueKey] = (0, import_sdk2.loadScript)(entry, {});
|
|
69
|
-
}
|
|
70
|
-
return this.global.entryLoading[uniqueKey];
|
|
71
|
-
} else {
|
|
72
|
-
const remoteInfo = (0, import_runtime2.getRemoteInfo)(remote);
|
|
73
|
-
const module2 = origin.moduleCache.get(remoteInfo.name);
|
|
74
|
-
return (0, import_runtime2.getRemoteEntry)({
|
|
75
|
-
origin,
|
|
76
|
-
remoteInfo,
|
|
77
|
-
remoteEntryExports: module2 ? module2.remoteEntryExports : void 0
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
getProjectExports() {
|
|
82
|
-
var _a2;
|
|
83
|
-
if (Object.keys(this._exports).length > 0) {
|
|
84
|
-
return this._exports;
|
|
85
|
-
}
|
|
86
|
-
const { name } = this._options;
|
|
87
|
-
const exportsPromise = (_a2 = globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__) == null ? void 0 : _a2[name];
|
|
88
|
-
const resolve = exportsPromise.then(
|
|
89
|
-
(exports = {}) => {
|
|
90
|
-
const memory = {};
|
|
91
|
-
Object.keys(exports).forEach((key) => {
|
|
92
|
-
memory[key] = {};
|
|
93
|
-
const exportVal = exports[key];
|
|
94
|
-
Object.keys(exportVal).reduce(
|
|
95
|
-
(memo, current) => {
|
|
96
|
-
if (current.toLocaleLowerCase().endsWith("prefetch") || current.toLocaleLowerCase() === "default") {
|
|
97
|
-
memo[current] = exportVal[current];
|
|
98
|
-
}
|
|
99
|
-
return memo;
|
|
100
|
-
},
|
|
101
|
-
memory[key]
|
|
102
|
-
);
|
|
103
|
-
});
|
|
104
|
-
this.memorizeExports(memory);
|
|
105
|
-
}
|
|
106
|
-
);
|
|
107
|
-
return resolve;
|
|
108
|
-
}
|
|
109
|
-
memorizeExports(exports) {
|
|
110
|
-
this._exports = exports;
|
|
111
|
-
}
|
|
112
|
-
getExposeExports(id) {
|
|
113
|
-
const prefetchId = getPrefetchId(id);
|
|
114
|
-
const compatId = compatGetPrefetchId(id);
|
|
115
|
-
const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
|
|
116
|
-
return prefetchExports || {};
|
|
117
|
-
}
|
|
118
|
-
prefetch(prefetchOptions) {
|
|
119
|
-
const { id, functionId = "default", refetchParams } = prefetchOptions;
|
|
120
|
-
let prefetchResult;
|
|
121
|
-
const prefetchId = getPrefetchId(id);
|
|
122
|
-
const compatId = compatGetPrefetchId(id);
|
|
123
|
-
const memorizeId = id + functionId;
|
|
124
|
-
const memory = this.prefetchMemory.get(memorizeId);
|
|
125
|
-
if (!this.checkOutdate(prefetchOptions) && memory) {
|
|
126
|
-
return memory;
|
|
127
|
-
}
|
|
128
|
-
const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
|
|
129
|
-
if (!prefetchExports) {
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
const executePrefetch = prefetchExports[functionId];
|
|
133
|
-
if (typeof executePrefetch === "function") {
|
|
134
|
-
if (refetchParams) {
|
|
135
|
-
prefetchResult = executePrefetch(refetchParams);
|
|
136
|
-
} else {
|
|
137
|
-
prefetchResult = executePrefetch();
|
|
138
|
-
}
|
|
139
|
-
} else {
|
|
140
|
-
throw new Error(
|
|
141
|
-
`[Module Federation Data Prefetch]: No prefetch function called ${functionId} export in prefetch file`
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
|
-
this.memorize(memorizeId, prefetchResult);
|
|
145
|
-
return prefetchResult;
|
|
146
|
-
}
|
|
147
|
-
memorize(id, value) {
|
|
148
|
-
this.prefetchMemory.set(id, value);
|
|
149
|
-
}
|
|
150
|
-
markOutdate(markOptions, isOutdate) {
|
|
151
|
-
const { id, functionId = "default" } = markOptions;
|
|
152
|
-
if (!this.recordOutdate[id]) {
|
|
153
|
-
this.recordOutdate[id] = {};
|
|
154
|
-
}
|
|
155
|
-
this.recordOutdate[id][functionId] = isOutdate;
|
|
156
|
-
}
|
|
157
|
-
checkOutdate(outdateOptions) {
|
|
158
|
-
const { id, functionId = "default", cacheStrategy } = outdateOptions;
|
|
159
|
-
if (typeof cacheStrategy === "function") {
|
|
160
|
-
return cacheStrategy();
|
|
161
|
-
}
|
|
162
|
-
if (!this.recordOutdate[id]) {
|
|
163
|
-
this.recordOutdate[id] = {};
|
|
164
|
-
}
|
|
165
|
-
if (this.recordOutdate[id][functionId]) {
|
|
166
|
-
this.markOutdate(
|
|
167
|
-
{
|
|
168
|
-
id,
|
|
169
|
-
functionId
|
|
170
|
-
},
|
|
171
|
-
false
|
|
172
|
-
);
|
|
173
|
-
return true;
|
|
174
|
-
} else {
|
|
175
|
-
return false;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
// src/universal/index.ts
|
|
181
|
-
function prefetch(options) {
|
|
182
|
-
const { id, functionId = "default" } = options;
|
|
183
|
-
const mfScope = getScope();
|
|
184
|
-
const prefetchInstance = MFDataPrefetch.getInstance(mfScope) || new MFDataPrefetch({
|
|
185
|
-
name: mfScope
|
|
186
|
-
});
|
|
187
|
-
const res = prefetchInstance.getProjectExports();
|
|
188
|
-
if (res instanceof Promise) {
|
|
189
|
-
const promise = res.then(() => {
|
|
190
|
-
const result = prefetchInstance.prefetch(options);
|
|
191
|
-
prefetchInstance.memorize(id + functionId, result);
|
|
192
|
-
return result;
|
|
193
|
-
});
|
|
194
|
-
return promise;
|
|
195
|
-
} else {
|
|
196
|
-
const result = prefetchInstance.prefetch(options);
|
|
197
|
-
prefetchInstance.memorize(id + functionId, result);
|
|
198
|
-
return result;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
202
|
-
0 && (module.exports = {
|
|
203
|
-
prefetch
|
|
204
|
-
});
|
package/jest.config.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
// For a detailed explanation regarding each configuration property, visit:
|
|
2
|
-
// https://jestjs.io/docs/en/configuration.html
|
|
3
|
-
|
|
4
|
-
module.exports = {
|
|
5
|
-
clearMocks: true,
|
|
6
|
-
cacheDirectory: './jest/cache',
|
|
7
|
-
coverageDirectory: './jest/coverage',
|
|
8
|
-
testEnvironment: 'jsdom',
|
|
9
|
-
coveragePathIgnorePatterns: ['__tests__', '/node_modules/'],
|
|
10
|
-
coverageProvider: 'v8',
|
|
11
|
-
coverageReporters: ['cobertura', 'clover', 'json', 'lcov', 'text'],
|
|
12
|
-
globals: {
|
|
13
|
-
__DEV__: true,
|
|
14
|
-
__TEST__: true,
|
|
15
|
-
__BROWSER__: false,
|
|
16
|
-
__VERSION__: '"unknow"',
|
|
17
|
-
},
|
|
18
|
-
preset: 'ts-jest',
|
|
19
|
-
transformIgnorePatterns: [
|
|
20
|
-
// Change MODULE_NAME_HERE to your module that isn't being compiled
|
|
21
|
-
'/node_modules/(?!((@byted/garfish-)|(byted-tea-sdk))).+\\.js$',
|
|
22
|
-
],
|
|
23
|
-
transform: {
|
|
24
|
-
'^.+\\.(t|j)sx?$': ['@swc/jest', { swcrc: false }],
|
|
25
|
-
},
|
|
26
|
-
rootDir: __dirname,
|
|
27
|
-
testMatch: ['<rootDir>__tests__/**/**.spec.[jt]s?(x)'],
|
|
28
|
-
testPathIgnorePatterns: ['/node_modules/', '/dev/'],
|
|
29
|
-
};
|
package/project.json
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "module-federation/data-prefetch",
|
|
3
|
-
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
-
"sourceRoot": "packages/data-prefetch/src",
|
|
5
|
-
"projectType": "library",
|
|
6
|
-
"targets": {
|
|
7
|
-
"build": {
|
|
8
|
-
"executor": "nx:run-commands",
|
|
9
|
-
"options": {
|
|
10
|
-
"commands": ["npm run build --prefix packages/data-prefetch"]
|
|
11
|
-
}
|
|
12
|
-
},
|
|
13
|
-
"test": {
|
|
14
|
-
"executor": "nx:run-commands",
|
|
15
|
-
"options": {
|
|
16
|
-
"commands": ["npm run test --prefix packages/data-prefetch"]
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
},
|
|
20
|
-
"tags": ["type:pkg"]
|
|
21
|
-
}
|
package/src/cli/babel.ts
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
// no used now
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import type { moduleFederationPlugin } from '@module-federation/sdk';
|
|
4
|
-
|
|
5
|
-
const attribute = 'id';
|
|
6
|
-
const hookId = 'usePrefetch';
|
|
7
|
-
const importPackage = '@module-federation/data-prefetch/react';
|
|
8
|
-
|
|
9
|
-
interface BabelPluginOptions {
|
|
10
|
-
hook_id: string;
|
|
11
|
-
import_pkg: string;
|
|
12
|
-
attribute: string;
|
|
13
|
-
name: string;
|
|
14
|
-
exposes: moduleFederationPlugin.ModuleFederationPluginOptions['exposes'];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
18
|
-
export default (babel: { types: any }, options: BabelPluginOptions) => {
|
|
19
|
-
const t = babel.types;
|
|
20
|
-
let shouldHandle = false;
|
|
21
|
-
let scope = '';
|
|
22
|
-
const { name, exposes } = options;
|
|
23
|
-
if (!exposes) {
|
|
24
|
-
return {};
|
|
25
|
-
}
|
|
26
|
-
const exposesKey = Object.keys(exposes);
|
|
27
|
-
const processedExposes = exposesKey.map((expose) => ({
|
|
28
|
-
key: expose.replace('.', ''),
|
|
29
|
-
value: path.resolve(
|
|
30
|
-
// @ts-ignore
|
|
31
|
-
typeof exposes[expose] === 'string'
|
|
32
|
-
? // @ts-ignore
|
|
33
|
-
exposes[expose]
|
|
34
|
-
: // @ts-ignore
|
|
35
|
-
exposes[expose].import,
|
|
36
|
-
),
|
|
37
|
-
}));
|
|
38
|
-
|
|
39
|
-
return {
|
|
40
|
-
visitor: {
|
|
41
|
-
ImportDeclaration(
|
|
42
|
-
nodePath: {
|
|
43
|
-
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
44
|
-
node: { source: { value: any }; specifiers: any };
|
|
45
|
-
},
|
|
46
|
-
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
47
|
-
state: { file: { opts: { filename: any } } },
|
|
48
|
-
) {
|
|
49
|
-
const source = nodePath.node.source.value;
|
|
50
|
-
const { specifiers } = nodePath.node;
|
|
51
|
-
const { filename } = state.file.opts;
|
|
52
|
-
|
|
53
|
-
if (source === importPackage) {
|
|
54
|
-
shouldHandle = specifiers.some(
|
|
55
|
-
(specifier: { imported: { name: string } }) =>
|
|
56
|
-
specifier.imported &&
|
|
57
|
-
specifier.imported.name === hookId &&
|
|
58
|
-
processedExposes.find(
|
|
59
|
-
// biome-ignore lint/suspicious/noAssignInExpressions: <explanation>
|
|
60
|
-
(expose) => expose.value === filename && (scope = expose.key),
|
|
61
|
-
),
|
|
62
|
-
);
|
|
63
|
-
}
|
|
64
|
-
},
|
|
65
|
-
|
|
66
|
-
CallExpression(nodePath: {
|
|
67
|
-
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
68
|
-
node: { callee: any; arguments: string | any[] };
|
|
69
|
-
}) {
|
|
70
|
-
if (
|
|
71
|
-
shouldHandle &&
|
|
72
|
-
t.isIdentifier(nodePath.node.callee, { name: hookId }) &&
|
|
73
|
-
nodePath.node.arguments.length > 0
|
|
74
|
-
) {
|
|
75
|
-
const objectExpression = nodePath.node.arguments[0];
|
|
76
|
-
if (
|
|
77
|
-
objectExpression &&
|
|
78
|
-
t.isObjectExpression(objectExpression) &&
|
|
79
|
-
!objectExpression.properties.find(
|
|
80
|
-
(p: { key: { name: string } }) => p.key.name === attribute,
|
|
81
|
-
)
|
|
82
|
-
) {
|
|
83
|
-
objectExpression.properties.push(
|
|
84
|
-
t.objectProperty(
|
|
85
|
-
t.identifier(attribute),
|
|
86
|
-
t.stringLiteral(name + scope),
|
|
87
|
-
),
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
},
|
|
92
|
-
},
|
|
93
|
-
};
|
|
94
|
-
};
|
package/src/cli/index.ts
DELETED
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import fs from 'fs-extra';
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
encodeName,
|
|
6
|
-
moduleFederationPlugin,
|
|
7
|
-
MFPrefetchCommon,
|
|
8
|
-
} from '@module-federation/sdk';
|
|
9
|
-
import { normalizeWebpackPath } from '@module-federation/sdk/normalize-webpack-path';
|
|
10
|
-
import type { Compiler, WebpackPluginInstance } from 'webpack';
|
|
11
|
-
|
|
12
|
-
import { TEMP_DIR } from '../common/constant';
|
|
13
|
-
import { fileExistsWithCaseSync, fixPrefetchPath } from '../common/node-utils';
|
|
14
|
-
import { getPrefetchId } from '../common/runtime-utils';
|
|
15
|
-
|
|
16
|
-
const { RuntimeGlobals, Template } = require(
|
|
17
|
-
normalizeWebpackPath('webpack'),
|
|
18
|
-
) as typeof import('webpack');
|
|
19
|
-
|
|
20
|
-
export function getFederationGlobalScope(
|
|
21
|
-
runtimeGlobals: typeof RuntimeGlobals,
|
|
22
|
-
): string {
|
|
23
|
-
return `${runtimeGlobals.require || '__webpack_require__'}.federation`;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export class PrefetchPlugin implements WebpackPluginInstance {
|
|
27
|
-
public options: moduleFederationPlugin.ModuleFederationPluginOptions;
|
|
28
|
-
private _reWriteExports: string;
|
|
29
|
-
|
|
30
|
-
constructor(options: moduleFederationPlugin.ModuleFederationPluginOptions) {
|
|
31
|
-
this.options = options;
|
|
32
|
-
this._reWriteExports = '';
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// eslint-disable-next-line max-lines-per-function
|
|
36
|
-
apply(compiler: Compiler) {
|
|
37
|
-
const { name, exposes } = this.options;
|
|
38
|
-
if (!exposes) {
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
if (!compiler.options.context) {
|
|
42
|
-
throw new Error('compiler.options.context is not defined');
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const { runtimePlugins } = this.options;
|
|
46
|
-
if (!Array.isArray(runtimePlugins)) {
|
|
47
|
-
this.options.runtimePlugins = [];
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const runtimePath = path.resolve(__dirname, '../esm/plugin.js');
|
|
51
|
-
const sharedPath = path.resolve(__dirname, '../esm/shared/index.js');
|
|
52
|
-
if (!this.options.runtimePlugins?.includes(runtimePath)) {
|
|
53
|
-
this.options.runtimePlugins!.push(runtimePath);
|
|
54
|
-
}
|
|
55
|
-
if (!this.options.runtimePlugins?.includes(sharedPath)) {
|
|
56
|
-
this.options.runtimePlugins!.push(sharedPath);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const encodedName = encodeName(name as string);
|
|
60
|
-
const asyncEntryPath = path.resolve(
|
|
61
|
-
compiler.options.context,
|
|
62
|
-
`node_modules/${TEMP_DIR}/${encodedName}/bootstrap.js`,
|
|
63
|
-
);
|
|
64
|
-
if (fs.existsSync(asyncEntryPath)) {
|
|
65
|
-
fs.unlinkSync(asyncEntryPath);
|
|
66
|
-
}
|
|
67
|
-
if (!this.options.dataPrefetch) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const prefetchs: Array<string> = [];
|
|
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
|
-
prefetchs.push(pathItem);
|
|
87
|
-
const absoluteAlias = alias.replace('.', '');
|
|
88
|
-
this._reWriteExports += `export * as ${getPrefetchId(
|
|
89
|
-
`${name}${absoluteAlias}`,
|
|
90
|
-
)} from '${absolutePath}';\n`;
|
|
91
|
-
break;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
if (!this._reWriteExports) {
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
const tempDirRealPath = path.resolve(
|
|
100
|
-
compiler.options.context,
|
|
101
|
-
'node_modules',
|
|
102
|
-
TEMP_DIR,
|
|
103
|
-
);
|
|
104
|
-
if (!fs.existsSync(tempDirRealPath)) {
|
|
105
|
-
fs.mkdirSync(tempDirRealPath);
|
|
106
|
-
}
|
|
107
|
-
if (!fs.existsSync(`${tempDirRealPath}/${encodedName}`)) {
|
|
108
|
-
fs.mkdirSync(`${tempDirRealPath}/${encodedName}`);
|
|
109
|
-
}
|
|
110
|
-
fs.writeFileSync(asyncEntryPath, this._reWriteExports);
|
|
111
|
-
new compiler.webpack.DefinePlugin({
|
|
112
|
-
FederationDataPrefetch: JSON.stringify(asyncEntryPath),
|
|
113
|
-
}).apply(compiler);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
static addRuntime(
|
|
117
|
-
compiler: Compiler,
|
|
118
|
-
options: {
|
|
119
|
-
name: string;
|
|
120
|
-
},
|
|
121
|
-
) {
|
|
122
|
-
const encodedName = encodeName(options.name as string);
|
|
123
|
-
if (!compiler.options.context) {
|
|
124
|
-
throw new Error('compiler.options.context is not defined');
|
|
125
|
-
}
|
|
126
|
-
const prefetchEntry = path.resolve(
|
|
127
|
-
compiler.options.context,
|
|
128
|
-
`node_modules/.mf/${encodedName}/bootstrap.js`,
|
|
129
|
-
);
|
|
130
|
-
const federationGlobal = getFederationGlobalScope(
|
|
131
|
-
RuntimeGlobals || ({} as typeof RuntimeGlobals),
|
|
132
|
-
);
|
|
133
|
-
|
|
134
|
-
return Template.asString([
|
|
135
|
-
fs.existsSync(prefetchEntry)
|
|
136
|
-
? Template.indent([
|
|
137
|
-
'function injectPrefetch() {',
|
|
138
|
-
`globalThis.__FEDERATION__ = globalThis.__FEDERATION__ || {};`,
|
|
139
|
-
`globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}'] = globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}'] || {`,
|
|
140
|
-
`entryLoading: {},`,
|
|
141
|
-
`instance: new Map(),`,
|
|
142
|
-
`__PREFETCH_EXPORTS__: {},`,
|
|
143
|
-
`};`,
|
|
144
|
-
`globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}']['${MFPrefetchCommon.exportsKey}'] = globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}']['${MFPrefetchCommon.exportsKey}'] || {};`,
|
|
145
|
-
`globalThis.__FEDERATION__['${MFPrefetchCommon.globalKey}']['${MFPrefetchCommon.exportsKey}']['${options.name}'] = import('${prefetchEntry}');`,
|
|
146
|
-
'}',
|
|
147
|
-
`${federationGlobal}.prefetch = injectPrefetch`,
|
|
148
|
-
])
|
|
149
|
-
: '',
|
|
150
|
-
Template.indent([
|
|
151
|
-
`if(!${federationGlobal}.isMFRemote && ${federationGlobal}.prefetch){`,
|
|
152
|
-
`${federationGlobal}.prefetch()`,
|
|
153
|
-
'}',
|
|
154
|
-
]),
|
|
155
|
-
]);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
static setRemoteIdentifier() {
|
|
159
|
-
const federationGlobal = getFederationGlobalScope(
|
|
160
|
-
RuntimeGlobals || ({} as typeof RuntimeGlobals),
|
|
161
|
-
);
|
|
162
|
-
return Template.indent([`${federationGlobal}.isMFRemote = true;`]);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
static removeRemoteIdentifier() {
|
|
166
|
-
const federationGlobal = getFederationGlobalScope(
|
|
167
|
-
RuntimeGlobals || ({} as typeof RuntimeGlobals),
|
|
168
|
-
);
|
|
169
|
-
return Template.indent([`${federationGlobal}.isMFRemote = false;`]);
|
|
170
|
-
}
|
|
171
|
-
}
|
package/src/common/constant.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const TEMP_DIR = '.mf';
|
package/src/common/node-utils.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import fs from 'fs-extra';
|
|
3
|
-
|
|
4
|
-
export const fileExistsWithCaseSync = (filepath: string): boolean => {
|
|
5
|
-
const dir = path.dirname(filepath);
|
|
6
|
-
if (filepath === '/' || filepath === '.') {
|
|
7
|
-
return true;
|
|
8
|
-
}
|
|
9
|
-
const filenames = fs.readdirSync(dir);
|
|
10
|
-
if (filenames.indexOf(path.basename(filepath)) === -1) {
|
|
11
|
-
return false;
|
|
12
|
-
}
|
|
13
|
-
return fileExistsWithCaseSync(dir);
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export const fixPrefetchPath = (exposePath: string): Array<string> => {
|
|
17
|
-
const pathExt = ['.js', '.ts'];
|
|
18
|
-
const extReg = /\.(ts|js|tsx|jsx)$/;
|
|
19
|
-
if (extReg.test(exposePath)) {
|
|
20
|
-
return pathExt.map((ext) => exposePath.replace(extReg, `.prefetch${ext}`));
|
|
21
|
-
} else {
|
|
22
|
-
return pathExt.map((ext) => exposePath + `.prefetch${ext}`);
|
|
23
|
-
}
|
|
24
|
-
};
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { getInstance } from '@module-federation/runtime';
|
|
2
|
-
import {
|
|
3
|
-
encodeName,
|
|
4
|
-
ModuleInfo,
|
|
5
|
-
MFPrefetchCommon,
|
|
6
|
-
} from '@module-federation/sdk';
|
|
7
|
-
|
|
8
|
-
export const getScope = (): string => {
|
|
9
|
-
return getInstance()!.options.name;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export const getPrefetchId = (id: string): string =>
|
|
13
|
-
encodeName(`${id}/${MFPrefetchCommon.identifier}`);
|
|
14
|
-
|
|
15
|
-
export const compatGetPrefetchId = (id: string): string =>
|
|
16
|
-
encodeName(`${id}/VmokPrefetch`);
|
|
17
|
-
|
|
18
|
-
export const getSignalFromManifest = (remoteSnapshot: ModuleInfo): boolean => {
|
|
19
|
-
if (!remoteSnapshot) {
|
|
20
|
-
return false;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (
|
|
24
|
-
!('prefetchEntry' in remoteSnapshot) &&
|
|
25
|
-
!('prefetchInterface' in remoteSnapshot)
|
|
26
|
-
) {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (!remoteSnapshot.prefetchEntry && !remoteSnapshot.prefetchInterface) {
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
return true;
|
|
34
|
-
};
|
package/src/logger/index.ts
DELETED