-
-
Notifications
You must be signed in to change notification settings - Fork 157
feat: add hmr support for compound components #518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Newbie012
wants to merge
1
commit into
vitejs:main
Choose a base branch
from
Newbie012:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
playground/compound-components/__tests__/compound-components.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason we need this?
typeof obj === "object" && obj !== null
should be enough no?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, but in your condition, values such as Date, [], Map, etc will return true as well, which is not what we want. Or maybe it's an oversight on my end?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hum true, it will still be possible to loop over keys but it's cleaner to not do it for these so we can keep this