Skip to content

Commit 594f42b

Browse files
committed
fix: handle reactRefreshHost
1 parent e2861c9 commit 594f42b

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

packages/common/refresh-utils.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,15 @@ function $RefreshSig$() { return RefreshRuntime.createSignatureFunctionForTransf
6464
return newCode
6565
}
6666

67-
export function virtualPreamblePlugin({
68-
isEnabled,
69-
}: {
67+
export function virtualPreamblePlugin(opts: {
7068
isEnabled: () => boolean
69+
reactRefreshHost?: string
7170
}): Plugin {
7271
const VIRTUAL_NAME = 'virtual:@vitejs/plugin-react/preamble'
72+
let importSource = VIRTUAL_NAME
73+
if (opts.reactRefreshHost) {
74+
importSource = opts.reactRefreshHost + '/@id/__x00__' + VIRTUAL_NAME
75+
}
7376
return {
7477
name: 'vite:react-virtual-preamble',
7578
apply: 'serve',
@@ -86,7 +89,7 @@ export function virtualPreamblePlugin({
8689
filter: { id: exactRegex('\0' + VIRTUAL_NAME) },
8790
handler(id) {
8891
if (id === '\0' + VIRTUAL_NAME) {
89-
if (isEnabled()) {
92+
if (opts.isEnabled()) {
9093
// vite dev import analysis can rewrite base
9194
return preambleCode.replace('__BASE__', '/')
9295
}
@@ -102,8 +105,11 @@ export function virtualPreamblePlugin({
102105

103106
// this is expected to match `react`, `react-dom`, and `react-dom/client`.
104107
// they are all optimized to be esm during dev.
105-
if (isEnabled() && code.includes('__REACT_DEVTOOLS_GLOBAL_HOOK__')) {
106-
return `import ${JSON.stringify(VIRTUAL_NAME)};` + code
108+
if (
109+
opts.isEnabled() &&
110+
code.includes('__REACT_DEVTOOLS_GLOBAL_HOOK__')
111+
) {
112+
return `import ${JSON.stringify(importSource)};` + code
107113
}
108114
},
109115
},

packages/plugin-react/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ export default function viteReact(opts: Options = {}): Plugin[] {
527527
viteReactRefresh,
528528
virtualPreamblePlugin({
529529
isEnabled: () => !skipFastRefresh && !isFullBundle,
530+
reactRefreshHost: opts.reactRefreshHost,
530531
}),
531532
]
532533
}

playground/react/vite.config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import type { UserConfig } from 'vite'
44
const config: UserConfig = {
55
server: { port: 8902 /* Should be unique */ },
66
mode: 'development',
7-
plugins: [react()],
7+
plugins: [
8+
react({
9+
reactRefreshHost: 'http://localhost:8902',
10+
}),
11+
],
812
build: {
913
// to make tests faster
1014
minify: false,

0 commit comments

Comments
 (0)