@module-federation/data-prefetch 1.0.16 → 2.0.1

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 (98) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +3 -1
  3. package/dist/LICENSE +21 -0
  4. package/dist/babel.cjs +95 -0
  5. package/dist/babel.d.ts +1 -0
  6. package/dist/babel.js +5 -0
  7. package/dist/cli/babel.cjs +115 -0
  8. package/dist/cli/babel.d.ts +2 -4
  9. package/dist/cli/babel.js +50 -82
  10. package/dist/cli/index.cjs +217 -0
  11. package/dist/cli/index.d.ts +5 -8
  12. package/dist/cli/index.js +150 -197
  13. package/dist/cli.cjs +95 -0
  14. package/dist/cli.d.ts +1 -0
  15. package/dist/cli.js +5 -0
  16. package/dist/common/constant.cjs +51 -0
  17. package/dist/common/constant.d.ts +1 -0
  18. package/dist/common/constant.js +5 -0
  19. package/dist/common/index.cjs +95 -0
  20. package/dist/common/index.js +5 -0
  21. package/dist/common/node-utils.cjs +100 -0
  22. package/dist/common/node-utils.d.ts +2 -0
  23. package/dist/common/node-utils.js +35 -0
  24. package/dist/common/runtime-utils.cjs +84 -0
  25. package/dist/common/runtime-utils.d.ts +5 -0
  26. package/dist/common/runtime-utils.js +29 -0
  27. package/dist/constant.cjs +51 -0
  28. package/dist/constant.d.ts +1 -0
  29. package/dist/constant.js +5 -0
  30. package/dist/index.cjs +106 -0
  31. package/dist/index.d.ts +2 -5
  32. package/dist/index.js +3 -346
  33. package/dist/logger/index.cjs +60 -0
  34. package/dist/logger/index.d.ts +2 -0
  35. package/dist/logger/index.js +10 -0
  36. package/dist/plugin.cjs +234 -0
  37. package/dist/plugin.d.ts +3 -5
  38. package/dist/plugin.js +167 -340
  39. package/dist/prefetch.cjs +197 -0
  40. package/dist/{prefetch-4e9646e4.d.ts → prefetch.d.ts} +14 -17
  41. package/dist/prefetch.js +149 -0
  42. package/dist/react/hooks.cjs +144 -0
  43. package/dist/react/hooks.d.ts +11 -0
  44. package/dist/react/hooks.js +86 -0
  45. package/dist/react/index.cjs +95 -0
  46. package/dist/react/index.d.ts +1 -16
  47. package/dist/react/index.js +2 -283
  48. package/dist/react/utils.cjs +65 -0
  49. package/dist/react/utils.d.ts +1 -0
  50. package/dist/react/utils.js +15 -0
  51. package/dist/react.cjs +95 -0
  52. package/dist/react.d.ts +1 -0
  53. package/dist/react.js +5 -0
  54. package/dist/universal/index.cjs +81 -0
  55. package/dist/universal/index.d.ts +2 -8
  56. package/dist/universal/index.js +24 -196
  57. package/dist/universal.cjs +95 -0
  58. package/dist/universal.d.ts +1 -0
  59. package/dist/universal.js +5 -0
  60. package/package.json +76 -40
  61. package/CHANGELOG.md +0 -9
  62. package/__tests__/babel.spec.ts +0 -75
  63. package/__tests__/prefetch.spec.ts +0 -138
  64. package/__tests__/react.spec.ts +0 -119
  65. package/dist/esm/chunk-AJPO2B2T.js +0 -30
  66. package/dist/esm/chunk-EZUCZHGV.js +0 -11
  67. package/dist/esm/chunk-ISLXMAIA.js +0 -155
  68. package/dist/esm/chunk-KI4QGPIS.js +0 -32
  69. package/dist/esm/chunk-TTJJJ2WZ.js +0 -7
  70. package/dist/esm/chunk-VVZ6XIM6.js +0 -166
  71. package/dist/esm/cli/babel.js +0 -57
  72. package/dist/esm/cli/index.js +0 -178
  73. package/dist/esm/index.js +0 -13
  74. package/dist/esm/plugin.js +0 -12
  75. package/dist/esm/react/index.js +0 -94
  76. package/dist/esm/shared/index.js +0 -27
  77. package/dist/esm/universal/index.js +0 -9
  78. package/dist/shared/index.d.ts +0 -5
  79. package/dist/shared/index.js +0 -48
  80. package/jest.config.js +0 -29
  81. package/project.json +0 -21
  82. package/src/cli/babel.ts +0 -94
  83. package/src/cli/index.ts +0 -171
  84. package/src/common/constant.ts +0 -1
  85. package/src/common/node-utils.ts +0 -24
  86. package/src/common/runtime-utils.ts +0 -34
  87. package/src/index.ts +0 -2
  88. package/src/logger/index.ts +0 -3
  89. package/src/plugin.ts +0 -191
  90. package/src/prefetch.ts +0 -207
  91. package/src/react/hooks.ts +0 -95
  92. package/src/react/index.ts +0 -1
  93. package/src/react/utils.ts +0 -11
  94. package/src/shared/index.ts +0 -26
  95. package/src/universal/index.ts +0 -27
  96. package/tsconfig.json +0 -27
  97. package/tsup.config.ts +0 -35
  98. /package/{src/common/index.ts → dist/common/index.d.ts} +0 -0
@@ -1,95 +0,0 @@
1
- import { useEffect, useState } from 'react';
2
- import type { defer } from 'react-router';
3
-
4
- import logger from '../logger';
5
- import { MFDataPrefetch, type prefetchOptions } from '../prefetch';
6
- import { prefetch } from '../universal';
7
- import { getScope } from '../common/runtime-utils';
8
- import { useFirstMounted } from './utils';
9
-
10
- type refetchParams = any;
11
- type DeferredData = ReturnType<typeof defer>;
12
- type prefetchReturnType<T> = [
13
- Promise<T>,
14
- (refetchParams?: refetchParams) => void,
15
- ];
16
-
17
- type UsePrefetchOptions = prefetchOptions & {
18
- deferId?: string;
19
- };
20
-
21
- export const usePrefetch = <T>(
22
- options: UsePrefetchOptions,
23
- ): prefetchReturnType<T> => {
24
- const isFirstMounted = useFirstMounted();
25
- if (isFirstMounted) {
26
- const startTiming = performance.now();
27
- logger.info(
28
- `2. Start Get Prefetch Data: ${options.id} - ${
29
- options.functionId || 'default'
30
- } - ${startTiming}`,
31
- );
32
- }
33
- const { id, functionId, deferId } = options;
34
- const prefetchInfo = {
35
- id,
36
- functionId,
37
- };
38
- const mfScope = getScope();
39
-
40
- let state;
41
- const prefetchResult = prefetch(options);
42
- if (deferId) {
43
- if (prefetchResult instanceof Promise) {
44
- state = (prefetchResult as Promise<DeferredData>).then(
45
- (deferredData) => deferredData.data[deferId],
46
- );
47
- } else {
48
- state = (prefetchResult as DeferredData).data[deferId];
49
- }
50
- } else {
51
- state = prefetchResult;
52
- }
53
- const [prefetchState, setPrefetchState] = useState<Promise<T>>(
54
- state as Promise<T>,
55
- );
56
- const prefetchInstance = MFDataPrefetch.getInstance(mfScope);
57
-
58
- useEffect(() => {
59
- const useEffectTiming = performance.now();
60
- logger.info(
61
- `3. Start Execute UseEffect: ${options.id} - ${
62
- options.functionId || 'default'
63
- } - ${useEffectTiming}`,
64
- );
65
-
66
- return () => {
67
- prefetchInstance?.markOutdate(prefetchInfo, true);
68
- };
69
- }, []);
70
-
71
- const refreshExecutor = (refetchParams?: refetchParams) => {
72
- const refetchOptions = {
73
- ...options,
74
- };
75
- if (refetchParams) {
76
- refetchOptions.refetchParams = refetchParams;
77
- }
78
- prefetchInstance?.markOutdate(prefetchInfo, true);
79
- const newVal = prefetch(refetchOptions) as Promise<DeferredData>;
80
- let newState;
81
- if (deferId) {
82
- if (newVal instanceof Promise) {
83
- newState = newVal.then((deferredData) => deferredData.data[deferId]);
84
- } else {
85
- newState = (newVal as DeferredData).data[deferId];
86
- }
87
- } else {
88
- newState = newVal;
89
- }
90
-
91
- setPrefetchState(newState as Promise<T>);
92
- };
93
-
94
- return [prefetchState, refreshExecutor];
95
- };
@@ -1 +0,0 @@
1
- export * from './hooks';
@@ -1,11 +0,0 @@
1
- import { useEffect, useRef } from 'react';
2
-
3
- export const useFirstMounted = (): boolean => {
4
- const ref = useRef(true);
5
-
6
- useEffect(() => {
7
- ref.current = false;
8
- }, []);
9
-
10
- return ref.current;
11
- };
@@ -1,26 +0,0 @@
1
- import type { FederationRuntimePlugin } from '@module-federation/runtime';
2
-
3
- const sharedStrategy: () => FederationRuntimePlugin = () => ({
4
- name: 'shared-strategy',
5
- beforeInit(args) {
6
- const { userOptions } = args;
7
- const shared = userOptions.shared;
8
- if (shared) {
9
- Object.keys(shared).forEach((sharedKey) => {
10
- const sharedConfigs = shared[sharedKey];
11
- const arraySharedConfigs = Array.isArray(sharedConfigs)
12
- ? sharedConfigs
13
- : [sharedConfigs];
14
- arraySharedConfigs.forEach((s) => {
15
- s.strategy = 'loaded-first';
16
- });
17
- });
18
- console.warn(
19
- `[Module Federation Data Prefetch]: Your shared strategy is set to 'loaded-first', this is a necessary condition for data prefetch`,
20
- );
21
- }
22
- return args;
23
- },
24
- });
25
-
26
- export default sharedStrategy;
@@ -1,27 +0,0 @@
1
- import { MFDataPrefetch, type prefetchOptions } from '../prefetch';
2
- import { getScope } from '../common/runtime-utils';
3
-
4
- export function prefetch(options: prefetchOptions): Promise<any> {
5
- const { id, functionId = 'default' } = options;
6
- const mfScope = getScope();
7
-
8
- const prefetchInstance =
9
- MFDataPrefetch.getInstance(mfScope) ||
10
- new MFDataPrefetch({
11
- name: mfScope,
12
- });
13
-
14
- const res = prefetchInstance.getProjectExports();
15
- if (res instanceof Promise) {
16
- const promise = res.then(() => {
17
- const result = prefetchInstance!.prefetch(options);
18
- prefetchInstance.memorize(id + functionId, result);
19
- return result;
20
- });
21
- return promise;
22
- } else {
23
- const result = prefetchInstance!.prefetch(options);
24
- prefetchInstance.memorize(id + functionId, result);
25
- return result;
26
- }
27
- }
package/tsconfig.json DELETED
@@ -1,27 +0,0 @@
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
- }
package/tsup.config.ts DELETED
@@ -1,35 +0,0 @@
1
- import { replace } from 'esbuild-plugin-replace';
2
- import minimist from 'minimist';
3
- import type { Options } from 'tsup';
4
-
5
- import pkg from './package.json';
6
-
7
- const args = minimist(process.argv.slice(2));
8
- const watch = process.env.WATCH;
9
- const sourceMap = args.sourcemap || args.s;
10
-
11
- export const tsup: Options = {
12
- entry: [
13
- 'src/index.ts',
14
- 'src/react/index.ts',
15
- 'src/cli/index.ts',
16
- 'src/cli/babel.ts',
17
- 'src/universal/index.ts',
18
- 'src/plugin.ts',
19
- 'src/shared/index.ts',
20
- ],
21
- sourcemap: sourceMap,
22
- clean: true,
23
- dts: true,
24
- watch: watch ? 'src/' : false,
25
- format: ['esm', 'cjs'],
26
- legacyOutput: true,
27
- esbuildPlugins: [
28
- replace({
29
- __VERSION__: `'${pkg.version}'`,
30
- __DEV__:
31
- '(typeof process !== "undefined" && process.env && process.env.NODE_ENV ? (process.env.NODE_ENV !== "production") : false)',
32
- __TEST__: 'false',
33
- }),
34
- ],
35
- };
File without changes