@module-federation/data-prefetch 0.0.0-next-20240617071542

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.
@@ -0,0 +1,213 @@
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_runtime = require("@module-federation/runtime");
29
+ var import_sdk2 = require("@module-federation/sdk");
30
+
31
+ // src/common/runtime-utils.ts
32
+ var import_sdk = require("@module-federation/sdk");
33
+ var getScope = (id) => {
34
+ const idArray = id.split("/");
35
+ if (idArray.length >= 2) {
36
+ idArray.pop();
37
+ }
38
+ const name = idArray.join("/");
39
+ return name;
40
+ };
41
+ var getPrefetchId = (id) => (0, import_sdk.encodeName)(`${id}/${import_sdk.MFPrefetchCommon.identifier}`);
42
+
43
+ // src/prefetch.ts
44
+ globalThis.__FEDERATION__ ?? (globalThis.__FEDERATION__ = {});
45
+ var _a;
46
+ (_a = globalThis.__FEDERATION__).__PREFETCH__ ?? (_a.__PREFETCH__ = {
47
+ entryLoading: {},
48
+ instance: /* @__PURE__ */ new Map(),
49
+ __PREFETCH_EXPORTS__: {}
50
+ });
51
+ var MFDataPrefetch = class {
52
+ constructor(options) {
53
+ this.prefetchMemory = /* @__PURE__ */ new Map();
54
+ this.recordOutdate = {};
55
+ this._exports = {};
56
+ this._options = options;
57
+ this.global.instance.set(options.name, this);
58
+ }
59
+ get global() {
60
+ return globalThis.__FEDERATION__.__PREFETCH__;
61
+ }
62
+ static getInstance(id) {
63
+ return globalThis.__FEDERATION__.__PREFETCH__.instance.get(id);
64
+ }
65
+ async loadEntry(entry) {
66
+ const { name, remoteSnapshot, remote, origin } = this._options;
67
+ if (entry) {
68
+ const { buildVersion, globalName } = remoteSnapshot;
69
+ const uniqueKey = globalName || `${name}:${buildVersion}`;
70
+ if (!this.global.entryLoading[uniqueKey]) {
71
+ this.global.entryLoading[uniqueKey] = (0, import_sdk2.loadScript)(entry, {});
72
+ }
73
+ return this.global.entryLoading[uniqueKey];
74
+ } else {
75
+ const remoteInfo = (0, import_runtime.getRemoteInfo)(remote);
76
+ const module2 = origin.moduleCache.get(remoteInfo.name);
77
+ return (0, import_runtime.getRemoteEntry)({
78
+ remoteInfo,
79
+ remoteEntryExports: module2 ? module2.remoteEntryExports : void 0,
80
+ createScriptHook: (url) => {
81
+ const res = origin.loaderHook.lifecycle.createScript.emit({
82
+ url
83
+ });
84
+ if (res instanceof HTMLScriptElement) {
85
+ return res;
86
+ }
87
+ return;
88
+ }
89
+ });
90
+ }
91
+ }
92
+ getProjectExports() {
93
+ var _a2;
94
+ if (Object.keys(this._exports).length > 0) {
95
+ return this._exports;
96
+ }
97
+ const { name } = this._options;
98
+ const exportsPromise = (_a2 = globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__) == null ? void 0 : _a2[name];
99
+ const resolve = exportsPromise.then(
100
+ (exports = {}) => {
101
+ const memory = {};
102
+ Object.keys(exports).forEach((key) => {
103
+ memory[key] = {};
104
+ const exportVal = exports[key];
105
+ Object.keys(exportVal).reduce(
106
+ (memo, current) => {
107
+ if (current.toLocaleLowerCase().endsWith("prefetch") || current.toLocaleLowerCase() === "default") {
108
+ memo[current] = exportVal[current];
109
+ }
110
+ return memo;
111
+ },
112
+ memory[key]
113
+ );
114
+ });
115
+ this.memorizeExports(memory);
116
+ }
117
+ );
118
+ return resolve;
119
+ }
120
+ memorizeExports(exports) {
121
+ this._exports = exports;
122
+ }
123
+ getExposeExports(id) {
124
+ const prefetchId = getPrefetchId(id);
125
+ const prefetchExports = this._exports[prefetchId];
126
+ return prefetchExports || {};
127
+ }
128
+ prefetch(prefetchOptions) {
129
+ const { id, functionId = "default", refetchParams } = prefetchOptions;
130
+ let prefetchResult;
131
+ const prefetchId = getPrefetchId(id);
132
+ const memorizeId = id + functionId;
133
+ const memory = this.prefetchMemory.get(memorizeId);
134
+ if (!this.checkOutdate(prefetchOptions) && memory) {
135
+ return memory;
136
+ }
137
+ const prefetchExports = this._exports[prefetchId];
138
+ if (!prefetchExports) {
139
+ return;
140
+ }
141
+ const executePrefetch = prefetchExports[functionId];
142
+ if (typeof executePrefetch === "function") {
143
+ if (refetchParams) {
144
+ prefetchResult = executePrefetch(refetchParams);
145
+ } else {
146
+ prefetchResult = executePrefetch();
147
+ }
148
+ } else {
149
+ throw new Error(
150
+ `[Module Federation Data Prefetch]: No prefetch function called ${functionId} export in prefetch file`
151
+ );
152
+ }
153
+ this.memorize(memorizeId, prefetchResult);
154
+ return prefetchResult;
155
+ }
156
+ memorize(id, value) {
157
+ this.prefetchMemory.set(id, value);
158
+ }
159
+ markOutdate(markOptions, isOutdate) {
160
+ const { id, functionId = "default" } = markOptions;
161
+ if (!this.recordOutdate[id]) {
162
+ this.recordOutdate[id] = {};
163
+ }
164
+ this.recordOutdate[id][functionId] = isOutdate;
165
+ }
166
+ checkOutdate(outdateOptions) {
167
+ const { id, functionId = "default", cacheStrategy } = outdateOptions;
168
+ if (typeof cacheStrategy === "function") {
169
+ return cacheStrategy();
170
+ }
171
+ if (!this.recordOutdate[id]) {
172
+ this.recordOutdate[id] = {};
173
+ }
174
+ if (this.recordOutdate[id][functionId]) {
175
+ this.markOutdate(
176
+ {
177
+ id,
178
+ functionId
179
+ },
180
+ false
181
+ );
182
+ return true;
183
+ } else {
184
+ return false;
185
+ }
186
+ }
187
+ };
188
+
189
+ // src/universal/index.ts
190
+ function prefetch(options) {
191
+ const { id, functionId } = options;
192
+ const mfScope = getScope(id);
193
+ const prefetchInstance = MFDataPrefetch.getInstance(mfScope) || new MFDataPrefetch({
194
+ name: mfScope
195
+ });
196
+ const res = prefetchInstance.getProjectExports();
197
+ if (res instanceof Promise) {
198
+ const promise = res.then(() => {
199
+ const result = prefetchInstance.prefetch(options);
200
+ prefetchInstance.memorize(id + functionId, result);
201
+ return result;
202
+ });
203
+ return promise;
204
+ } else {
205
+ const result = prefetchInstance.prefetch(options);
206
+ prefetchInstance.memorize(id + functionId, result);
207
+ return result;
208
+ }
209
+ }
210
+ // Annotate the CommonJS export names for ESM import in node:
211
+ 0 && (module.exports = {
212
+ prefetch
213
+ });
package/jest.config.js ADDED
@@ -0,0 +1,29 @@
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'],
25
+ },
26
+ rootDir: __dirname,
27
+ testMatch: ['<rootDir>__tests__/**/**.spec.[jt]s?(x)'],
28
+ testPathIgnorePatterns: ['/node_modules/', '/dev/'],
29
+ };
package/package.json ADDED
@@ -0,0 +1,88 @@
1
+ {
2
+ "name": "@module-federation/data-prefetch",
3
+ "description": "Module Federation Data Prefetch",
4
+ "version": "0.0.0-next-20240617071542",
5
+ "author": "nieyan <nyqykk@foxmail.com>",
6
+ "homepage": "https://github.com/module-federation/core",
7
+ "license": "MIT",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/esm/index.js",
14
+ "require": "./dist/index.js",
15
+ "types": "./dist/index.d.ts"
16
+ },
17
+ "./react": {
18
+ "import": "./dist/esm/react/index.js",
19
+ "require": "./dist/react/index.js",
20
+ "types": "./dist/react/index.d.ts"
21
+ },
22
+ "./cli": {
23
+ "import": "./dist/esm/cli/index.js",
24
+ "require": "./dist/cli/index.js",
25
+ "types": "./dist/cli/index.d.ts"
26
+ },
27
+ "./babel-plugin": {
28
+ "import": "./dist/esm/cli/babel.js",
29
+ "require": "./dist/cli/babel.js",
30
+ "types": "./dist/cli/babel.d.ts"
31
+ },
32
+ "./universal": {
33
+ "import": "./dist/esm/universal/index.js",
34
+ "require": "./dist/universal/index.js",
35
+ "types": "./dist/universal/index.d.ts"
36
+ }
37
+ },
38
+ "typesVersions": {
39
+ "*": {
40
+ ".": [
41
+ "./dist/index.d.ts"
42
+ ],
43
+ "react": [
44
+ "./dist/react/index.d.ts"
45
+ ],
46
+ "cli": [
47
+ "./dist/cli/index.d.ts"
48
+ ],
49
+ "universal": [
50
+ "./dist/universal/index.d.ts"
51
+ ]
52
+ }
53
+ },
54
+ "main": "dist/index.js",
55
+ "module": "dist/esm/index.js",
56
+ "peerDependencies": {
57
+ "react": ">=16.9.0",
58
+ "react-dom": ">=16.9.0"
59
+ },
60
+ "devDependencies": {
61
+ "@testing-library/react-hooks": "^8.0.1",
62
+ "@types/fs-extra": "9.0.6",
63
+ "@types/jest": "^29.5.11",
64
+ "@types/node": "^17.0.45",
65
+ "@types/react": "~18.0.38",
66
+ "esbuild-plugin-replace": "^1.4.0",
67
+ "jest": "^29.7.0",
68
+ "jest-environment-jsdom": "^29.7.0",
69
+ "minimist": "^1.2.8",
70
+ "react": "^18.2.0",
71
+ "react-dom": "^18.2.0",
72
+ "react-router": "^6.21.3",
73
+ "react-test-renderer": "^18.2.0",
74
+ "ts-jest": "29.0.1",
75
+ "tsup": "6.2.0",
76
+ "webpack": "5.75.0"
77
+ },
78
+ "dependencies": {
79
+ "fs-extra": "9.1.0",
80
+ "@module-federation/sdk": "0.0.0-next-20240617071542",
81
+ "@module-federation/runtime": "0.0.0-next-20240617071542"
82
+ },
83
+ "scripts": {
84
+ "dev": "cross-env WATCH=true tsup",
85
+ "build": "rm -rf dist && tsup",
86
+ "test": "jest"
87
+ }
88
+ }
package/project.json ADDED
@@ -0,0 +1,21 @@
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/tsconfig.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "compilerOptions": {
3
+ "baseUrl": ".",
4
+ "rootDir": "./",
5
+ "outDir": "dist",
6
+ "sourceMap": false,
7
+ "module": "commonjs",
8
+ "target": "ES2015",
9
+ "skipLibCheck": true,
10
+ "moduleResolution": "node",
11
+ "allowJs": false,
12
+ "strict": true,
13
+ "types": ["jest", "node"],
14
+ "experimentalDecorators": true,
15
+ "resolveJsonModule": true,
16
+ "allowSyntheticDefaultImports": true,
17
+ "esModuleInterop": true,
18
+ "removeComments": true,
19
+ "declaration": true,
20
+ "paths": {
21
+ "@/*": ["./*"],
22
+ "@src/*": ["./src/*"]
23
+ }
24
+ },
25
+ "include": ["src", "../../global.d.ts", "__tests__/**/*", "tsup.config.ts"],
26
+ "exclude": ["node_modules/**/*", "../node_modules"]
27
+ }