[d565449] | 1 | function debounce(fn, delay) {
|
---|
| 2 | let handle
|
---|
| 3 | return () => {
|
---|
| 4 | clearTimeout(handle)
|
---|
| 5 | handle = setTimeout(fn, delay)
|
---|
| 6 | }
|
---|
| 7 | }
|
---|
| 8 |
|
---|
| 9 | /* eslint-disable no-undef */
|
---|
[0c6b92a] | 10 | const hooks = []
|
---|
| 11 | window.__registerBeforePerformReactRefresh = (cb) => {
|
---|
| 12 | hooks.push(cb)
|
---|
| 13 | }
|
---|
| 14 | const enqueueUpdate = debounce(async () => {
|
---|
| 15 | if (hooks.length) await Promise.all(hooks.map((cb) => cb()))
|
---|
| 16 | exports.performReactRefresh()
|
---|
| 17 | }, 16)
|
---|
[d565449] | 18 |
|
---|
| 19 | // Taken from https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/lib/runtime/RefreshUtils.js#L141
|
---|
| 20 | // This allows to resister components not detected by SWC like styled component
|
---|
| 21 | function registerExportsForReactRefresh(filename, moduleExports) {
|
---|
| 22 | for (const key in moduleExports) {
|
---|
| 23 | if (key === '__esModule') continue
|
---|
| 24 | const exportValue = moduleExports[key]
|
---|
| 25 | if (exports.isLikelyComponentType(exportValue)) {
|
---|
| 26 | // 'export' is required to avoid key collision when renamed exports that
|
---|
| 27 | // shadow a local component name: https://github.com/vitejs/vite-plugin-react/issues/116
|
---|
| 28 | // The register function has an identity check to not register twice the same component,
|
---|
| 29 | // so this is safe to not used the same key here.
|
---|
| 30 | exports.register(exportValue, filename + ' export ' + key)
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 | }
|
---|
| 34 |
|
---|
[0c6b92a] | 35 | function validateRefreshBoundaryAndEnqueueUpdate(id, prevExports, nextExports) {
|
---|
| 36 | const ignoredExports = window.__getReactRefreshIgnoredExports?.({ id }) ?? []
|
---|
| 37 | if (
|
---|
| 38 | predicateOnExport(
|
---|
| 39 | ignoredExports,
|
---|
| 40 | prevExports,
|
---|
| 41 | (key) => key in nextExports,
|
---|
| 42 | ) !== true
|
---|
| 43 | ) {
|
---|
[d565449] | 44 | return 'Could not Fast Refresh (export removed)'
|
---|
| 45 | }
|
---|
[0c6b92a] | 46 | if (
|
---|
| 47 | predicateOnExport(
|
---|
| 48 | ignoredExports,
|
---|
| 49 | nextExports,
|
---|
| 50 | (key) => key in prevExports,
|
---|
| 51 | ) !== true
|
---|
| 52 | ) {
|
---|
[d565449] | 53 | return 'Could not Fast Refresh (new export)'
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | let hasExports = false
|
---|
| 57 | const allExportsAreComponentsOrUnchanged = predicateOnExport(
|
---|
[0c6b92a] | 58 | ignoredExports,
|
---|
[d565449] | 59 | nextExports,
|
---|
| 60 | (key, value) => {
|
---|
| 61 | hasExports = true
|
---|
| 62 | if (exports.isLikelyComponentType(value)) return true
|
---|
| 63 | return prevExports[key] === nextExports[key]
|
---|
| 64 | },
|
---|
| 65 | )
|
---|
[0c6b92a] | 66 | if (hasExports && allExportsAreComponentsOrUnchanged === true) {
|
---|
[d565449] | 67 | enqueueUpdate()
|
---|
| 68 | } else {
|
---|
[0c6b92a] | 69 | return `Could not Fast Refresh ("${allExportsAreComponentsOrUnchanged}" export is incompatible). Learn more at https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react#consistent-components-exports`
|
---|
[d565449] | 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[0c6b92a] | 73 | function predicateOnExport(ignoredExports, moduleExports, predicate) {
|
---|
[d565449] | 74 | for (const key in moduleExports) {
|
---|
| 75 | if (key === '__esModule') continue
|
---|
[0c6b92a] | 76 | if (ignoredExports.includes(key)) continue
|
---|
[d565449] | 77 | const desc = Object.getOwnPropertyDescriptor(moduleExports, key)
|
---|
[0c6b92a] | 78 | if (desc && desc.get) return key
|
---|
| 79 | if (!predicate(key, moduleExports[key])) return key
|
---|
[d565449] | 80 | }
|
---|
| 81 | return true
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | // Hides vite-ignored dynamic import so that Vite can skip analysis if no other
|
---|
| 85 | // dynamic import is present (https://github.com/vitejs/vite/pull/12732)
|
---|
| 86 | function __hmr_import(module) {
|
---|
| 87 | return import(/* @vite-ignore */ module)
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | exports.__hmr_import = __hmr_import
|
---|
| 91 | exports.registerExportsForReactRefresh = registerExportsForReactRefresh
|
---|
| 92 | exports.validateRefreshBoundaryAndEnqueueUpdate =
|
---|
| 93 | validateRefreshBoundaryAndEnqueueUpdate
|
---|