Skip to content

Commit c6c29b4

Browse files
committed
feat: auto inject hmr preamble without transformIndexHtml
1 parent 5568bd5 commit c6c29b4

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

packages/common/refresh-utils.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export const preambleCode = `import { injectIntoGlobalHook } from "__BASE__${run
1212
)}";
1313
injectIntoGlobalHook(window);
1414
window.$RefreshReg$ = () => {};
15-
window.$RefreshSig$ = () => (type) => type;`
15+
window.$RefreshSig$ = () => (type) => type;
16+
`
1617

1718
export const getPreambleCode = (base: string): string =>
1819
preambleCode.replace('__BASE__', base)
@@ -94,5 +95,13 @@ export function virtualPreamblePlugin({
9495
}
9596
},
9697
},
98+
transform: {
99+
filter: { code: /__REACT_DEVTOOLS_GLOBAL_HOOK__/ },
100+
handler(code) {
101+
if (isEnabled() && code.includes('__REACT_DEVTOOLS_GLOBAL_HOOK__')) {
102+
return `import ${JSON.stringify(name)};` + code
103+
}
104+
},
105+
},
97106
}
98107
}

packages/plugin-react/src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -510,11 +510,11 @@ export default function viteReact(opts: Options = {}): Plugin[] {
510510
transformIndexHtml() {
511511
if (!skipFastRefresh && !isFullBundle)
512512
return [
513-
{
514-
tag: 'script',
515-
attrs: { type: 'module' },
516-
children: getPreambleCode(base),
517-
},
513+
// {
514+
// tag: 'script',
515+
// attrs: { type: 'module' },
516+
// children: getPreambleCode(base),
517+
// },
518518
]
519519
},
520520
}

playground/ssr-react/src/entry-client.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import '@vitejs/plugin-react/preamble'
21
import ReactDOM from 'react-dom/client'
32
import { App } from './App'
43

0 commit comments

Comments
 (0)