@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.
Files changed (114) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +3 -1
  3. package/dist/LICENSE +21 -0
  4. package/dist/babel.cjs.cjs +54 -0
  5. package/dist/babel.cjs.cjs.map +1 -0
  6. package/dist/babel.cjs.d.ts +2 -0
  7. package/dist/babel.d.ts +2 -0
  8. package/dist/babel.esm.js +52 -0
  9. package/dist/babel.esm.js.map +1 -0
  10. package/dist/cli.cjs.cjs +156 -0
  11. package/dist/cli.cjs.cjs.map +1 -0
  12. package/dist/cli.cjs.d.ts +1 -0
  13. package/dist/cli.d.ts +1 -0
  14. package/dist/cli.esm.js +153 -0
  15. package/dist/cli.esm.js.map +1 -0
  16. package/dist/constant.cjs.cjs +6 -0
  17. package/dist/constant.cjs.cjs.map +1 -0
  18. package/dist/constant.esm.js +4 -0
  19. package/dist/constant.esm.js.map +1 -0
  20. package/dist/index.cjs.cjs +10 -0
  21. package/dist/index.cjs.cjs.map +1 -0
  22. package/dist/index.cjs.d.ts +1 -0
  23. package/dist/index.cjs2.cjs +8 -0
  24. package/dist/index.cjs2.cjs.map +1 -0
  25. package/dist/index.d.ts +1 -5
  26. package/dist/index.esm.js +3 -0
  27. package/dist/index.esm.js.map +1 -0
  28. package/dist/index.esm2.js +6 -0
  29. package/dist/index.esm2.js.map +1 -0
  30. package/dist/plugin.cjs.cjs +158 -0
  31. package/dist/plugin.cjs.cjs.map +1 -0
  32. package/dist/plugin.cjs.d.ts +2 -0
  33. package/dist/plugin.d.ts +2 -5
  34. package/dist/plugin.esm.js +153 -0
  35. package/dist/plugin.esm.js.map +1 -0
  36. package/dist/prefetch.cjs.cjs +182 -0
  37. package/dist/prefetch.cjs.cjs.map +1 -0
  38. package/dist/prefetch.esm.js +179 -0
  39. package/dist/prefetch.esm.js.map +1 -0
  40. package/dist/react.cjs.cjs +76 -0
  41. package/dist/react.cjs.cjs.map +1 -0
  42. package/dist/react.cjs.d.ts +1 -0
  43. package/dist/react.d.ts +1 -0
  44. package/dist/react.esm.js +74 -0
  45. package/dist/react.esm.js.map +1 -0
  46. package/dist/runtime-utils.cjs.cjs +29 -0
  47. package/dist/runtime-utils.cjs.cjs.map +1 -0
  48. package/dist/runtime-utils.esm.js +24 -0
  49. package/dist/runtime-utils.esm.js.map +1 -0
  50. package/dist/{cli → src/cli}/babel.d.ts +2 -4
  51. package/dist/{cli → src/cli}/index.d.ts +5 -8
  52. package/dist/src/common/constant.d.ts +1 -0
  53. package/dist/src/common/node-utils.d.ts +2 -0
  54. package/dist/src/common/runtime-utils.d.ts +5 -0
  55. package/dist/src/constant.d.ts +1 -0
  56. package/dist/src/logger/index.d.ts +2 -0
  57. package/dist/src/plugin.d.ts +3 -0
  58. package/dist/{prefetch-4e9646e4.d.ts → src/prefetch.d.ts} +14 -17
  59. package/dist/src/react/hooks.d.ts +11 -0
  60. package/dist/src/react/utils.d.ts +1 -0
  61. package/dist/src/universal/index.d.ts +2 -0
  62. package/dist/universal.cjs.cjs +30 -0
  63. package/dist/universal.cjs.cjs.map +1 -0
  64. package/dist/universal.cjs.d.ts +1 -0
  65. package/dist/universal.d.ts +1 -0
  66. package/dist/universal.esm.js +28 -0
  67. package/dist/universal.esm.js.map +1 -0
  68. package/package.json +49 -39
  69. package/CHANGELOG.md +0 -9
  70. package/__tests__/babel.spec.ts +0 -75
  71. package/__tests__/prefetch.spec.ts +0 -138
  72. package/__tests__/react.spec.ts +0 -119
  73. package/dist/cli/babel.js +0 -84
  74. package/dist/cli/index.js +0 -205
  75. package/dist/esm/chunk-AJPO2B2T.js +0 -30
  76. package/dist/esm/chunk-EZUCZHGV.js +0 -11
  77. package/dist/esm/chunk-ISLXMAIA.js +0 -155
  78. package/dist/esm/chunk-KI4QGPIS.js +0 -32
  79. package/dist/esm/chunk-TTJJJ2WZ.js +0 -7
  80. package/dist/esm/chunk-VVZ6XIM6.js +0 -166
  81. package/dist/esm/cli/babel.js +0 -57
  82. package/dist/esm/cli/index.js +0 -178
  83. package/dist/esm/index.js +0 -13
  84. package/dist/esm/plugin.js +0 -12
  85. package/dist/esm/react/index.js +0 -94
  86. package/dist/esm/shared/index.js +0 -27
  87. package/dist/esm/universal/index.js +0 -9
  88. package/dist/index.js +0 -350
  89. package/dist/plugin.js +0 -346
  90. package/dist/react/index.d.ts +0 -16
  91. package/dist/react/index.js +0 -286
  92. package/dist/shared/index.d.ts +0 -5
  93. package/dist/shared/index.js +0 -48
  94. package/dist/universal/index.d.ts +0 -8
  95. package/dist/universal/index.js +0 -204
  96. package/jest.config.js +0 -29
  97. package/project.json +0 -21
  98. package/src/cli/babel.ts +0 -94
  99. package/src/cli/index.ts +0 -171
  100. package/src/common/constant.ts +0 -1
  101. package/src/common/node-utils.ts +0 -24
  102. package/src/common/runtime-utils.ts +0 -34
  103. package/src/logger/index.ts +0 -3
  104. package/src/plugin.ts +0 -191
  105. package/src/prefetch.ts +0 -207
  106. package/src/react/hooks.ts +0 -95
  107. package/src/react/utils.ts +0 -11
  108. package/src/shared/index.ts +0 -26
  109. package/src/universal/index.ts +0 -27
  110. package/tsconfig.json +0 -27
  111. package/tsup.config.ts +0 -35
  112. /package/{src/common/index.ts → dist/src/common/index.d.ts} +0 -0
  113. /package/{src/index.ts → dist/src/index.d.ts} +0 -0
  114. /package/{src/react/index.ts → dist/src/react/index.d.ts} +0 -0
@@ -0,0 +1,2 @@
1
+ import { type prefetchOptions } from '../prefetch';
2
+ export declare function prefetch(options: prefetchOptions): Promise<any>;
@@ -0,0 +1,30 @@
1
+ 'use strict';
2
+
3
+ var prefetch$1 = require('./prefetch.cjs.cjs');
4
+ var runtimeUtils = require('./runtime-utils.cjs.cjs');
5
+
6
+ function prefetch(options) {
7
+ const { id, functionId = 'default' } = options;
8
+ const mfScope = runtimeUtils.getScope();
9
+ const prefetchInstance = prefetch$1.MFDataPrefetch.getInstance(mfScope) ||
10
+ new prefetch$1.MFDataPrefetch({
11
+ name: mfScope,
12
+ });
13
+ const res = prefetchInstance.getProjectExports();
14
+ if (res instanceof Promise) {
15
+ const promise = res.then(() => {
16
+ const result = prefetchInstance.prefetch(options);
17
+ prefetchInstance.memorize(id + functionId, result);
18
+ return result;
19
+ });
20
+ return promise;
21
+ }
22
+ else {
23
+ const result = prefetchInstance.prefetch(options);
24
+ prefetchInstance.memorize(id + functionId, result);
25
+ return result;
26
+ }
27
+ }
28
+
29
+ exports.prefetch = prefetch;
30
+ //# sourceMappingURL=universal.cjs.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"universal.cjs.cjs","sources":["../src/universal/index.ts"],"sourcesContent":["import { MFDataPrefetch } from '../prefetch';\nimport { getScope } from '../common/runtime-utils';\nexport function prefetch(options) {\n const { id, functionId = 'default' } = options;\n const mfScope = getScope();\n const prefetchInstance = MFDataPrefetch.getInstance(mfScope) ||\n new MFDataPrefetch({\n name: mfScope,\n });\n const res = prefetchInstance.getProjectExports();\n if (res instanceof Promise) {\n const promise = res.then(() => {\n const result = prefetchInstance.prefetch(options);\n prefetchInstance.memorize(id + functionId, result);\n return result;\n });\n return promise;\n }\n else {\n const result = prefetchInstance.prefetch(options);\n prefetchInstance.memorize(id + functionId, result);\n return result;\n }\n}\n"],"names":["getScope","MFDataPrefetch"],"mappings":";;;;;AAEO,SAAS,QAAQ,CAAC,OAAO,EAAE;AAClC,IAAI,MAAM,EAAE,EAAE,EAAE,UAAU,GAAG,SAAS,EAAE,GAAG,OAAO;AAClD,IAAI,MAAM,OAAO,GAAGA,qBAAQ,EAAE;AAC9B,IAAI,MAAM,gBAAgB,GAAGC,yBAAc,CAAC,WAAW,CAAC,OAAO,CAAC;AAChE,QAAQ,IAAIA,yBAAc,CAAC;AAC3B,YAAY,IAAI,EAAE,OAAO;AACzB,SAAS,CAAC;AACV,IAAI,MAAM,GAAG,GAAG,gBAAgB,CAAC,iBAAiB,EAAE;AACpD,IAAI,IAAI,GAAG,YAAY,OAAO,EAAE;AAChC,QAAQ,MAAM,OAAO,GAAG,GAAG,CAAC,IAAI,CAAC,MAAM;AACvC,YAAY,MAAM,MAAM,GAAG,gBAAgB,CAAC,QAAQ,CAAC,OAAO,CAAC;AAC7D,YAAY,gBAAgB,CAAC,QAAQ,CAAC,EAAE,GAAG,UAAU,EAAE,MAAM,CAAC;AAC9D,YAAY,OAAO,MAAM;AACzB,QAAQ,CAAC,CAAC;AACV,QAAQ,OAAO,OAAO;AACtB,IAAI;AACJ,SAAS;AACT,QAAQ,MAAM,MAAM,GAAG,gBAAgB,CAAC,QAAQ,CAAC,OAAO,CAAC;AACzD,QAAQ,gBAAgB,CAAC,QAAQ,CAAC,EAAE,GAAG,UAAU,EAAE,MAAM,CAAC;AAC1D,QAAQ,OAAO,MAAM;AACrB,IAAI;AACJ;;;;"}
@@ -0,0 +1 @@
1
+ export * from "./src/universal/index";
@@ -0,0 +1 @@
1
+ export * from "./src/universal/index";
@@ -0,0 +1,28 @@
1
+ import { M as MFDataPrefetch } from './prefetch.esm.js';
2
+ import { g as getScope } from './runtime-utils.esm.js';
3
+
4
+ function prefetch(options) {
5
+ const { id, functionId = 'default' } = options;
6
+ const mfScope = getScope();
7
+ const prefetchInstance = MFDataPrefetch.getInstance(mfScope) ||
8
+ new MFDataPrefetch({
9
+ name: mfScope,
10
+ });
11
+ const res = prefetchInstance.getProjectExports();
12
+ if (res instanceof Promise) {
13
+ const promise = res.then(() => {
14
+ const result = prefetchInstance.prefetch(options);
15
+ prefetchInstance.memorize(id + functionId, result);
16
+ return result;
17
+ });
18
+ return promise;
19
+ }
20
+ else {
21
+ const result = prefetchInstance.prefetch(options);
22
+ prefetchInstance.memorize(id + functionId, result);
23
+ return result;
24
+ }
25
+ }
26
+
27
+ export { prefetch };
28
+ //# sourceMappingURL=universal.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"universal.esm.js","sources":["../src/universal/index.ts"],"sourcesContent":["import { MFDataPrefetch } from '../prefetch';\nimport { getScope } from '../common/runtime-utils';\nexport function prefetch(options) {\n const { id, functionId = 'default' } = options;\n const mfScope = getScope();\n const prefetchInstance = MFDataPrefetch.getInstance(mfScope) ||\n new MFDataPrefetch({\n name: mfScope,\n });\n const res = prefetchInstance.getProjectExports();\n if (res instanceof Promise) {\n const promise = res.then(() => {\n const result = prefetchInstance.prefetch(options);\n prefetchInstance.memorize(id + functionId, result);\n return result;\n });\n return promise;\n }\n else {\n const result = prefetchInstance.prefetch(options);\n prefetchInstance.memorize(id + functionId, result);\n return result;\n }\n}\n"],"names":[],"mappings":";;;AAEO,SAAS,QAAQ,CAAC,OAAO,EAAE;AAClC,IAAI,MAAM,EAAE,EAAE,EAAE,UAAU,GAAG,SAAS,EAAE,GAAG,OAAO;AAClD,IAAI,MAAM,OAAO,GAAG,QAAQ,EAAE;AAC9B,IAAI,MAAM,gBAAgB,GAAG,cAAc,CAAC,WAAW,CAAC,OAAO,CAAC;AAChE,QAAQ,IAAI,cAAc,CAAC;AAC3B,YAAY,IAAI,EAAE,OAAO;AACzB,SAAS,CAAC;AACV,IAAI,MAAM,GAAG,GAAG,gBAAgB,CAAC,iBAAiB,EAAE;AACpD,IAAI,IAAI,GAAG,YAAY,OAAO,EAAE;AAChC,QAAQ,MAAM,OAAO,GAAG,GAAG,CAAC,IAAI,CAAC,MAAM;AACvC,YAAY,MAAM,MAAM,GAAG,gBAAgB,CAAC,QAAQ,CAAC,OAAO,CAAC;AAC7D,YAAY,gBAAgB,CAAC,QAAQ,CAAC,EAAE,GAAG,UAAU,EAAE,MAAM,CAAC;AAC9D,YAAY,OAAO,MAAM;AACzB,QAAQ,CAAC,CAAC;AACV,QAAQ,OAAO,OAAO;AACtB,IAAI;AACJ,SAAS;AACT,QAAQ,MAAM,MAAM,GAAG,gBAAgB,CAAC,QAAQ,CAAC,OAAO,CAAC;AACzD,QAAQ,gBAAgB,CAAC,QAAQ,CAAC,EAAE,GAAG,UAAU,EAAE,MAAM,CAAC;AAC1D,QAAQ,OAAO,MAAM;AACrB,IAAI;AACJ;;;;"}
package/package.json CHANGED
@@ -1,43 +1,49 @@
1
1
  {
2
2
  "name": "@module-federation/data-prefetch",
3
3
  "description": "Module Federation Data Prefetch",
4
- "version": "1.0.16",
4
+ "version": "2.0.0",
5
+ "type": "module",
5
6
  "author": "nieyan <nyqykk@foxmail.com>",
6
7
  "homepage": "https://github.com/module-federation/core",
7
8
  "license": "MIT",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/module-federation/core.git",
12
+ "directory": "packages/data-prefetch"
13
+ },
8
14
  "publishConfig": {
9
15
  "access": "public"
10
16
  },
17
+ "files": [
18
+ "dist",
19
+ "README.md",
20
+ "LICENSE"
21
+ ],
11
22
  "exports": {
12
23
  ".": {
13
- "import": "./dist/esm/index.js",
14
- "require": "./dist/index.js",
15
- "types": "./dist/index.d.ts"
24
+ "types": "./dist/index.d.ts",
25
+ "import": "./dist/index.esm.js",
26
+ "require": "./dist/index.cjs.cjs"
16
27
  },
17
28
  "./react": {
18
- "import": "./dist/esm/react/index.js",
19
- "require": "./dist/react/index.js",
20
- "types": "./dist/react/index.d.ts"
29
+ "types": "./dist/react.d.ts",
30
+ "import": "./dist/react.esm.js",
31
+ "require": "./dist/react.cjs.cjs"
21
32
  },
22
33
  "./cli": {
23
- "import": "./dist/esm/cli/index.js",
24
- "require": "./dist/cli/index.js",
25
- "types": "./dist/cli/index.d.ts"
34
+ "types": "./dist/cli.d.ts",
35
+ "import": "./dist/cli.esm.js",
36
+ "require": "./dist/cli.cjs.cjs"
26
37
  },
27
38
  "./babel-plugin": {
28
- "import": "./dist/esm/cli/babel.js",
29
- "require": "./dist/cli/babel.js",
30
- "types": "./dist/cli/babel.d.ts"
39
+ "types": "./dist/babel.d.ts",
40
+ "import": "./dist/babel.esm.js",
41
+ "require": "./dist/babel.cjs.cjs"
31
42
  },
32
43
  "./universal": {
33
- "import": "./dist/esm/universal/index.js",
34
- "require": "./dist/universal/index.js",
35
- "types": "./dist/universal/index.d.ts"
36
- },
37
- "./shared": {
38
- "import": "./dist/esm/shared/index.js",
39
- "require": "./dist/shared/index.js",
40
- "types": "./dist/shared/index.d.ts"
44
+ "types": "./dist/universal.d.ts",
45
+ "import": "./dist/universal.esm.js",
46
+ "require": "./dist/universal.cjs.cjs"
41
47
  }
42
48
  },
43
49
  "typesVersions": {
@@ -46,51 +52,55 @@
46
52
  "./dist/index.d.ts"
47
53
  ],
48
54
  "react": [
49
- "./dist/react/index.d.ts"
55
+ "./dist/react.d.ts"
50
56
  ],
51
57
  "cli": [
52
- "./dist/cli/index.d.ts"
58
+ "./dist/cli.d.ts"
53
59
  ],
54
60
  "universal": [
55
- "./dist/universal/index.d.ts"
61
+ "./dist/universal.d.ts"
56
62
  ],
57
- "shared": [
58
- "./dist/shared/index.d.ts"
63
+ "babel-plugin": [
64
+ "./dist/babel.d.ts"
59
65
  ]
60
66
  }
61
67
  },
62
- "main": "dist/index.js",
63
- "module": "dist/esm/index.js",
68
+ "main": "dist/index.cjs.cjs",
69
+ "module": "dist/index.esm.js",
64
70
  "peerDependencies": {
65
71
  "react": ">=16.9.0",
66
72
  "react-dom": ">=16.9.0"
67
73
  },
74
+ "peerDependenciesMeta": {
75
+ "react": {
76
+ "optional": true
77
+ },
78
+ "react-dom": {
79
+ "optional": true
80
+ }
81
+ },
68
82
  "devDependencies": {
69
83
  "@testing-library/react-hooks": "^8.0.1",
70
- "@types/fs-extra": "9.0.6",
84
+ "@types/fs-extra": "9.0.13",
71
85
  "@types/jest": "^29.5.11",
72
- "@types/node": "^17.0.45",
86
+ "@types/node": "^20.19.5",
73
87
  "@types/react": "~18.0.38",
74
- "esbuild-plugin-replace": "^1.4.0",
75
88
  "jest": "^29.7.0",
76
89
  "jest-environment-jsdom": "^29.7.0",
77
90
  "minimist": "^1.2.8",
78
- "react": "^18.2.0",
79
- "react-dom": "^18.2.0",
80
- "react-router": "^6.21.3",
91
+ "react": "^18.3.1",
92
+ "react-dom": "^18.3.1",
93
+ "react-router": "^6.30.2",
81
94
  "react-test-renderer": "^18.2.0",
82
95
  "ts-jest": "29.0.1",
83
- "tsup": "6.2.0",
84
96
  "webpack": "5.75.0"
85
97
  },
86
98
  "dependencies": {
87
99
  "fs-extra": "9.1.0",
88
- "@module-federation/sdk": "0.6.5",
89
- "@module-federation/runtime": "0.6.5"
100
+ "@module-federation/sdk": "2.0.0",
101
+ "@module-federation/runtime": "2.0.0"
90
102
  },
91
103
  "scripts": {
92
- "dev": "cross-env WATCH=true tsup",
93
- "build": "rm -rf dist && tsup",
94
104
  "test": "jest"
95
105
  }
96
106
  }
package/CHANGELOG.md DELETED
@@ -1,9 +0,0 @@
1
- @module-federation/data-prefetch
2
-
3
- ## 1.0.16
4
-
5
- ### Patch Changes
6
-
7
- - 3070e26: feat(@module-federation/data-prefetch): support data prefetch in Module Federation
8
- - @module-federation/runtime@0.6.5
9
- - @module-federation/sdk@0.6.5
@@ -1,75 +0,0 @@
1
- /// <reference types="jest" />
2
-
3
- import path from 'path';
4
- import fs from 'fs';
5
- import { BabelFileResult, transformFileSync } from '@babel/core';
6
- // @ts-ignore no use
7
- import type from '@types/jest';
8
-
9
- import babelPlugin from '../src/cli/babel';
10
-
11
- const emptyRegexp = /\s+/g;
12
- describe('Babel Plugin Test', () => {
13
- const options = {
14
- name: '@mf/test',
15
- exposes: { './expose': path.resolve(__dirname, 'test') },
16
- };
17
-
18
- const testFilePath = path.join(__dirname, './test');
19
-
20
- beforeEach(() => {
21
- fs.mkdirSync(path.dirname(testFilePath), { recursive: true });
22
- fs.closeSync(fs.openSync(testFilePath, 'w'));
23
- });
24
-
25
- afterEach(() => {
26
- fs.unlinkSync(testFilePath);
27
- });
28
-
29
- // Check if the plugin will add the id parameter when it is not present
30
- test('it adds id argument to usePrefetch call when it is absent', () => {
31
- const input = `
32
- import { usePrefetch } from "@module-federation/data-prefetch/react";
33
- usePrefetch({});
34
- `;
35
-
36
- const expected = `
37
- import { usePrefetch } from "@module-federation/data-prefetch/react";
38
- usePrefetch({ id: "@mf/test/expose" });
39
- `;
40
-
41
- fs.writeFileSync(testFilePath, input);
42
- const { code } = transformFileSync(testFilePath, {
43
- plugins: [[babelPlugin, options]],
44
- configFile: false,
45
- }) as BabelFileResult;
46
-
47
- expect(code?.replace(emptyRegexp, '')).toBe(
48
- expected.replace(emptyRegexp, ''),
49
- );
50
- });
51
-
52
- // The plugin should retain the existing id parameter in the usePrefetch call
53
- test('it does not overwrite existing id argument in usePrefetch call', () => {
54
- const input = `
55
- import { usePrefetch } from "@module-federation/data-prefetch/react";
56
- usePrefetch({ id: "existingId" });
57
- `;
58
-
59
- const expected = `
60
- import { usePrefetch } from "@module-federation/data-prefetch/react";
61
- usePrefetch({ id: "existingId" });
62
- `;
63
-
64
- fs.writeFileSync(testFilePath, input);
65
- const { code } = transformFileSync(testFilePath, {
66
- plugins: [[babelPlugin, options]],
67
- configFile: false,
68
- babelrc: false,
69
- }) as BabelFileResult;
70
-
71
- expect(code?.replace(emptyRegexp, '')).toBe(
72
- expected.replace(emptyRegexp, ''),
73
- );
74
- });
75
- });
@@ -1,138 +0,0 @@
1
- // Import the necessary modules and functions
2
- import { MFDataPrefetch } from '../src/prefetch';
3
- import {
4
- loadScript,
5
- MFPrefetchCommon,
6
- encodeName,
7
- } from '@module-federation/sdk';
8
-
9
- // Mock loadScript function from SDK
10
- jest.mock('@module-federation/sdk', () => {
11
- const originalModule = jest.requireActual('@module-federation/sdk');
12
- return {
13
- ...originalModule,
14
- loadScript: jest.fn(() => Promise.resolve()),
15
- };
16
- });
17
-
18
- describe('MF Data Prefetch', () => {
19
- let prefetch: MFDataPrefetch;
20
-
21
- const options = {
22
- name: '@mf/test',
23
- remoteSnapshot: {
24
- buildVersion: '1.0.0',
25
- globalName: 'TestGlobalName',
26
- },
27
- };
28
- const exposeId = `${options.name}/button/${MFPrefetchCommon.identifier}`;
29
-
30
- beforeEach(() => {
31
- globalThis.__FEDERATION__.__PREFETCH__ = {
32
- entryLoading: {},
33
- instance: new Map(),
34
- __PREFETCH_EXPORTS__: {},
35
- };
36
- // @ts-ignore
37
- prefetch = new MFDataPrefetch(options);
38
- });
39
- afterAll(() => {
40
- // @ts-ignore
41
- delete globalThis.__FEDERATION__;
42
- });
43
-
44
- // Instance gets added to global memory on creation
45
- it('adds itself to global instances on creation', () => {
46
- expect(prefetch.global.instance.get(options.name)).toBe(prefetch);
47
- });
48
-
49
- // Loads entry script using loadScript from sdk
50
- it('loads entry script using loadScript from sdk', async () => {
51
- const url = 'testUrl'; // Url of the script to be loaded
52
-
53
- await prefetch.loadEntry(url); // Call `loadEntry` function
54
-
55
- // Expect that the loadScript function is called with the correct url
56
- expect(loadScript).toHaveBeenCalledWith(url, expect.any(Object));
57
- });
58
-
59
- // Retrieves project exports
60
- it('gets project exports', async () => {
61
- const exposeExport = {
62
- nyPrefetch: () => {},
63
- };
64
- const projectExport = {
65
- [encodeName(exposeId)]: exposeExport,
66
- };
67
- globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__[options.name] =
68
- Promise.resolve(projectExport);
69
-
70
- await prefetch.getProjectExports();
71
- expect(prefetch.getExposeExports(`${options.name}/button`)).toEqual(
72
- exposeExport,
73
- );
74
- });
75
- // Prefetching with memory and executing prefetch function
76
- it('executes prefetch using prefetch function with and without memory', async () => {
77
- const id = options.name;
78
- const functionId = 'nyPrefetch';
79
- const refetchParams = 'testParams';
80
- const prefetchOptions = { id: `${id}/button`, functionId, refetchParams };
81
-
82
- // Creating a mock prefetch function
83
- const executePrefetch = jest.fn(() => 'Expected Result');
84
- const prefetchExports = { [functionId]: executePrefetch };
85
-
86
- // Mock Project Exports
87
- globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__[id] =
88
- Promise.resolve({
89
- [encodeName(exposeId)]: prefetchExports,
90
- });
91
-
92
- await prefetch.getProjectExports();
93
- // Call the prefetch function first time
94
- let result = await prefetch.prefetch(prefetchOptions);
95
-
96
- // Verify that executePrefetch function is correctly executed
97
- expect(executePrefetch).toHaveBeenCalled();
98
-
99
- // Clear mock function calls data
100
- executePrefetch.mockClear();
101
-
102
- // Call the prefetch function again
103
- result = await prefetch.prefetch(prefetchOptions);
104
-
105
- // Verify that executePrefetch function is NOT called this time (since the result should come from memory)
106
- expect(executePrefetch).not.toHaveBeenCalled();
107
-
108
- // Clear mock function calls data
109
- executePrefetch.mockClear();
110
-
111
- prefetch.markOutdate(prefetchOptions, true);
112
-
113
- // Call the prefetch function first time
114
- result = await prefetch.prefetch(prefetchOptions);
115
-
116
- // Verify that executePrefetch function is correctly executed
117
- expect(executePrefetch).toHaveBeenCalled();
118
- });
119
-
120
- // Checking outdate marking
121
- it('checks outdate marking', () => {
122
- const markOptions = { id: 'testId', functionId: 'testFunction' };
123
-
124
- // Mark the function as outdated
125
- prefetch.markOutdate(markOptions, true);
126
-
127
- // Verify that the function is marked as outdated
128
- let isOutdated = prefetch.checkOutdate(markOptions);
129
- expect(isOutdated).toBe(true);
130
-
131
- // Mark the function as up-to-date
132
- prefetch.markOutdate(markOptions, false);
133
-
134
- // Verify that the function is marked as up-to-date
135
- isOutdated = prefetch.checkOutdate(markOptions);
136
- expect(isOutdated).toBe(false);
137
- });
138
- });
@@ -1,119 +0,0 @@
1
- import { FederationHost, init } from '@module-federation/runtime';
2
- import { renderHook, act } from '@testing-library/react-hooks';
3
- import * as ModuleFederationSDK from '@module-federation/sdk';
4
- import { usePrefetch } from '../src/react';
5
- import { MFDataPrefetch } from '../src/prefetch';
6
-
7
- const mockLoadScript = jest.spyOn(ModuleFederationSDK, 'loadScript');
8
- mockLoadScript.mockImplementation(() => Promise.resolve());
9
-
10
- describe('usePrefetch', () => {
11
- // Mock prefetch function
12
- global.fetch = jest.fn(() =>
13
- Promise.resolve({
14
- ok: true,
15
- status: 200,
16
- statusText: 'OK',
17
- redirected: false,
18
- type: 'basic',
19
- url: '',
20
- clone: jest.fn(),
21
- headers: new Headers(),
22
- body: null,
23
- bodyUsed: false,
24
- text: () => Promise.resolve(JSON.stringify({ data: 'testData' })),
25
- json: () => Promise.resolve({ data: 'testData' }),
26
- formData: () => Promise.resolve(new FormData()),
27
- arrayBuffer: () => Promise.resolve(new ArrayBuffer(0)),
28
- blob: () => Promise.resolve(new Blob()),
29
- }),
30
- );
31
-
32
- let prefetch: MFDataPrefetch;
33
-
34
- const options = {
35
- // Options that will be passed to the `MFDataPrefetch` constructor
36
- name: '@mf/test',
37
- remoteSnapshot: {
38
- buildVersion: '1.0.0',
39
- globalName: 'TestGlobalName',
40
- },
41
- };
42
- const testData = 'testData';
43
- const newTestData = 'newTestData';
44
- const functionId = 'testPrefetch';
45
- const executePrefetch = jest.fn((params: string) =>
46
- Promise.resolve(params ? params : testData),
47
- );
48
-
49
- new FederationHost({
50
- name: options.name,
51
- remotes: [],
52
- });
53
- init({
54
- name: options.name,
55
- remotes: [],
56
- });
57
- beforeEach(() => {
58
- globalThis.__FEDERATION__.__PREFETCH__ = {
59
- entryLoading: {},
60
- instance: new Map(),
61
- __PREFETCH_EXPORTS__: {},
62
- };
63
- // @ts-ignore
64
- prefetch = new MFDataPrefetch(options); // Create a new instance of `MFDataPrefetch` class before each test
65
-
66
- const exposeExport = {
67
- [functionId]: executePrefetch,
68
- };
69
- const exposeId = `${options.name}/button/${ModuleFederationSDK.MFPrefetchCommon.identifier}`;
70
- const projectExport = {
71
- [ModuleFederationSDK.encodeName(exposeId)]: exposeExport,
72
- };
73
- globalThis.__FEDERATION__.__PREFETCH__.__PREFETCH_EXPORTS__[options.name] =
74
- Promise.resolve(projectExport);
75
- });
76
-
77
- afterEach(() => {
78
- executePrefetch.mockClear();
79
- mockLoadScript.mockRestore();
80
- });
81
-
82
- afterAll(() => {
83
- // @ts-ignore
84
- delete globalThis.__FEDERATION__;
85
- });
86
-
87
- it('should prefetch data on first mount', async () => {
88
- const { result } = renderHook(() =>
89
- usePrefetch({ id: `${options.name}/button`, functionId }),
90
- );
91
- await result.current[0];
92
- expect(executePrefetch).toHaveBeenCalled();
93
- // Verify the prefechState
94
- expect(result.current[0]).resolves.toEqual(testData);
95
- });
96
-
97
- it('should refetch data when refreshExecutor is called', async () => {
98
- const { result } = renderHook(() =>
99
- usePrefetch({ id: `${options.name}/button`, functionId }),
100
- );
101
-
102
- await result.current[0];
103
- expect(executePrefetch).toHaveBeenCalled();
104
- executePrefetch.mockClear();
105
- const { result: newCallResult } = renderHook(() =>
106
- usePrefetch({ id: `${options.name}/button`, functionId }),
107
- );
108
- await newCallResult.current[0];
109
- expect(executePrefetch).not.toHaveBeenCalled();
110
- // Call refreshExecutor
111
- act(() => {
112
- result.current[1](newTestData);
113
- });
114
-
115
- expect(executePrefetch).toHaveBeenCalled();
116
- // // Verify the prefetchState after refetch
117
- expect(result.current[0]).resolves.toEqual(newTestData);
118
- });
119
- });
package/dist/cli/babel.js DELETED
@@ -1,84 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
- mod
23
- ));
24
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
-
26
- // src/cli/babel.ts
27
- var babel_exports = {};
28
- __export(babel_exports, {
29
- default: () => babel_default
30
- });
31
- module.exports = __toCommonJS(babel_exports);
32
- var import_path = __toESM(require("path"));
33
- var attribute = "id";
34
- var hookId = "usePrefetch";
35
- var importPackage = "@module-federation/data-prefetch/react";
36
- var babel_default = (babel, options) => {
37
- const t = babel.types;
38
- let shouldHandle = false;
39
- let scope = "";
40
- const { name, exposes } = options;
41
- if (!exposes) {
42
- return {};
43
- }
44
- const exposesKey = Object.keys(exposes);
45
- const processedExposes = exposesKey.map((expose) => ({
46
- key: expose.replace(".", ""),
47
- value: import_path.default.resolve(
48
- typeof exposes[expose] === "string" ? exposes[expose] : exposes[expose].import
49
- )
50
- }));
51
- return {
52
- visitor: {
53
- ImportDeclaration(nodePath, state) {
54
- const source = nodePath.node.source.value;
55
- const { specifiers } = nodePath.node;
56
- const { filename } = state.file.opts;
57
- if (source === importPackage) {
58
- shouldHandle = specifiers.some(
59
- (specifier) => specifier.imported && specifier.imported.name === hookId && processedExposes.find(
60
- (expose) => expose.value === filename && (scope = expose.key)
61
- )
62
- );
63
- }
64
- },
65
- CallExpression(nodePath) {
66
- if (shouldHandle && t.isIdentifier(nodePath.node.callee, { name: hookId }) && nodePath.node.arguments.length > 0) {
67
- const objectExpression = nodePath.node.arguments[0];
68
- if (objectExpression && t.isObjectExpression(objectExpression) && !objectExpression.properties.find(
69
- (p) => p.key.name === attribute
70
- )) {
71
- objectExpression.properties.push(
72
- t.objectProperty(
73
- t.identifier(attribute),
74
- t.stringLiteral(name + scope)
75
- )
76
- );
77
- }
78
- }
79
- }
80
- }
81
- };
82
- };
83
- // Annotate the CommonJS export names for ESM import in node:
84
- 0 && (module.exports = {});