[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 */
|
---|
| 10 | const enqueueUpdate = debounce(exports.performReactRefresh, 16)
|
---|
| 11 |
|
---|
| 12 | // Taken from https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/lib/runtime/RefreshUtils.js#L141
|
---|
| 13 | // This allows to resister components not detected by SWC like styled component
|
---|
| 14 | function registerExportsForReactRefresh(filename, moduleExports) {
|
---|
| 15 | for (const key in moduleExports) {
|
---|
| 16 | if (key === '__esModule') continue
|
---|
| 17 | const exportValue = moduleExports[key]
|
---|
| 18 | if (exports.isLikelyComponentType(exportValue)) {
|
---|
| 19 | // 'export' is required to avoid key collision when renamed exports that
|
---|
| 20 | // shadow a local component name: https://github.com/vitejs/vite-plugin-react/issues/116
|
---|
| 21 | // The register function has an identity check to not register twice the same component,
|
---|
| 22 | // so this is safe to not used the same key here.
|
---|
| 23 | exports.register(exportValue, filename + ' export ' + key)
|
---|
| 24 | }
|
---|
| 25 | }
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | function validateRefreshBoundaryAndEnqueueUpdate(prevExports, nextExports) {
|
---|
| 29 | if (!predicateOnExport(prevExports, (key) => key in nextExports)) {
|
---|
| 30 | return 'Could not Fast Refresh (export removed)'
|
---|
| 31 | }
|
---|
| 32 | if (!predicateOnExport(nextExports, (key) => key in prevExports)) {
|
---|
| 33 | return 'Could not Fast Refresh (new export)'
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | let hasExports = false
|
---|
| 37 | const allExportsAreComponentsOrUnchanged = predicateOnExport(
|
---|
| 38 | nextExports,
|
---|
| 39 | (key, value) => {
|
---|
| 40 | hasExports = true
|
---|
| 41 | if (exports.isLikelyComponentType(value)) return true
|
---|
| 42 | return prevExports[key] === nextExports[key]
|
---|
| 43 | },
|
---|
| 44 | )
|
---|
| 45 | if (hasExports && allExportsAreComponentsOrUnchanged) {
|
---|
| 46 | enqueueUpdate()
|
---|
| 47 | } else {
|
---|
| 48 | return 'Could not Fast Refresh. Learn more at https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react#consistent-components-exports'
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | function predicateOnExport(moduleExports, predicate) {
|
---|
| 53 | for (const key in moduleExports) {
|
---|
| 54 | if (key === '__esModule') continue
|
---|
| 55 | const desc = Object.getOwnPropertyDescriptor(moduleExports, key)
|
---|
| 56 | if (desc && desc.get) return false
|
---|
| 57 | if (!predicate(key, moduleExports[key])) return false
|
---|
| 58 | }
|
---|
| 59 | return true
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | // Hides vite-ignored dynamic import so that Vite can skip analysis if no other
|
---|
| 63 | // dynamic import is present (https://github.com/vitejs/vite/pull/12732)
|
---|
| 64 | function __hmr_import(module) {
|
---|
| 65 | return import(/* @vite-ignore */ module)
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | exports.__hmr_import = __hmr_import
|
---|
| 69 | exports.registerExportsForReactRefresh = registerExportsForReactRefresh
|
---|
| 70 | exports.validateRefreshBoundaryAndEnqueueUpdate =
|
---|
| 71 | validateRefreshBoundaryAndEnqueueUpdate
|
---|