Skip to content
This repository was archived by the owner on Jul 24, 2025. It is now read-only.

Commit 53878df

Browse files
authored
feat: support HMR for class components (#203)
1 parent 21eef9e commit 53878df

File tree

12 files changed

+135
-6
lines changed

12 files changed

+135
-6
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { expect, test } from "@playwright/test";
2+
import { setupDevServer, setupWaitForLogs } from "../../utils.ts";
3+
4+
test("Class component HMR", async ({ page }) => {
5+
const { testUrl, server, editFile } =
6+
await setupDevServer("class-components");
7+
const waitForLogs = await setupWaitForLogs(page);
8+
await page.goto(testUrl);
9+
10+
await expect(page.locator("body")).toHaveText("Hello World");
11+
editFile("src/App.tsx", ["World", "class components"]);
12+
await waitForLogs("[vite] hot updated: /src/App.tsx");
13+
await expect(page.locator("body")).toHaveText("Hello class components");
14+
15+
editFile("src/utils.tsx", ["Hello", "Hi"]);
16+
await waitForLogs("[vite] hot updated: /src/App.tsx");
17+
await expect(page.locator("body")).toHaveText("Hi class components");
18+
19+
await server.close();
20+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + React + class components</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/index.tsx"></script>
12+
</body>
13+
</html>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "class-components",
3+
"type": "module",
4+
"private": true,
5+
"scripts": {
6+
"dev": "vite",
7+
"build": "vite build",
8+
"preview": "vite preview"
9+
},
10+
"dependencies": {
11+
"react": "^18.2.0",
12+
"react-dom": "^18.2.0"
13+
},
14+
"devDependencies": {
15+
"@types/react": "^18.2.48",
16+
"@types/react-dom": "^18.2.18",
17+
"@vitejs/plugin-react-swc": "../../dist"
18+
}
19+
}
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Component } from "react";
2+
import { getGetting } from "./utils.tsx";
3+
4+
export class App extends Component {
5+
render() {
6+
return <span>{getGetting()} World</span>;
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { StrictMode } from "react";
2+
import { createRoot } from "react-dom/client";
3+
import { App } from "./App.tsx";
4+
5+
createRoot(document.getElementById("root")!).render(
6+
<StrictMode>
7+
<App />
8+
</StrictMode>,
9+
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const getGetting = () => <span>Hello</span>;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"include": ["src", "vite.config.ts"],
3+
"compilerOptions": {
4+
"module": "ESNext",
5+
"lib": ["ESNext", "DOM", "DOM.Iterable"],
6+
"target": "ESNext",
7+
"jsx": "react-jsx",
8+
"types": ["vite/client"],
9+
"noEmit": true,
10+
"isolatedModules": true,
11+
"skipLibCheck": true,
12+
"moduleResolution": "bundler",
13+
"allowImportingTsExtensions": true,
14+
"resolveJsonModule": true,
15+
16+
/* Linting */
17+
"strict": true,
18+
"noUnusedLocals": true,
19+
"noUnusedParameters": true,
20+
"noFallthroughCasesInSwitch": true,
21+
"useUnknownInCatchVariables": true
22+
}
23+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { defineConfig } from "vite";
2+
import react from "@vitejs/plugin-react-swc";
3+
4+
export default defineConfig({
5+
plugins: [react()],
6+
});

0 commit comments

Comments
 (0)