8000 fix: handle using compiler with React 18 (#330) · vitejs/vite-plugin-react@ea647d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit ea647d1

Browse files
authored
fix: handle using compiler with React 18 (#330)
1 parent 2737d41 commit ea647d1

File tree

17 files changed

+342
-3
lines changed

17 files changed

+342
-3
lines changed

packages/plugin-react/CHANGELOG.md

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

33
## Unreleased
44

5+
### Fix support for React Compiler with React 18
6+
7+
The previous version made this assumption that the compiler was only usable with React 19, but it's possible to use it with React 18 and a custom `runtimeModule`: https://gist.github.com/poteto/37c076bf112a07ba39d0e5f0645fec43
8+
9+
When using a custom `runtimeModule`, the plugin will not try to pre-optimize `react/compiler-runtime` dependency.
10+
11+
Reminder: Vite expect code outside of `node_modules` to be ESM, so you will need to update the gist with `import React from 'react'`.
12+
513
## 4.3.0 (2024-05-22)
614

715
### Fix support for React compiler

packages/plugin-react/src/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
274274
const dependencies = ['react', jsxImportDevRuntime, jsxImportRuntime]
275275
const staticBabelPlugins =
276276
typeof opts.babel === 'object' ? opts.babel?.plugins ?? [] : []
277-
if (hasCompiler(staticBabelPlugins)) {
277+
if (hasCompilerWithDefaultRuntime(staticBabelPlugins)) {
278278
dependencies.push('react/compiler-runtime')
279279
}
280280

@@ -376,3 +376,14 @@ function hasCompiler(plugins: ReactBabelOptions['plugins']) {
376376
(Array.isArray(p) && p[0] === 'babel-plugin-react-compiler'),
377377
)
378378
}
379+
380+
// https://gist.github.com/poteto/37c076bf112a07ba39d0e5f0645fec43
381+
function hasCompilerWithDefaultRuntime(plugins: ReactBabelOptions['plugins']) {
382+
return plugins.some(
383+
(p) =>
384+
p === 'babel-plugin-react-compiler' ||
385+
(Array.isArray(p) &&
386+
p[0] === 'babel-plugin-react-compiler' &&
387+
p[1]?.runtimeModule === undefined),
388+
)
389+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { expect, test } from 'vitest'
2+
import { editFile, isServe, page, untilUpdated } from '~utils'
3+
4+
test('should render', async () => {
5+
expect(await page.textContent('button')).toMatch('count is 0')
6+
expect(await page.click('button'))
7+
expect(await page.textContent('button')).toMatch('count is 1')
8+
})
9+
10+
test.runIf(isServe)('should hmr', async () => {
11+
editFile('src/App.tsx', (code) =>
12+
code.replace('count is {count}', 'count is {count}!'),
13+
)
14+
await untilUpdated(() => page.textContent('button'), 'count is 1!')
15+
})
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 + TS</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.tsx"></script>
12+
</body>
13+
</html>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react'
2+
const $empty = Symbol.for('react.memo_cache_sentinel')
3+
/**
4+
* DANGER: this hook is NEVER meant to be called directly!
5+
*
6+
* Note that this is a temporary userspace implementation of this function
7+
* from React 19. It is not as efficient and may invalidate more frequently
8+
* than the official API. Please upgrade to React 19 as soon as you can.
9+
**/
10+
export function c(size) {
11+
return React.useState(() => {
12+
const $ = new Array(size)
13+
for (let ii = 0; ii < size; ii++) {
14+
$[ii] = $empty
15+
}
16+
// @ts-ignore
17+
$[$empty] = true
18+
return $
19+
})[0]
20+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "react-compiler-runtime",
3+
"private": true,
4+
"main": "index.js",
5+
"dependencies": {
6+
"react": "^18.3.1"
7+
}
8+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "@vitejs/test-compiler-react-18",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"dev": "vite",
7+
"build": "tsc && vite build",
8+
"preview": "vite preview"
9+
},
10+
"dependencies": {
11+
"react": "^18.3.1",
12+
"react-compiler-runtime": "file:./lib/react-compiler-runtime",
13+
"react-dom": "^18.3.1"
14+
},
15+
"devDependencies": {
16+
"@babel/plugin-transform-react-jsx-development": "^7.22.5",
17+
"@types/react": "^18.3.2",
18+
"@types/react-dom": "^18.3.0",
19+
"@vitejs/plugin-react": "workspace:*",
20+
"babel-plugin-react-compiler": "^0.0.0-experimental-592953e-20240517",
21+
"typescript": "^5.4.5",
22+
"vite": "^5.2.11"
23+
}
24+
}
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#root {
2+
max-width: 1280px;
3+
margin: 0 auto;
4+
padding: 2rem;
5+
text-align: center;
6+
}
7+
8+
.logo {
9+
height: 6em;
10+
padding: 1.5em;
11+
will-change: filter;
12+
transition: filter 300ms;
13+
}
14+
.logo:hover {
15+
filter: drop-shadow(0 0 2em #646cffaa);
16+
}
17+
.logo.react:hover {
18+
filter: drop-shadow(0 0 2em #61dafbaa);
19+
}
20+
21+
@keyframes logo-spin {
22+
from {
23+
transform: rotate(0deg);
24+
}
25+
to {
26+
transform: rotate(360deg);
27+
}
28+
}
29+
30+
@media (prefers-reduced-motion: no-preference) {
31+
a:nth-of-type(2) .logo {
32+
animation: logo-spin infinite 20s linear;
33+
}
34+
}
35+
36+
.card {
37+
padding: 2em;
38+
}
39+
40+
.read-the-docs {
41+
color: #888;
42+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useState } from 'react'
2+
import './App.css'
3+
4+
export function App() {
5+
const [count, setCount] = useState(0)
6+
7+
return (
8+
<>
9+
<h1>Vite + React 18 + compiler</h1>
10+
<div className="card">
11+
<button onClick={() => setCount((count) => count + 1)}>
12+
count is {count}
13+
</button>
14+
<p>
15+
Edit <code>src/App.tsx</code> and save to test HMR
16+
</p>
17+
</div>
18+
<p className="read-the-docs">
19+
Click on the Vite and React logos to learn more
20+
</p>
21+
</>
22+
)
23+
}

0 commit comments

Comments
 (0)
0