Skip to content

Commit 12eddea

Browse files
authored
fix(hmr): better handle compound components (#1484)
1 parent 3cfb1d1 commit 12eddea

5 files changed

Lines changed: 74 additions & 44 deletions

File tree

‎packages/common/refresh-runtime.js‎

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -632,50 +632,49 @@ export function validateRefreshBoundaryAndEnqueueUpdate(
632632
prevExports,
633633
nextExports,
634634
) {
635+
let hasExports = false
635636
const ignoredExports = window.__getReactRefreshIgnoredExports?.({ id }) ?? []
636-
if (
637-
predicateOnExport(
638-
ignoredExports,
639-
prevExports,
640-
(key) => key in nextExports,
641-
) !== true
642-
) {
643-
return 'Could not Fast Refresh (export removed)'
644-
}
645-
if (
646-
predicateOnExport(
647-
ignoredExports,
648-
nextExports,
649-
(key) => key in prevExports,
650-
) !== true
651-
) {
652-
return 'Could not Fast Refresh (new export)'
653-
}
654637

655-
let hasExports = false
656-
const allExportsAreComponentsOrUnchanged = predicateOnExport(
657-
ignoredExports,
658-
nextExports,
659-
(key, value) => {
660-
hasExports = true
661-
if (isLikelyComponentType(value)) return true
662-
if (isCompoundComponent(value)) return true
663-
return prevExports[key] === nextExports[key]
664-
},
665-
)
666-
if (hasExports && allExportsAreComponentsOrUnchanged === true) {
667-
enqueueUpdate()
668-
} else {
669-
return `Could not Fast Refresh ("${allExportsAreComponentsOrUnchanged}" export is incompatible). Learn more at __README_URL__#consistent-components-exports`
638+
for (const key in prevExports) {
639+
if (ignoredExports.includes(key)) continue
640+
if (!(key in nextExports)) {
641+
return `Could not Fast Refresh (export "${key}" removed)`
642+
}
670643
}
671-
}
672644

673-
function predicateOnExport(ignoredExports, moduleExports, predicate) {
674-
for (const key in moduleExports) {
645+
for (const key in nextExports) {
675646
if (ignoredExports.includes(key)) continue
676-
if (!predicate(key, moduleExports[key])) return key
647+
hasExports = true
648+
if (!(key in prevExports)) {
649+
return `Could not Fast Refresh (export "${key}" added)`
650+
}
651+
if (isLikelyComponentType(nextExports[key])) continue
652+
if (isCompoundComponent(nextExports[key])) {
653+
if (!isCompoundComponent(prevExports[key])) {
654+
return `Could not Fast Refresh (compound component "${key}" added)`
655+
}
656+
for (const subKey in prevExports[key]) {
657+
if (!(subKey in nextExports[key])) {
658+
return `Could not Fast Refresh (compound component "${key}.${subKey}" removed)`
659+
}
660+
}
661+
for (const subKey in nextExports[key]) {
662+
if (!(subKey in prevExports[key])) {
663+
return `Could not Fast Refresh (compound component "${key}.${subKey}" added)`
664+
}
665+
}
666+
continue
667+
}
668+
if (prevExports[key] !== nextExports[key]) {
669+
return `Could not Fast Refresh ("${key}" export is incompatible). Learn more at __README_URL__#consistent-components-exports`
670+
}
671+
}
672+
673+
if (hasExports) {
674+
enqueueUpdate()
675+
} else {
676+
return `No exports`
677677
}
678-
return true
679678
}
680679

681680
// Hides vite-ignored dynamic import so that Vite can skip analysis if no other

‎packages/plugin-react-swc/playground/hmr/__tests__/hmr.spec.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ test('HMR invalidate', async ({ page }) => {
4646
"React!'\nexport const useless = 3",
4747
])
4848
await waitForLogs(
49-
'[vite] invalidate /src/TitleWithExport.tsx: Could not Fast Refresh (new export)',
49+
'[vite] invalidate /src/TitleWithExport.tsx: Could not Fast Refresh (export "useless" added)',
5050
'[vite] hot updated: /src/App.tsx',
5151
)
5252

@@ -72,7 +72,7 @@ test('HMR invalidate', async ({ page }) => {
7272
'',
7373
])
7474
await waitForLogs(
75-
'[vite] invalidate /src/TitleWithExport.tsx: Could not Fast Refresh (export removed)',
75+
'[vite] invalidate /src/TitleWithExport.tsx: Could not Fast Refresh (export "Title2" removed)',
7676
'[vite] hot updated: /src/App.tsx',
7777
/Failed to reload \/src\/App\.tsx. This could be due to syntax errors or importing non-existent modules\. \(see errors above\)$/,
7878
)
@@ -89,7 +89,7 @@ test('HMR invalidate', async ({ page }) => {
8989
// Remove useless export
9090
editFile('src/TitleWithExport.tsx', ['\nexport const useless = 3', ''])
9191
await waitForLogs(
92-
'[vite] invalidate /src/TitleWithExport.tsx: Could not Fast Refresh (export removed)',
92+
'[vite] invalidate /src/TitleWithExport.tsx: Could not Fast Refresh (export "useless" removed)',
9393
'[vite] hot updated: /src/App.tsx',
9494
)
9595

‎packages/plugin-react-swc/playground/react-18/__tests__/react-18.spec.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ test('HMR invalidate', async ({ page }) => {
4646
"React!'\nexport const useless = 3",
4747
])
4848
await waitForLogs(
49-
'[vite] invalidate /src/TitleWithExport.tsx: Could not Fast Refresh (new export)',
49+
'[vite] invalidate /src/TitleWithExport.tsx: Could not Fast Refresh (export "useless" added)',
5050
'[vite] hot updated: /src/App.tsx',
5151
)
5252

@@ -72,7 +72,7 @@ test('HMR invalidate', async ({ page }) => {
7272
'',
7373
])
7474
await waitForLogs(
75-
'[vite] invalidate /src/TitleWithExport.tsx: Could not Fast Refresh (export removed)',
75+
'[vite] invalidate /src/TitleWithExport.tsx: Could not Fast Refresh (export "Title2" removed)',
7676
'[vite] hot updated: /src/App.tsx',
7777
/Failed to reload \/src\/App\.tsx. This could be due to syntax errors or importing non-existent modules\. \(see errors above\)$/,
7878
)
@@ -89,7 +89,7 @@ test('HMR invalidate', async ({ page }) => {
8989
// Remove useless export
9090
editFile('src/TitleWithExport.tsx', ['\nexport const useless = 3', ''])
9191
await waitForLogs(
92-
'[vite] invalidate /src/TitleWithExport.tsx: Could not Fast Refresh (export removed)',
92+
'[vite] invalidate /src/TitleWithExport.tsx: Could not Fast Refresh (export "useless" removed)',
9393
'[vite] hot updated: /src/App.tsx',
9494
)
9595

‎packages/plugin-react/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Fix HMR for compound components ([#1484](https://github.com/vitejs/vite-plugin-react/pull/1484))
6+
7+
Adding or removing a component from a compound component was not handled, creating stale updates. Doing this now triggers hmr invalidate on the file and propagates the invalidation to importers.
8+
59
### Fix `compiler.logDiagnostics` option not working ([#1483](https://github.com/vitejs/vite-plugin-react/pull/1483))
610

711
With `oxc-transform-react` >= 0.148, the `logDiagnostics` was not working. This version fixes it and requires `oxc-transform-react` >= 0.152 as a peer dependency if you are using the `compiler` option. `compiler.logDiagnostics` option is now deprecated and should be swapped for the builtin `compiler.reportDiagnostics`.

‎playground/react/__tests__/react.spec.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,33 @@ if (!isBuild) {
184184
await expect
185185
.poll(() => page.textContent('#accordion-root'))
186186
.toMatch('Accordion Root Updated')
187+
188+
await untilBrowserLogAfter(
189+
() =>
190+
editFile('components/Accordion.jsx', (code) =>
191+
code.replace(
192+
'export const Accordion = { Root, Item }',
193+
`function Noop() { return null }
194+
export const Accordion = { Root, Item, Noop }`,
195+
),
196+
),
197+
[
198+
'[vite] invalidate /components/Accordion.jsx: Could not Fast Refresh (compound component "Accordion.Noop" added)',
199+
],
200+
)
201+
202+
await untilBrowserLogAfter(
203+
() =>
204+
editFile('components/Accordion.jsx', (code) =>
205+
code.replace(
206+
'export const Accordion = { Root, Item, Noop }',
207+
`export const Accordion = { Root, Item }`,
208+
),
209+
),
210+
[
211+
'[vite] invalidate /components/Accordion.jsx: Could not Fast Refresh (compound component "Accordion.Noop" removed)',
212+
],
213+
)
187214
})
188215

189216
test('no refresh transform for non-jsx files', async () => {

0 commit comments

Comments
 (0)