@vxrn/compiler 1.1.397

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 +45 -0
  2. package/dist/cjs/configure.cjs +34 -0
  3. package/dist/cjs/configure.js +29 -0
  4. package/dist/cjs/configure.js.map +6 -0
  5. package/dist/cjs/configure.native.js +35 -0
  6. package/dist/cjs/configure.native.js.map +6 -0
  7. package/dist/cjs/constants.cjs +65 -0
  8. package/dist/cjs/constants.js +34 -0
  9. package/dist/cjs/constants.js.map +6 -0
  10. package/dist/cjs/constants.native.js +65 -0
  11. package/dist/cjs/constants.native.js.map +6 -0
  12. package/dist/cjs/index.cjs +124 -0
  13. package/dist/cjs/index.js +113 -0
  14. package/dist/cjs/index.js.map +6 -0
  15. package/dist/cjs/index.native.js +123 -0
  16. package/dist/cjs/index.native.js.map +6 -0
  17. package/dist/cjs/refresh-runtime.cjs +295 -0
  18. package/dist/cjs/refresh-runtime.js +296 -0
  19. package/dist/cjs/refresh-runtime.js.map +6 -0
  20. package/dist/cjs/refresh-runtime.native.js +316 -0
  21. package/dist/cjs/refresh-runtime.native.js.map +6 -0
  22. package/dist/cjs/transformBabel.cjs +111 -0
  23. package/dist/cjs/transformBabel.js +116 -0
  24. package/dist/cjs/transformBabel.js.map +6 -0
  25. package/dist/cjs/transformBabel.native.js +152 -0
  26. package/dist/cjs/transformBabel.native.js.map +6 -0
  27. package/dist/cjs/transformSWC.cjs +261 -0
  28. package/dist/cjs/transformSWC.js +256 -0
  29. package/dist/cjs/transformSWC.js.map +6 -0
  30. package/dist/cjs/transformSWC.native.js +275 -0
  31. package/dist/cjs/transformSWC.native.js.map +6 -0
  32. package/dist/cjs/types.cjs +16 -0
  33. package/dist/cjs/types.js +14 -0
  34. package/dist/cjs/types.js.map +6 -0
  35. package/dist/cjs/types.native.js +15 -0
  36. package/dist/cjs/types.native.js.map +6 -0
  37. package/dist/esm/configure.js +13 -0
  38. package/dist/esm/configure.js.map +6 -0
  39. package/dist/esm/configure.mjs +10 -0
  40. package/dist/esm/configure.mjs.map +1 -0
  41. package/dist/esm/configure.native.js +13 -0
  42. package/dist/esm/configure.native.js.map +6 -0
  43. package/dist/esm/constants.js +18 -0
  44. package/dist/esm/constants.js.map +6 -0
  45. package/dist/esm/constants.mjs +38 -0
  46. package/dist/esm/constants.mjs.map +1 -0
  47. package/dist/esm/constants.native.js +41 -0
  48. package/dist/esm/constants.native.js.map +6 -0
  49. package/dist/esm/index.js +102 -0
  50. package/dist/esm/index.js.map +6 -0
  51. package/dist/esm/index.mjs +99 -0
  52. package/dist/esm/index.mjs.map +1 -0
  53. package/dist/esm/index.native.js +104 -0
  54. package/dist/esm/index.native.js.map +6 -0
  55. package/dist/esm/refresh-runtime.js +280 -0
  56. package/dist/esm/refresh-runtime.js.map +6 -0
  57. package/dist/esm/refresh-runtime.mjs +266 -0
  58. package/dist/esm/refresh-runtime.mjs.map +1 -0
  59. package/dist/esm/refresh-runtime.native.js +290 -0
  60. package/dist/esm/refresh-runtime.native.js.map +6 -0
  61. package/dist/esm/transformBabel.js +95 -0
  62. package/dist/esm/transformBabel.js.map +6 -0
  63. package/dist/esm/transformBabel.mjs +76 -0
  64. package/dist/esm/transformBabel.mjs.map +1 -0
  65. package/dist/esm/transformBabel.native.js +125 -0
  66. package/dist/esm/transformBabel.native.js.map +6 -0
  67. package/dist/esm/transformSWC.js +246 -0
  68. package/dist/esm/transformSWC.js.map +6 -0
  69. package/dist/esm/transformSWC.mjs +237 -0
  70. package/dist/esm/transformSWC.mjs.map +1 -0
  71. package/dist/esm/transformSWC.native.js +257 -0
  72. package/dist/esm/transformSWC.native.js.map +6 -0
  73. package/dist/esm/types.js +1 -0
  74. package/dist/esm/types.js.map +6 -0
  75. package/dist/esm/types.mjs +2 -0
  76. package/dist/esm/types.mjs.map +1 -0
  77. package/dist/esm/types.native.js +1 -0
  78. package/dist/esm/types.native.js.map +6 -0
  79. package/package.json +54 -0
  80. package/src/configure.ts +17 -0
  81. package/src/constants.ts +21 -0
  82. package/src/index.ts +166 -0
  83. package/src/refresh-runtime.js +619 -0
  84. package/src/transformBabel.ts +218 -0
  85. package/src/transformSWC.ts +381 -0
  86. package/src/types.ts +36 -0
  87. package/types/configure.d.ts +10 -0
  88. package/types/configure.d.ts.map +1 -0
  89. package/types/constants.d.ts +9 -0
  90. package/types/constants.d.ts.map +1 -0
  91. package/types/index.d.ts +12 -0
  92. package/types/index.d.ts.map +1 -0
  93. package/types/transformBabel.d.ts +12 -0
  94. package/types/transformBabel.d.ts.map +1 -0
  95. package/types/transformSWC.d.ts +7 -0
  96. package/types/transformSWC.d.ts.map +1 -0
  97. package/types/types.d.ts +29 -0
  98. package/types/types.d.ts.map +1 -0
@@ -0,0 +1,619 @@
1
+ /*! Copyright (c) Meta Platforms, Inc. and affiliates. **/
2
+ /**
3
+ * This is simplified pure-js version of https://github.com/facebook/react/blob/main/packages/react-refresh/src/ReactFreshRuntime.js
4
+ * without IE11 compatibility and verbose isDev checks.
5
+ * Some utils are appended at the bottom for HMR integration.
6
+ */
7
+
8
+ const REACT_FORWARD_REF_TYPE = Symbol.for('react.forward_ref')
9
+ const REACT_MEMO_TYPE = Symbol.for('react.memo')
10
+
11
+ // We never remove these associations.
12
+ // It's OK to reference families, but use WeakMap/Set for types.
13
+ let allFamiliesByID = new Map()
14
+ let allFamiliesByType = new WeakMap()
15
+ let allSignaturesByType = new WeakMap()
16
+
17
+ // This WeakMap is read by React, so we only put families
18
+ // that have actually been edited here. This keeps checks fast.
19
+ const updatedFamiliesByType = new WeakMap()
20
+
21
+ // This is cleared on every performReactRefresh() call.
22
+ // It is an array of [Family, NextType] tuples.
23
+ let pendingUpdates = []
24
+
25
+ // This is injected by the renderer via DevTools global hook.
26
+ const helpersByRendererID = new Map()
27
+
28
+ const helpersByRoot = new Map()
29
+
30
+ // We keep track of mounted roots so we can schedule updates.
31
+ const mountedRoots = new Set()
32
+ // If a root captures an error, we remember it so we can retry on edit.
33
+ const failedRoots = new Set()
34
+
35
+ // We also remember the last element for every root.
36
+ // It needs to be weak because we do this even for roots that failed to mount.
37
+ // If there is no WeakMap, we won't attempt to do retrying.
38
+ let rootElements = new WeakMap()
39
+ let isPerformingRefresh = false
40
+
41
+ function computeFullKey(signature) {
42
+ if (signature.fullKey !== null) {
43
+ return signature.fullKey
44
+ }
45
+
46
+ let fullKey = signature.ownKey
47
+ let hooks
48
+ try {
49
+ hooks = signature.getCustomHooks()
50
+ } catch (err) {
51
+ // This can happen in an edge case, e.g. if expression like Foo.useSomething
52
+ // depends on Foo which is lazily initialized during rendering.
53
+ // In that case just assume we'll have to remount.
54
+ signature.forceReset = true
55
+ signature.fullKey = fullKey
56
+ return fullKey
57
+ }
58
+
59
+ for (let i = 0; i < hooks.length; i++) {
60
+ const hook = hooks[i]
61
+ if (typeof hook !== 'function') {
62
+ // Something's wrong. Assume we need to remount.
63
+ signature.forceReset = true
64
+ signature.fullKey = fullKey
65
+ return fullKey
66
+ }
67
+ const nestedHookSignature = allSignaturesByType.get(hook)
68
+ if (nestedHookSignature === undefined) {
69
+ // No signature means Hook wasn't in the source code, e.g. in a library.
70
+ // We'll skip it because we can assume it won't change during this session.
71
+ continue
72
+ }
73
+ const nestedHookKey = computeFullKey(nestedHookSignature)
74
+ if (nestedHookSignature.forceReset) {
75
+ signature.forceReset = true
76
+ }
77
+ fullKey += '\n---\n' + nestedHookKey
78
+ }
79
+
80
+ signature.fullKey = fullKey
81
+ return fullKey
82
+ }
83
+
84
+ function haveEqualSignatures(prevType, nextType) {
85
+ const prevSignature = allSignaturesByType.get(prevType)
86
+ const nextSignature = allSignaturesByType.get(nextType)
87
+
88
+ if (prevSignature === undefined && nextSignature === undefined) {
89
+ return true
90
+ }
91
+ if (prevSignature === undefined || nextSignature === undefined) {
92
+ return false
93
+ }
94
+ if (computeFullKey(prevSignature) !== computeFullKey(nextSignature)) {
95
+ return false
96
+ }
97
+ if (nextSignature.forceReset) {
98
+ return false
99
+ }
100
+
101
+ return true
102
+ }
103
+
104
+ function isReactClass(type) {
105
+ return type.prototype && type.prototype.isReactComponent
106
+ }
107
+
108
+ function canPreserveStateBetween(prevType, nextType) {
109
+ if (isReactClass(prevType) || isReactClass(nextType)) {
110
+ return false
111
+ }
112
+ if (haveEqualSignatures(prevType, nextType)) {
113
+ return true
114
+ }
115
+ return false
116
+ }
117
+
118
+ function resolveFamily(type) {
119
+ // Only check updated types to keep lookups fast.
120
+ return updatedFamiliesByType.get(type)
121
+ }
122
+
123
+ // This is a safety mechanism to protect against rogue getters and Proxies.
124
+ function getProperty(object, property) {
125
+ try {
126
+ return object[property]
127
+ } catch (err) {
128
+ // Intentionally ignore.
129
+ return undefined
130
+ }
131
+ }
132
+
133
+ function performReactRefresh() {
134
+ if (pendingUpdates.length === 0) {
135
+ return null
136
+ }
137
+ if (isPerformingRefresh) {
138
+ return null
139
+ }
140
+
141
+ isPerformingRefresh = true
142
+ try {
143
+ const staleFamilies = new Set()
144
+ const updatedFamilies = new Set()
145
+
146
+ const updates = pendingUpdates
147
+ pendingUpdates = []
148
+ updates.forEach(([family, nextType]) => {
149
+ // Now that we got a real edit, we can create associations
150
+ // that will be read by the React reconciler.
151
+ const prevType = family.current
152
+ updatedFamiliesByType.set(prevType, family)
153
+ updatedFamiliesByType.set(nextType, family)
154
+ family.current = nextType
155
+
156
+ // Determine whether this should be a re-render or a re-mount.
157
+ if (canPreserveStateBetween(prevType, nextType)) {
158
+ updatedFamilies.add(family)
159
+ } else {
160
+ staleFamilies.add(family)
161
+ }
162
+ })
163
+
164
+ // TODO: rename these fields to something more meaningful.
165
+ const update = {
166
+ updatedFamilies, // Families that will re-render preserving state
167
+ staleFamilies, // Families that will be remounted
168
+ }
169
+
170
+ helpersByRendererID.forEach((helpers) => {
171
+ // Even if there are no roots, set the handler on first update.
172
+ // This ensures that if *new* roots are mounted, they'll use the resolve handler.
173
+ helpers.setRefreshHandler(resolveFamily)
174
+ })
175
+
176
+ let didError = false
177
+ let firstError = null
178
+
179
+ // We snapshot maps and sets that are mutated during commits.
180
+ // If we don't do this, there is a risk they will be mutated while
181
+ // we iterate over them. For example, trying to recover a failed root
182
+ // may cause another root to be added to the failed list -- an infinite loop.
183
+ const failedRootsSnapshot = new Set(failedRoots)
184
+ const mountedRootsSnapshot = new Set(mountedRoots)
185
+ const helpersByRootSnapshot = new Map(helpersByRoot)
186
+
187
+ failedRootsSnapshot.forEach((root) => {
188
+ const helpers = helpersByRootSnapshot.get(root)
189
+ if (helpers === undefined) {
190
+ throw new Error('Could not find helpers for a root. This is a bug in React Refresh.')
191
+ }
192
+ if (!failedRoots.has(root)) {
193
+ // No longer failed.
194
+ }
195
+ if (rootElements === null) {
196
+ return
197
+ }
198
+ if (!rootElements.has(root)) {
199
+ return
200
+ }
201
+ const element = rootElements.get(root)
202
+ try {
203
+ helpers.scheduleRoot(root, element)
204
+ } catch (err) {
205
+ if (!didError) {
206
+ didError = true
207
+ firstError = err
208
+ }
209
+ // Keep trying other roots.
210
+ }
211
+ })
212
+ mountedRootsSnapshot.forEach((root) => {
213
+ const helpers = helpersByRootSnapshot.get(root)
214
+ if (helpers === undefined) {
215
+ throw new Error('Could not find helpers for a root. This is a bug in React Refresh.')
216
+ }
217
+ if (!mountedRoots.has(root)) {
218
+ // No longer mounted.
219
+ }
220
+ try {
221
+ helpers.scheduleRefresh(root, update)
222
+ } catch (err) {
223
+ if (!didError) {
224
+ didError = true
225
+ firstError = err
226
+ }
227
+ // Keep trying other roots.
228
+ }
229
+ })
230
+ if (didError) {
231
+ throw firstError
232
+ }
233
+ return update
234
+ } finally {
235
+ isPerformingRefresh = false
236
+ }
237
+ }
238
+
239
+ function register(type, id) {
240
+ if (type === null) {
241
+ return
242
+ }
243
+ if (typeof type !== 'function' && typeof type !== 'object') {
244
+ return
245
+ }
246
+
247
+ // This can happen in an edge case, e.g. if we register
248
+ // return value of a HOC but it returns a cached component.
249
+ // Ignore anything but the first registration for each type.
250
+ if (allFamiliesByType.has(type)) {
251
+ return
252
+ }
253
+ // Create family or remember to update it.
254
+ // None of this bookkeeping affects reconciliation
255
+ // until the first performReactRefresh() call above.
256
+ let family = allFamiliesByID.get(id)
257
+ if (family === undefined) {
258
+ family = { current: type }
259
+ allFamiliesByID.set(id, family)
260
+ } else {
261
+ pendingUpdates.push([family, type])
262
+ }
263
+ allFamiliesByType.set(type, family)
264
+
265
+ // Visit inner types because we might not have registered them.
266
+ if (typeof type === 'object' && type !== null) {
267
+ switch (getProperty(type, '$$typeof')) {
268
+ case REACT_FORWARD_REF_TYPE:
269
+ register(type.render, id + '$render')
270
+ break
271
+ case REACT_MEMO_TYPE:
272
+ register(type.type, id + '$type')
273
+ break
274
+ }
275
+ }
276
+ }
277
+
278
+ function setSignature(type, key, forceReset, getCustomHooks) {
279
+ if (!allSignaturesByType.has(type)) {
280
+ allSignaturesByType.set(type, {
281
+ forceReset,
282
+ ownKey: key,
283
+ fullKey: null,
284
+ getCustomHooks: getCustomHooks || (() => []),
285
+ })
286
+ }
287
+ // Visit inner types because we might not have signed them.
288
+ if (typeof type === 'object' && type !== null) {
289
+ switch (getProperty(type, '$$typeof')) {
290
+ case REACT_FORWARD_REF_TYPE:
291
+ setSignature(type.render, key, forceReset, getCustomHooks)
292
+ break
293
+ case REACT_MEMO_TYPE:
294
+ setSignature(type.type, key, forceReset, getCustomHooks)
295
+ break
296
+ }
297
+ }
298
+ }
299
+
300
+ // This is lazily called during first render for a type.
301
+ // It captures Hook list at that time so inline requires don't break comparisons.
302
+ function collectCustomHooksForSignature(type) {
303
+ const signature = allSignaturesByType.get(type)
304
+ if (signature !== undefined) {
305
+ computeFullKey(signature)
306
+ }
307
+ }
308
+
309
+ export function injectIntoGlobalHook(globalObject) {
310
+ // For React Native, the global hook will be set up by require('react-devtools-core').
311
+ // That code will run before us. So we need to monkeypatch functions on existing hook.
312
+
313
+ // For React Web, the global hook will be set up by the extension.
314
+ // This will also run before us.
315
+ let hook = globalObject.__REACT_DEVTOOLS_GLOBAL_HOOK__
316
+ if (hook === undefined) {
317
+ // However, if there is no DevTools extension, we'll need to set up the global hook ourselves.
318
+ // Note that in this case it's important that renderer code runs *after* this method call.
319
+ // Otherwise, the renderer will think that there is no global hook, and won't do the injection.
320
+ let nextID = 0
321
+ globalObject.__REACT_DEVTOOLS_GLOBAL_HOOK__ = hook = {
322
+ renderers: new Map(),
323
+ supportsFiber: true,
324
+ inject: (injected) => nextID++,
325
+ onScheduleFiberRoot: (id, root, children) => {},
326
+ onCommitFiberRoot: (id, root, maybePriorityLevel, didError) => {},
327
+ onCommitFiberUnmount() {},
328
+ }
329
+ }
330
+
331
+ if (hook.isDisabled) {
332
+ // This isn't a real property on the hook, but it can be set to opt out
333
+ // of DevTools integration and associated warnings and logs.
334
+ // Using console['warn'] to evade Babel and ESLint
335
+ console['warn'](
336
+ 'Something has shimmed the React DevTools global hook (__REACT_DEVTOOLS_GLOBAL_HOOK__). ' +
337
+ 'Fast Refresh is not compatible with this shim and will be disabled.'
338
+ )
339
+ return
340
+ }
341
+
342
+ // Here, we just want to get a reference to scheduleRefresh.
343
+ const oldInject = hook.inject
344
+ hook.inject = function (injected) {
345
+ const id = oldInject.apply(this, arguments)
346
+ if (
347
+ typeof injected.scheduleRefresh === 'function' &&
348
+ typeof injected.setRefreshHandler === 'function'
349
+ ) {
350
+ // This version supports React Refresh.
351
+ helpersByRendererID.set(id, injected)
352
+ }
353
+ return id
354
+ }
355
+
356
+ // Do the same for any already injected roots.
357
+ // This is useful if ReactDOM has already been initialized.
358
+ // https://github.com/facebook/react/issues/17626
359
+ hook.renderers.forEach((injected, id) => {
360
+ if (
361
+ typeof injected.scheduleRefresh === 'function' &&
362
+ typeof injected.setRefreshHandler === 'function'
363
+ ) {
364
+ // This version supports React Refresh.
365
+ helpersByRendererID.set(id, injected)
366
+ }
367
+ })
368
+
369
+ // We also want to track currently mounted roots.
370
+ const oldOnCommitFiberRoot = hook.onCommitFiberRoot
371
+ const oldOnScheduleFiberRoot = hook.onScheduleFiberRoot || (() => {})
372
+ hook.onScheduleFiberRoot = function (id, root, children) {
373
+ if (!isPerformingRefresh) {
374
+ // If it was intentionally scheduled, don't attempt to restore.
375
+ // This includes intentionally scheduled unmounts.
376
+ failedRoots.delete(root)
377
+ if (rootElements !== null) {
378
+ rootElements.set(root, children)
379
+ }
380
+ }
381
+ return oldOnScheduleFiberRoot.apply(this, arguments)
382
+ }
383
+ hook.onCommitFiberRoot = function (id, root, maybePriorityLevel, didError) {
384
+ const helpers = helpersByRendererID.get(id)
385
+ if (helpers !== undefined) {
386
+ helpersByRoot.set(root, helpers)
387
+
388
+ const current = root.current
389
+ const alternate = current.alternate
390
+
391
+ // We need to determine whether this root has just (un)mounted.
392
+ // This logic is copy-pasted from similar logic in the DevTools backend.
393
+ // If this breaks with some refactoring, you'll want to update DevTools too.
394
+
395
+ if (alternate !== null) {
396
+ const wasMounted =
397
+ alternate.memoizedState != null &&
398
+ alternate.memoizedState.element != null &&
399
+ mountedRoots.has(root)
400
+
401
+ const isMounted = current.memoizedState != null && current.memoizedState.element != null
402
+
403
+ if (!wasMounted && isMounted) {
404
+ // Mount a new root.
405
+ mountedRoots.add(root)
406
+ failedRoots.delete(root)
407
+ } else if (wasMounted && isMounted) {
408
+ // Update an existing root.
409
+ // This doesn't affect our mounted root Set.
410
+ } else if (wasMounted && !isMounted) {
411
+ // Unmount an existing root.
412
+ mountedRoots.delete(root)
413
+ if (didError) {
414
+ // We'll remount it on future edits.
415
+ failedRoots.add(root)
416
+ } else {
417
+ helpersByRoot.delete(root)
418
+ }
419
+ } else if (!wasMounted && !isMounted) {
420
+ if (didError) {
421
+ // We'll remount it on future edits.
422
+ failedRoots.add(root)
423
+ }
424
+ }
425
+ } else {
426
+ // Mount a new root.
427
+ mountedRoots.add(root)
428
+ }
429
+ }
430
+
431
+ // Always call the decorated DevTools hook.
432
+ return oldOnCommitFiberRoot.apply(this, arguments)
433
+ }
434
+ }
435
+
436
+ // This is a wrapper over more primitive functions for setting signature.
437
+ // Signatures let us decide whether the Hook order has changed on refresh.
438
+ //
439
+ // This function is intended to be used as a transform target, e.g.:
440
+ // var _s = createSignatureFunctionForTransform()
441
+ //
442
+ // function Hello() {
443
+ // const [foo, setFoo] = useState(0);
444
+ // const value = useCustomHook();
445
+ // _s(); /* Call without arguments triggers collecting the custom Hook list.
446
+ // * This doesn't happen during the module evaluation because we
447
+ // * don't want to change the module order with inline requires.
448
+ // * Next calls are noops. */
449
+ // return <h1>Hi</h1>;
450
+ // }
451
+ //
452
+ // /* Call with arguments attaches the signature to the type: */
453
+ // _s(
454
+ // Hello,
455
+ // 'useState{[foo, setFoo]}(0)',
456
+ // () => [useCustomHook], /* Lazy to avoid triggering inline requires */
457
+ // );
458
+ export function createSignatureFunctionForTransform() {
459
+ let savedType
460
+ let hasCustomHooks
461
+ let didCollectHooks = false
462
+ return (type, key, forceReset, getCustomHooks) => {
463
+ if (typeof key === 'string') {
464
+ // We're in the initial phase that associates signatures
465
+ // with the functions. Note this may be called multiple times
466
+ // in HOC chains like _s(hoc1(_s(hoc2(_s(actualFunction))))).
467
+ if (!savedType) {
468
+ // We're in the innermost call, so this is the actual type.
469
+ // $FlowFixMe[escaped-generic] discovered when updating Flow
470
+ savedType = type
471
+ hasCustomHooks = typeof getCustomHooks === 'function'
472
+ }
473
+ // Set the signature for all types (even wrappers!) in case
474
+ // they have no signatures of their own. This is to prevent
475
+ // problems like https://github.com/facebook/react/issues/20417.
476
+ if (type != null && (typeof type === 'function' || typeof type === 'object')) {
477
+ setSignature(type, key, forceReset, getCustomHooks)
478
+ }
479
+ return type
480
+ }
481
+ // We're in the _s() call without arguments, which means
482
+ // this is the time to collect custom Hook signatures.
483
+ // Only do this once. This path is hot and runs *inside* every render!
484
+ if (!didCollectHooks && hasCustomHooks) {
485
+ didCollectHooks = true
486
+ collectCustomHooksForSignature(savedType)
487
+ }
488
+ }
489
+ }
490
+
491
+ function isLikelyComponentType(type) {
492
+ switch (typeof type) {
493
+ case 'function': {
494
+ // First, deal with classes.
495
+ if (type.prototype != null) {
496
+ if (type.prototype.isReactComponent) {
497
+ // React class.
498
+ return true
499
+ }
500
+ const ownNames = Object.getOwnPropertyNames(type.prototype)
501
+ if (ownNames.length > 1 || ownNames[0] !== 'constructor') {
502
+ // This looks like a class.
503
+ return false
504
+ }
505
+ // eslint-disable-next-line no-proto
506
+ if (type.prototype.__proto__ !== Object.prototype) {
507
+ // It has a superclass.
508
+ return false
509
+ }
510
+ // Pass through.
511
+ // This looks like a regular function with empty prototype.
512
+ }
513
+ // For plain functions and arrows, use name as a heuristic.
514
+ const name = type.name || type.displayName
515
+ return typeof name === 'string' && /^[A-Z]/.test(name)
516
+ }
517
+ case 'object': {
518
+ if (type != null) {
519
+ switch (getProperty(type, '$$typeof')) {
520
+ case REACT_FORWARD_REF_TYPE:
521
+ case REACT_MEMO_TYPE:
522
+ // Definitely React components.
523
+ return true
524
+ default:
525
+ return false
526
+ }
527
+ }
528
+ return false
529
+ }
530
+ default: {
531
+ return false
532
+ }
533
+ }
534
+ }
535
+
536
+ /**
537
+ * Plugin utils
538
+ */
539
+
540
+ export function getRefreshReg(filename) {
541
+ return (type, id) => register(type, filename + ' ' + id)
542
+ }
543
+
544
+ // Taken from https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/lib/runtime/RefreshUtils.js#L141
545
+ // This allows to resister components not detected by SWC like styled component
546
+ export function registerExportsForReactRefresh(filename, moduleExports) {
547
+ for (const key in moduleExports) {
548
+ if (key === '__esModule') continue
549
+ const exportValue = moduleExports[key]
550
+ if (isLikelyComponentType(exportValue)) {
551
+ // 'export' is required to avoid key collision when renamed exports that
552
+ // shadow a local component name: https://github.com/vitejs/vite-plugin-react/issues/116
553
+ // The register function has an identity check to not register twice the same component,
554
+ // so this is safe to not used the same key here.
555
+ register(exportValue, filename + ' export ' + key)
556
+ }
557
+ }
558
+ }
559
+
560
+ function debounce(fn, delay) {
561
+ let handle
562
+ return () => {
563
+ clearTimeout(handle)
564
+ handle = setTimeout(fn, delay)
565
+ }
566
+ }
567
+
568
+ const hooks = []
569
+ window.__registerBeforePerformReactRefresh = (cb) => {
570
+ hooks.push(cb)
571
+ }
572
+ const enqueueUpdate = debounce(async () => {
573
+ if (hooks.length) await Promise.all(hooks.map((cb) => cb()))
574
+ performReactRefresh()
575
+ }, 16)
576
+
577
+ export function validateRefreshBoundaryAndEnqueueUpdate(id, prevExports, nextExports) {
578
+ const ignoredExports = window.__getReactRefreshIgnoredExports?.({ id }) ?? []
579
+ if (predicateOnExport(ignoredExports, prevExports, (key) => key in nextExports) !== true) {
580
+ return 'Could not Fast Refresh (export removed)'
581
+ }
582
+ if (predicateOnExport(ignoredExports, nextExports, (key) => key in prevExports) !== true) {
583
+ return 'Could not Fast Refresh (new export)'
584
+ }
585
+
586
+ let hasExports = false
587
+ const allExportsAreComponentsOrUnchanged = predicateOnExport(
588
+ ignoredExports,
589
+ nextExports,
590
+ (key, value) => {
591
+ hasExports = true
592
+ if (isLikelyComponentType(value)) return true
593
+ return prevExports[key] === nextExports[key]
594
+ }
595
+ )
596
+ if (hasExports && allExportsAreComponentsOrUnchanged === true) {
597
+ enqueueUpdate()
598
+ } else {
599
+ return `Could not Fast Refresh ("${allExportsAreComponentsOrUnchanged}" export is incompatible). Learn more at https://github.com/vitejs/vite-plugin-react-swc#consistent-components-exports`
600
+ }
601
+ }
602
+
603
+ function predicateOnExport(ignoredExports, moduleExports, predicate) {
604
+ for (const key in moduleExports) {
605
+ if (key === '__esModule') continue
606
+ if (ignoredExports.includes(key)) continue
607
+ const desc = Object.getOwnPropertyDescriptor(moduleExports, key)
608
+ if (desc && desc.get) return key
609
+ if (!predicate(key, moduleExports[key])) return key
610
+ }
611
+ return true
612
+ }
613
+
614
+ // Hides vite-ignored dynamic import so that Vite can skip analysis if no other
615
+ // dynamic import is present (https://github.com/vitejs/vite/pull/12732)
616
+ export const __hmr_import = (module) => import(/* @vite-ignore */ module)
617
+
618
+ // For backwards compatibility with @vitejs/plugin-react.
619
+ export default { injectIntoGlobalHook }