|
| 1 | +import fs from 'node:fs' |
| 2 | +import path from 'node:path' |
| 3 | +import { createRequire } from 'node:module' |
| 4 | + |
| 5 | +export const runtimePublicPath = '/@react-refresh' |
| 6 | + |
| 7 | +const _require = createRequire(import.meta.url) |
| 8 | +const reactRefreshDir = path.dirname( |
| 9 | + _require.resolve('react-refresh/package.json'), |
| 10 | +) |
| 11 | +const runtimeFilePath = path.join( |
| 12 | + reactRefreshDir, |
| 13 | + 'cjs/react-refresh-runtime.development.js', |
| 14 | +) |
| 15 | + |
| 16 | +export const runtimeCode = ` |
| 17 | +const exports = {} |
| 18 | +${fs.readFileSync(runtimeFilePath, 'utf-8')} |
| 19 | +${fs.readFileSync(_require.resolve('./refreshUtils.js'), 'utf-8')} |
| 20 | +export default exports |
| 21 | +` |
| 22 | + |
| 23 | +export const preambleCode = ` |
| 24 | +import RefreshRuntime from "__BASE__${runtimePublicPath.slice(1)}" |
| 25 | +RefreshRuntime.injectIntoGlobalHook(window) |
| 26 | +window.$RefreshReg$ = () => {} |
| 27 | +window.$RefreshSig$ = () => (type) => type |
| 28 | +window.__vite_plugin_react_preamble_installed__ = true |
| 29 | +` |
| 30 | + |
| 31 | +const sharedHeader = ` |
| 32 | +import RefreshRuntime from "${runtimePublicPath}"; |
| 33 | +
|
| 34 | +const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope; |
| 35 | +`.replace(/\n+/g, '') |
| 36 | +const functionHeader = ` |
| 37 | +let prevRefreshReg; |
| 38 | +let prevRefreshSig; |
| 39 | +
|
| 40 | +if (import.meta.hot && !inWebWorker) { |
| 41 | + if (!window.__vite_plugin_react_preamble_installed__) { |
| 42 | + throw new Error( |
| 43 | + "@vitejs/plugin-react-oxc can't detect preamble. Something is wrong. " + |
| 44 | + "See https://github.com/vitejs/vite-plugin-react/pull/11#discussion_r430879201" |
| 45 | + ); |
| 46 | + } |
| 47 | +
|
| 48 | + prevRefreshReg = window.$RefreshReg$; |
| 49 | + prevRefreshSig = window.$RefreshSig$; |
| 50 | + window.$RefreshReg$ = (type, id) => { |
| 51 | + RefreshRuntime.register(type, __SOURCE__ + " " + id) |
| 52 | + }; |
| 53 | + window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform; |
| 54 | +}`.replace(/\n+/g, '') |
| 55 | + |
| 56 | +const functionFooter = ` |
| 57 | +if (import.meta.hot && !inWebWorker) { |
| 58 | + window.$RefreshReg$ = prevRefreshReg; |
| 59 | + window.$RefreshSig$ = prevRefreshSig; |
| 60 | +}` |
| 61 | +const sharedFooter = (id: string) => ` |
| 62 | +if (import.meta.hot && !inWebWorker) { |
| 63 | + RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => { |
| 64 | + RefreshRuntime.registerExportsForReactRefresh(${JSON.stringify( |
| 65 | + id, |
| 66 | + )}, currentExports); |
| 67 | + import.meta.hot.accept((nextExports) => { |
| 68 | + if (!nextExports) return; |
| 69 | + const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(${JSON.stringify( |
| 70 | + id, |
| 71 | + )}, currentExports, nextExports); |
| 72 | + if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage); |
| 73 | + }); |
| 74 | + }); |
| 75 | +}` |
| 76 | + |
| 77 | +export function addRefreshWrapper(code: string, id: string): string { |
| 78 | + return ( |
| 79 | + sharedHeader + |
| 80 | + functionHeader.replace('__SOURCE__', JSON.stringify(id)) + |
| 81 | + code + |
| 82 | + functionFooter + |
| 83 | + sharedFooter(id) |
| 84 | + ) |
| 85 | +} |
| 86 | + |
| 87 | +export function addClassComponentRefreshWrapper( |
| 88 | + code: string, |
| 89 | + id: string, |
| 90 | +): string { |
| 91 | + return sharedHeader + code + sharedFooter(id) |
| 92 | +} |
0 commit comments