Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/common/refresh-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,16 @@ function isLikelyComponentType(type) {
// Definitely React components.
return true
default:
// Check if this is a compound component (object with all properties being React components)
if (isPlainObject(type)) {
const keys = Object.keys(type)
if (
keys.length > 0 &&
keys.every((key) => isLikelyComponentType(type[key]))
) {
return true
}
}
return false
}
}
Expand All @@ -545,6 +555,13 @@ function isLikelyComponentType(type) {
}
}

function isPlainObject(obj) {
return (
Object.prototype.toString.call(obj) === '[object Object]' &&
(obj.constructor === Object || obj.constructor === undefined)
)
}

/**
* Plugin utils
*/
Expand All @@ -565,6 +582,24 @@ export function registerExportsForReactRefresh(filename, moduleExports) {
// The register function has an identity check to not register twice the same component,
// so this is safe to not used the same key here.
register(exportValue, filename + ' export ' + key)

// If it's a compound component (plain object with component properties),
// also register the individual components
if (
typeof exportValue === 'object' &&
exportValue != null &&
isPlainObject(exportValue) &&
Object.keys(exportValue).every((subKey) =>
isLikelyComponentType(exportValue[subKey]),
)
) {
for (const subKey in exportValue) {
register(
exportValue[subKey],
filename + ' export ' + key + '$' + subKey,
)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { expect, test } from 'vitest'
import { editFile, page, untilBrowserLogAfter } from '~utils'

test('should render compound components', async () => {
expect(await page.textContent('h1')).toMatch('Compound Components HMR Test')
expect(await page.textContent('h3')).toMatch('Accordion Root')
})

test('compound components should use HMR instead of full reload', async () => {
const logs = await untilBrowserLogAfter(() => {
editFile('src/Accordion.tsx', (code) =>
code.replace('Accordion Root', 'Accordion Root Updated'),
)
}, /\[vite\]/)

expect(logs).toContain('[vite] hot updated: /src/Accordion.tsx')
expect(logs).not.toContain('[vite] invalidate')

// revert changes
editFile('src/Accordion.tsx', (code) =>
code.replace('Accordion Root Updated', 'Accordion Root'),
)
})
13 changes: 13 additions & 0 deletions playground/compound-components/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Compound Components Test</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
22 changes: 22 additions & 0 deletions playground/compound-components/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@vitejs/test-compound-components",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"serve": "vite preview"
},
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
"devDependencies": {
"@types/react": "^18.3.5",
"@types/react-dom": "^18.3.1",
"@vitejs/plugin-react": "workspace:*",
"typescript": "^5.5.3",
"vite": "^6.0.5"
}
}
1 change: 1 addition & 0 deletions playground/compound-components/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions playground/compound-components/src/Accordion.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react'

const Root: React.FC<{ children: React.ReactNode }> = ({ children }) => {
return (
<div
style={{
border: '1px solid #ccc',
borderRadius: '4px',
margin: '16px 0',
}}
>
<h3 style={{ padding: '12px', margin: '0', backgroundColor: '#f5f5f5' }}>
Accordion Root
</h3>
<div style={{ padding: '12px' }}>{children}</div>
</div>
)
}

const Item: React.FC<{ children: React.ReactNode }> = ({ children }) => {
return (
<div
style={{
padding: '8px 12px',
border: '1px solid #e0e0e0',
margin: '4px 0',
borderRadius: '2px',
backgroundColor: '#fafafa',
}}
>
{children}
</div>
)
}

export const Accordion = { Root, Item }
21 changes: 21 additions & 0 deletions playground/compound-components/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import { Accordion } from './Accordion'

const root = ReactDOM.createRoot(document.getElementById('root')!)

root.render(
<React.StrictMode>
<div>
<h1>Compound Components HMR Test</h1>
<p>
This demonstrates the compound component pattern that causes full reload
instead of HMR.
</p>
<Accordion.Root>
<Accordion.Item>First Item</Accordion.Item>
<Accordion.Item>Second Item</Accordion.Item>
</Accordion.Root>
</div>
</React.StrictMode>,
)
25 changes: 25 additions & 0 deletions playground/compound-components/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,
"types": ["vite/client"],

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"]
}
14 changes: 14 additions & 0 deletions playground/compound-components/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import react from '@vitejs/plugin-react'
import type { UserConfig } from 'vite'

const config: UserConfig = {
server: { port: 8907 /* Should be unique */ },
mode: 'development',
plugins: [react()],
build: {
// to make tests faster
minify: false,
},
}

export default config
25 changes: 25 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.