@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
@@ -0,0 +1,197 @@
1
+ "use strict";
2
+ const __rslib_import_meta_url__ = /*#__PURE__*/ (function () {
3
+ return typeof document === 'undefined'
4
+ ? new (require('url'.replace('', '')).URL)('file:' + __filename).href
5
+ : (document.currentScript && document.currentScript.src) ||
6
+ new URL('main.js', document.baseURI).href;
7
+ })();
8
+ ;
9
+ // The require scope
10
+ var __webpack_require__ = {};
11
+
12
+ /************************************************************************/
13
+ // webpack/runtime/define_property_getters
14
+ (() => {
15
+ __webpack_require__.d = (exports, definition) => {
16
+ for(var key in definition) {
17
+ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
18
+ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
19
+ }
20
+ }
21
+ };
22
+ })();
23
+ // webpack/runtime/has_own_property
24
+ (() => {
25
+ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
26
+ })();
27
+ // webpack/runtime/make_namespace_object
28
+ (() => {
29
+ // define __esModule on exports
30
+ __webpack_require__.r = (exports) => {
31
+ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
32
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
33
+ }
34
+ Object.defineProperty(exports, '__esModule', { value: true });
35
+ };
36
+ })();
37
+ /************************************************************************/
38
+ var __webpack_exports__ = {};
39
+ // ESM COMPAT FLAG
40
+ __webpack_require__.r(__webpack_exports__);
41
+
42
+ // EXPORTS
43
+ __webpack_require__.d(__webpack_exports__, {
44
+ MFDataPrefetch: () => (/* binding */ MFDataPrefetch)
45
+ });
46
+
47
+ ;// CONCATENATED MODULE: external "@module-federation/runtime"
48
+ const runtime_namespaceObject = require("@module-federation/runtime");
49
+ ;// CONCATENATED MODULE: external "@module-federation/sdk"
50
+ const sdk_namespaceObject = require("@module-federation/sdk");
51
+ ;// CONCATENATED MODULE: external "./common/runtime-utils.cjs"
52
+ const runtime_utils_cjs_namespaceObject = require("./common/runtime-utils.cjs");
53
+ ;// CONCATENATED MODULE: ./src/prefetch.ts
54
+
55
+
56
+
57
+ // @ts-ignore init global variable for test
58
+ const federationGlobal = globalThis;
59
+ federationGlobal.__FEDERATION__ ??= {};
60
+ federationGlobal.__FEDERATION__.__PREFETCH__ ??= {
61
+ entryLoading: {},
62
+ instance: new Map(),
63
+ __PREFETCH_EXPORTS__: {}
64
+ };
65
+ const getFederation = ()=>globalThis.__FEDERATION__;
66
+ class MFDataPrefetch {
67
+ get global() {
68
+ return getFederation().__PREFETCH__;
69
+ }
70
+ static getInstance(id) {
71
+ return getFederation().__PREFETCH__.instance.get(id);
72
+ }
73
+ async loadEntry(entry) {
74
+ const { name, remoteSnapshot, remote, origin } = this._options;
75
+ if (entry) {
76
+ const { buildVersion, globalName } = remoteSnapshot;
77
+ const uniqueKey = globalName || `${name}:${buildVersion}`;
78
+ if (!this.global.entryLoading[uniqueKey]) {
79
+ this.global.entryLoading[uniqueKey] = (0,sdk_namespaceObject.loadScript)(entry, {});
80
+ }
81
+ return this.global.entryLoading[uniqueKey];
82
+ } else {
83
+ const remoteInfo = (0,runtime_namespaceObject.getRemoteInfo)(remote);
84
+ const module = origin.moduleCache.get(remoteInfo.name);
85
+ return (0,runtime_namespaceObject.getRemoteEntry)({
86
+ origin: origin,
87
+ remoteInfo,
88
+ remoteEntryExports: module ? module.remoteEntryExports : undefined
89
+ });
90
+ }
91
+ }
92
+ getProjectExports() {
93
+ var _getFederation___PREFETCH_____PREFETCH_EXPORTS__;
94
+ if (Object.keys(this._exports).length > 0) {
95
+ return this._exports;
96
+ }
97
+ const { name } = this._options;
98
+ const exportsPromiseFn = (_getFederation___PREFETCH_____PREFETCH_EXPORTS__ = getFederation().__PREFETCH__.__PREFETCH_EXPORTS__) === null || _getFederation___PREFETCH_____PREFETCH_EXPORTS__ === void 0 ? void 0 : _getFederation___PREFETCH_____PREFETCH_EXPORTS__[name];
99
+ const exportsPromise = typeof exportsPromiseFn === 'function' ? exportsPromiseFn() : exportsPromiseFn;
100
+ const resolve = exportsPromise.then((exports = {})=>{
101
+ // Match prefetch based on the function name suffix so that other capabilities can be expanded later.
102
+ // Not all functions should be directly identified as prefetch functions
103
+ const memory = {};
104
+ Object.keys(exports).forEach((key)=>{
105
+ memory[key] = {};
106
+ const exportVal = exports[key];
107
+ Object.keys(exportVal).reduce((memo, current)=>{
108
+ if (current.toLocaleLowerCase().endsWith('prefetch') || current.toLocaleLowerCase() === 'default') {
109
+ memo[current] = exportVal[current];
110
+ }
111
+ return memo;
112
+ }, memory[key]);
113
+ });
114
+ this.memorizeExports(memory);
115
+ });
116
+ return resolve;
117
+ }
118
+ memorizeExports(exports) {
119
+ this._exports = exports;
120
+ }
121
+ getExposeExports(id) {
122
+ const prefetchId = (0,runtime_utils_cjs_namespaceObject.getPrefetchId)(id);
123
+ const compatId = (0,runtime_utils_cjs_namespaceObject.compatGetPrefetchId)(id);
124
+ const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
125
+ return prefetchExports || {};
126
+ }
127
+ prefetch(prefetchOptions) {
128
+ const { id, functionId = 'default', refetchParams } = prefetchOptions;
129
+ let prefetchResult;
130
+ const prefetchId = (0,runtime_utils_cjs_namespaceObject.getPrefetchId)(id);
131
+ const compatId = (0,runtime_utils_cjs_namespaceObject.compatGetPrefetchId)(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] || this._exports[compatId];
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(`[Module Federation Data Prefetch]: No prefetch function called ${functionId} export in prefetch file`);
150
+ }
151
+ this.memorize(memorizeId, prefetchResult);
152
+ return prefetchResult;
153
+ }
154
+ memorize(id, value) {
155
+ this.prefetchMemory.set(id, value);
156
+ }
157
+ markOutdate(markOptions, isOutdate) {
158
+ const { id, functionId = 'default' } = markOptions;
159
+ if (!this.recordOutdate[id]) {
160
+ this.recordOutdate[id] = {};
161
+ }
162
+ this.recordOutdate[id][functionId] = isOutdate;
163
+ }
164
+ checkOutdate(outdateOptions) {
165
+ const { id, functionId = 'default', cacheStrategy } = outdateOptions;
166
+ if (typeof cacheStrategy === 'function') {
167
+ return cacheStrategy();
168
+ }
169
+ if (!this.recordOutdate[id]) {
170
+ this.recordOutdate[id] = {};
171
+ }
172
+ if (this.recordOutdate[id][functionId]) {
173
+ this.markOutdate({
174
+ id,
175
+ functionId
176
+ }, false);
177
+ return true;
178
+ } else {
179
+ return false;
180
+ }
181
+ }
182
+ constructor(options){
183
+ this.prefetchMemory = new Map();
184
+ this.recordOutdate = {};
185
+ this._exports = {};
186
+ this._options = options;
187
+ this.global.instance.set(options.name, this);
188
+ }
189
+ }
190
+
191
+ exports.MFDataPrefetch = __webpack_exports__.MFDataPrefetch;
192
+ for(var __webpack_i__ in __webpack_exports__) {
193
+ if(["MFDataPrefetch"].indexOf(__webpack_i__) === -1) {
194
+ exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
195
+ }
196
+ }
197
+ Object.defineProperty(exports, '__esModule', { value: true });
@@ -1,36 +1,34 @@
1
- import { FederationHost } from '@module-federation/runtime';
1
+ import { ModuleFederation } from '@module-federation/runtime';
2
2
  import { ModuleInfo } from '@module-federation/sdk';
3
3
  import { Remote } from '@module-federation/runtime/types';
4
-
5
- declare module '@module-federation/runtime' {
6
- interface Federation {
7
- __PREFETCH__: {
8
- entryLoading: Record<string, undefined | Promise<void>>;
9
- instance: Map<string, MFDataPrefetch>;
10
- __PREFETCH_EXPORTS__: Record<string, Promise<Record<string, any>>>;
11
- };
12
- }
4
+ import type { Federation } from '@module-federation/runtime';
5
+ interface FederationWithPrefetch extends Federation {
6
+ __PREFETCH__: {
7
+ entryLoading: Record<string, undefined | Promise<void>>;
8
+ instance: Map<string, MFDataPrefetch>;
9
+ __PREFETCH_EXPORTS__: Record<string, () => Promise<Record<string, any>>>;
10
+ };
13
11
  }
14
12
  type PrefetchExports = Record<string, any>;
15
- interface DataPrefetchOptions {
13
+ export interface DataPrefetchOptions {
16
14
  name: string;
17
15
  remote?: Remote;
18
- origin?: FederationHost;
16
+ origin?: ModuleFederation;
19
17
  remoteSnapshot?: ModuleInfo;
20
18
  }
21
- interface prefetchOptions {
19
+ export interface prefetchOptions {
22
20
  id: string;
23
21
  functionId?: string;
24
22
  cacheStrategy?: () => boolean;
25
23
  refetchParams?: any;
26
24
  }
27
- declare class MFDataPrefetch {
25
+ export declare class MFDataPrefetch {
28
26
  prefetchMemory: Map<string, Promise<any>>;
29
27
  recordOutdate: Record<string, Record<string, boolean>>;
30
28
  private _exports;
31
29
  private _options;
32
30
  constructor(options: DataPrefetchOptions);
33
- get global(): Record<string, any>;
31
+ get global(): FederationWithPrefetch['__PREFETCH__'];
34
32
  static getInstance(id: string): MFDataPrefetch | undefined;
35
33
  loadEntry(entry: string | undefined): Promise<any>;
36
34
  getProjectExports(): Promise<void> | Record<string, any>;
@@ -41,5 +39,4 @@ declare class MFDataPrefetch {
41
39
  markOutdate(markOptions: Omit<prefetchOptions, 'cacheStrategy'>, isOutdate: boolean): void;
42
40
  checkOutdate(outdateOptions: prefetchOptions): boolean;
43
41
  }
44
-
45
- export { DataPrefetchOptions as D, MFDataPrefetch as M, prefetchOptions as p };
42
+ export {};
@@ -0,0 +1,149 @@
1
+ import { getRemoteEntry, getRemoteInfo } from "@module-federation/runtime";
2
+ import { loadScript } from "@module-federation/sdk";
3
+ import { compatGetPrefetchId, getPrefetchId } from "./common/runtime-utils.js";
4
+
5
+ ;// CONCATENATED MODULE: external "@module-federation/runtime"
6
+
7
+ ;// CONCATENATED MODULE: external "@module-federation/sdk"
8
+
9
+ ;// CONCATENATED MODULE: external "./common/runtime-utils.js"
10
+
11
+ ;// CONCATENATED MODULE: ./src/prefetch.ts
12
+
13
+
14
+
15
+ // @ts-ignore init global variable for test
16
+ const federationGlobal = globalThis;
17
+ federationGlobal.__FEDERATION__ ??= {};
18
+ federationGlobal.__FEDERATION__.__PREFETCH__ ??= {
19
+ entryLoading: {},
20
+ instance: new Map(),
21
+ __PREFETCH_EXPORTS__: {}
22
+ };
23
+ const getFederation = ()=>globalThis.__FEDERATION__;
24
+ class MFDataPrefetch {
25
+ get global() {
26
+ return getFederation().__PREFETCH__;
27
+ }
28
+ static getInstance(id) {
29
+ return getFederation().__PREFETCH__.instance.get(id);
30
+ }
31
+ async loadEntry(entry) {
32
+ const { name, remoteSnapshot, remote, origin } = this._options;
33
+ if (entry) {
34
+ const { buildVersion, globalName } = remoteSnapshot;
35
+ const uniqueKey = globalName || `${name}:${buildVersion}`;
36
+ if (!this.global.entryLoading[uniqueKey]) {
37
+ this.global.entryLoading[uniqueKey] = loadScript(entry, {});
38
+ }
39
+ return this.global.entryLoading[uniqueKey];
40
+ } else {
41
+ const remoteInfo = getRemoteInfo(remote);
42
+ const module = origin.moduleCache.get(remoteInfo.name);
43
+ return getRemoteEntry({
44
+ origin: origin,
45
+ remoteInfo,
46
+ remoteEntryExports: module ? module.remoteEntryExports : undefined
47
+ });
48
+ }
49
+ }
50
+ getProjectExports() {
51
+ var _getFederation___PREFETCH_____PREFETCH_EXPORTS__;
52
+ if (Object.keys(this._exports).length > 0) {
53
+ return this._exports;
54
+ }
55
+ const { name } = this._options;
56
+ const exportsPromiseFn = (_getFederation___PREFETCH_____PREFETCH_EXPORTS__ = getFederation().__PREFETCH__.__PREFETCH_EXPORTS__) === null || _getFederation___PREFETCH_____PREFETCH_EXPORTS__ === void 0 ? void 0 : _getFederation___PREFETCH_____PREFETCH_EXPORTS__[name];
57
+ const exportsPromise = typeof exportsPromiseFn === 'function' ? exportsPromiseFn() : exportsPromiseFn;
58
+ const resolve = exportsPromise.then((exports = {})=>{
59
+ // Match prefetch based on the function name suffix so that other capabilities can be expanded later.
60
+ // Not all functions should be directly identified as prefetch functions
61
+ const memory = {};
62
+ Object.keys(exports).forEach((key)=>{
63
+ memory[key] = {};
64
+ const exportVal = exports[key];
65
+ Object.keys(exportVal).reduce((memo, current)=>{
66
+ if (current.toLocaleLowerCase().endsWith('prefetch') || current.toLocaleLowerCase() === 'default') {
67
+ memo[current] = exportVal[current];
68
+ }
69
+ return memo;
70
+ }, memory[key]);
71
+ });
72
+ this.memorizeExports(memory);
73
+ });
74
+ return resolve;
75
+ }
76
+ memorizeExports(exports) {
77
+ this._exports = exports;
78
+ }
79
+ getExposeExports(id) {
80
+ const prefetchId = getPrefetchId(id);
81
+ const compatId = compatGetPrefetchId(id);
82
+ const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
83
+ return prefetchExports || {};
84
+ }
85
+ prefetch(prefetchOptions) {
86
+ const { id, functionId = 'default', refetchParams } = prefetchOptions;
87
+ let prefetchResult;
88
+ const prefetchId = getPrefetchId(id);
89
+ const compatId = compatGetPrefetchId(id);
90
+ const memorizeId = id + functionId;
91
+ const memory = this.prefetchMemory.get(memorizeId);
92
+ if (!this.checkOutdate(prefetchOptions) && memory) {
93
+ return memory;
94
+ }
95
+ const prefetchExports = this._exports[prefetchId] || this._exports[compatId];
96
+ if (!prefetchExports) {
97
+ return;
98
+ }
99
+ const executePrefetch = prefetchExports[functionId];
100
+ if (typeof executePrefetch === 'function') {
101
+ if (refetchParams) {
102
+ prefetchResult = executePrefetch(refetchParams);
103
+ } else {
104
+ prefetchResult = executePrefetch();
105
+ }
106
+ } else {
107
+ throw new Error(`[Module Federation Data Prefetch]: No prefetch function called ${functionId} export in prefetch file`);
108
+ }
109
+ this.memorize(memorizeId, prefetchResult);
110
+ return prefetchResult;
111
+ }
112
+ memorize(id, value) {
113
+ this.prefetchMemory.set(id, value);
114
+ }
115
+ markOutdate(markOptions, isOutdate) {
116
+ const { id, functionId = 'default' } = markOptions;
117
+ if (!this.recordOutdate[id]) {
118
+ this.recordOutdate[id] = {};
119
+ }
120
+ this.recordOutdate[id][functionId] = isOutdate;
121
+ }
122
+ checkOutdate(outdateOptions) {
123
+ const { id, functionId = 'default', cacheStrategy } = outdateOptions;
124
+ if (typeof cacheStrategy === 'function') {
125
+ return cacheStrategy();
126
+ }
127
+ if (!this.recordOutdate[id]) {
128
+ this.recordOutdate[id] = {};
129
+ }
130
+ if (this.recordOutdate[id][functionId]) {
131
+ this.markOutdate({
132
+ id,
133
+ functionId
134
+ }, false);
135
+ return true;
136
+ } else {
137
+ return false;
138
+ }
139
+ }
140
+ constructor(options){
141
+ this.prefetchMemory = new Map();
142
+ this.recordOutdate = {};
143
+ this._exports = {};
144
+ this._options = options;
145
+ this.global.instance.set(options.name, this);
146
+ }
147
+ }
148
+
149
+ export { MFDataPrefetch };
@@ -0,0 +1,144 @@
1
+ "use strict";
2
+ const __rslib_import_meta_url__ = /*#__PURE__*/ (function () {
3
+ return typeof document === 'undefined'
4
+ ? new (require('url'.replace('', '')).URL)('file:' + __filename).href
5
+ : (document.currentScript && document.currentScript.src) ||
6
+ new URL('main.js', document.baseURI).href;
7
+ })();
8
+ ;
9
+ // The require scope
10
+ var __webpack_require__ = {};
11
+
12
+ /************************************************************************/
13
+ // webpack/runtime/compat_get_default_export
14
+ (() => {
15
+ // getDefaultExport function for compatibility with non-ESM modules
16
+ __webpack_require__.n = (module) => {
17
+ var getter = module && module.__esModule ?
18
+ () => (module['default']) :
19
+ () => (module);
20
+ __webpack_require__.d(getter, { a: getter });
21
+ return getter;
22
+ };
23
+
24
+ })();
25
+ // webpack/runtime/define_property_getters
26
+ (() => {
27
+ __webpack_require__.d = (exports, definition) => {
28
+ for(var key in definition) {
29
+ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
30
+ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
31
+ }
32
+ }
33
+ };
34
+ })();
35
+ // webpack/runtime/has_own_property
36
+ (() => {
37
+ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
38
+ })();
39
+ // webpack/runtime/make_namespace_object
40
+ (() => {
41
+ // define __esModule on exports
42
+ __webpack_require__.r = (exports) => {
43
+ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
44
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
45
+ }
46
+ Object.defineProperty(exports, '__esModule', { value: true });
47
+ };
48
+ })();
49
+ /************************************************************************/
50
+ var __webpack_exports__ = {};
51
+ // ESM COMPAT FLAG
52
+ __webpack_require__.r(__webpack_exports__);
53
+
54
+ // EXPORTS
55
+ __webpack_require__.d(__webpack_exports__, {
56
+ usePrefetch: () => (/* binding */ usePrefetch)
57
+ });
58
+
59
+ ;// CONCATENATED MODULE: external "react"
60
+ const external_react_namespaceObject = require("react");
61
+ ;// CONCATENATED MODULE: external "../logger/index.cjs"
62
+ const index_cjs_namespaceObject = require("../logger/index.cjs");
63
+ var index_cjs_default = /*#__PURE__*/__webpack_require__.n(index_cjs_namespaceObject);
64
+ ;// CONCATENATED MODULE: external "../prefetch.cjs"
65
+ const external_prefetch_cjs_namespaceObject = require("../prefetch.cjs");
66
+ ;// CONCATENATED MODULE: external "../universal.cjs"
67
+ const external_universal_cjs_namespaceObject = require("../universal.cjs");
68
+ ;// CONCATENATED MODULE: external "../common/runtime-utils.cjs"
69
+ const runtime_utils_cjs_namespaceObject = require("../common/runtime-utils.cjs");
70
+ ;// CONCATENATED MODULE: external "./utils.cjs"
71
+ const external_utils_cjs_namespaceObject = require("./utils.cjs");
72
+ ;// CONCATENATED MODULE: ./src/react/hooks.ts
73
+
74
+
75
+
76
+
77
+
78
+
79
+ const usePrefetch = (options)=>{
80
+ const isFirstMounted = (0,external_utils_cjs_namespaceObject.useFirstMounted)();
81
+ if (isFirstMounted) {
82
+ const startTiming = performance.now();
83
+ index_cjs_default().info(`2. Start Get Prefetch Data: ${options.id} - ${options.functionId || 'default'} - ${startTiming}`);
84
+ }
85
+ const { id, functionId, deferId } = options;
86
+ const prefetchInfo = {
87
+ id,
88
+ functionId
89
+ };
90
+ const mfScope = (0,runtime_utils_cjs_namespaceObject.getScope)();
91
+ let state;
92
+ const prefetchResult = (0,external_universal_cjs_namespaceObject.prefetch)(options);
93
+ if (deferId) {
94
+ if (prefetchResult instanceof Promise) {
95
+ state = prefetchResult.then((deferredData)=>deferredData.data[deferId]);
96
+ } else {
97
+ state = prefetchResult.data[deferId];
98
+ }
99
+ } else {
100
+ state = prefetchResult;
101
+ }
102
+ const [prefetchState, setPrefetchState] = (0,external_react_namespaceObject.useState)(state);
103
+ const prefetchInstance = external_prefetch_cjs_namespaceObject.MFDataPrefetch.getInstance(mfScope);
104
+ (0,external_react_namespaceObject.useEffect)(()=>{
105
+ const useEffectTiming = performance.now();
106
+ index_cjs_default().info(`3. Start Execute UseEffect: ${options.id} - ${options.functionId || 'default'} - ${useEffectTiming}`);
107
+ return ()=>{
108
+ prefetchInstance === null || prefetchInstance === void 0 ? void 0 : prefetchInstance.markOutdate(prefetchInfo, true);
109
+ };
110
+ }, []);
111
+ const refreshExecutor = (refetchParams)=>{
112
+ const refetchOptions = {
113
+ ...options
114
+ };
115
+ if (refetchParams) {
116
+ refetchOptions.refetchParams = refetchParams;
117
+ }
118
+ prefetchInstance === null || prefetchInstance === void 0 ? void 0 : prefetchInstance.markOutdate(prefetchInfo, true);
119
+ const newVal = (0,external_universal_cjs_namespaceObject.prefetch)(refetchOptions);
120
+ let newState;
121
+ if (deferId) {
122
+ if (newVal instanceof Promise) {
123
+ newState = newVal.then((deferredData)=>deferredData.data[deferId]);
124
+ } else {
125
+ newState = newVal.data[deferId];
126
+ }
127
+ } else {
128
+ newState = newVal;
129
+ }
130
+ setPrefetchState(newState);
131
+ };
132
+ return [
133
+ prefetchState,
134
+ refreshExecutor
135
+ ];
136
+ };
137
+
138
+ exports.usePrefetch = __webpack_exports__.usePrefetch;
139
+ for(var __webpack_i__ in __webpack_exports__) {
140
+ if(["usePrefetch"].indexOf(__webpack_i__) === -1) {
141
+ exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
142
+ }
143
+ }
144
+ Object.defineProperty(exports, '__esModule', { value: true });
@@ -0,0 +1,11 @@
1
+ import { type prefetchOptions } from '../prefetch';
2
+ type refetchParams = any;
3
+ type prefetchReturnType<T> = [
4
+ Promise<T>,
5
+ (refetchParams?: refetchParams) => void
6
+ ];
7
+ type UsePrefetchOptions = prefetchOptions & {
8
+ deferId?: string;
9
+ };
10
+ export declare const usePrefetch: <T>(options: UsePrefetchOptions) => prefetchReturnType<T>;
11
+ export {};
@@ -0,0 +1,86 @@
1
+ import { useEffect, useState } from "react";
2
+ import logger from "../logger/index.js";
3
+ import { MFDataPrefetch } from "../prefetch.js";
4
+ import { prefetch } from "../universal.js";
5
+ import { getScope } from "../common/runtime-utils.js";
6
+ import { useFirstMounted } from "./utils.js";
7
+
8
+ ;// CONCATENATED MODULE: external "react"
9
+
10
+ ;// CONCATENATED MODULE: external "../logger/index.js"
11
+
12
+ ;// CONCATENATED MODULE: external "../prefetch.js"
13
+
14
+ ;// CONCATENATED MODULE: external "../universal.js"
15
+
16
+ ;// CONCATENATED MODULE: external "../common/runtime-utils.js"
17
+
18
+ ;// CONCATENATED MODULE: external "./utils.js"
19
+
20
+ ;// CONCATENATED MODULE: ./src/react/hooks.ts
21
+
22
+
23
+
24
+
25
+
26
+
27
+ const usePrefetch = (options)=>{
28
+ const isFirstMounted = useFirstMounted();
29
+ if (isFirstMounted) {
30
+ const startTiming = performance.now();
31
+ logger.info(`2. Start Get Prefetch Data: ${options.id} - ${options.functionId || 'default'} - ${startTiming}`);
32
+ }
33
+ const { id, functionId, deferId } = options;
34
+ const prefetchInfo = {
35
+ id,
36
+ functionId
37
+ };
38
+ const mfScope = getScope();
39
+ let state;
40
+ const prefetchResult = prefetch(options);
41
+ if (deferId) {
42
+ if (prefetchResult instanceof Promise) {
43
+ state = prefetchResult.then((deferredData)=>deferredData.data[deferId]);
44
+ } else {
45
+ state = prefetchResult.data[deferId];
46
+ }
47
+ } else {
48
+ state = prefetchResult;
49
+ }
50
+ const [prefetchState, setPrefetchState] = useState(state);
51
+ const prefetchInstance = MFDataPrefetch.getInstance(mfScope);
52
+ useEffect(()=>{
53
+ const useEffectTiming = performance.now();
54
+ logger.info(`3. Start Execute UseEffect: ${options.id} - ${options.functionId || 'default'} - ${useEffectTiming}`);
55
+ return ()=>{
56
+ prefetchInstance === null || prefetchInstance === void 0 ? void 0 : prefetchInstance.markOutdate(prefetchInfo, true);
57
+ };
58
+ }, []);
59
+ const refreshExecutor = (refetchParams)=>{
60
+ const refetchOptions = {
61
+ ...options
62
+ };
63
+ if (refetchParams) {
64
+ refetchOptions.refetchParams = refetchParams;
65
+ }
66
+ prefetchInstance === null || prefetchInstance === void 0 ? void 0 : prefetchInstance.markOutdate(prefetchInfo, true);
67
+ const newVal = prefetch(refetchOptions);
68
+ let newState;
69
+ if (deferId) {
70
+ if (newVal instanceof Promise) {
71
+ newState = newVal.then((deferredData)=>deferredData.data[deferId]);
72
+ } else {
73
+ newState = newVal.data[deferId];
74
+ }
75
+ } else {
76
+ newState = newVal;
77
+ }
78
+ setPrefetchState(newState);
79
+ };
80
+ return [
81
+ prefetchState,
82
+ refreshExecutor
83
+ ];
84
+ };
85
+
86
+ export { usePrefetch };