Skip to content

Commit e2f0c78

Browse files
hi-ogawaclaude
andauthored
fix(react): respect tsconfig jsxImportSource by default (#726)
Co-authored-by: Claude <[email protected]>
1 parent fd96308 commit e2f0c78

File tree

19 files changed

+358
-16
lines changed

19 files changed

+358
-16
lines changed

packages/plugin-react/CHANGELOG.md

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

33
## Unreleased
44

5+
### Respect tsconfig `jsxImportSource` ([#726](https://github.com/vitejs/vite-plugin-react/pull/726))
6+
57
### Fix `reactRefreshHost` option on rolldown-vite ([#716](https://github.com/vitejs/vite-plugin-react/pull/716))
68

79
### Fix `RefreshRuntime` being injected twice for class components on rolldown-vite ([#708](https://github.com/vitejs/vite-plugin-react/pull/708))

packages/plugin-react/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default defineConfig({
3838

3939
### jsxImportSource
4040

41-
Control where the JSX factory is imported from. Default to `'react'`
41+
Control where the JSX factory is imported from. By default, this is inferred from `jsxImportSource` from corresponding a tsconfig file for a transformed file.
4242

4343
```js
4444
react({ jsxImportSource: '@emotion/react' })

packages/plugin-react/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export default function viteReact(opts: Options = {}): Plugin[] {
153153
oxc: {
154154
jsx: {
155155
runtime: 'automatic',
156-
importSource: jsxImportSource,
156+
importSource: opts.jsxImportSource,
157157
refresh: command === 'serve',
158158
},
159159
jsxRefreshInclude: include,
@@ -174,7 +174,8 @@ export default function viteReact(opts: Options = {}): Plugin[] {
174174
return {
175175
esbuild: {
176176
jsx: 'automatic',
177-
jsxImportSource: jsxImportSource,
177+
// keep undefined by default so that vite's esbuild transform can prioritize jsxImportSource from tsconfig
178+
jsxImportSource: opts.jsxImportSource,
178179
},
179180
optimizeDeps: { esbuildOptions: { jsx: 'automatic' } },
180181
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { expect, test } from 'vitest'
2+
import { page } from '~utils'
3+
4+
test('override tsconfig jsx preserve', async () => {
5+
await expect.poll(() => page.textContent('#app')).toBe('ok')
6+
})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<div id="app"></div>
2+
<script type="module" src="./src/main.tsx"></script>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "@vitejs/test-react-tsconfig-jsx-preserve",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"dev": "vite",
7+
"build": "vite build",
8+
"debug": "node --inspect-brk vite",
9+
"preview": "vite preview"
10+
},
11+
"dependencies": {
12+
"react": "^19.1.1",
13+
"react-dom": "^19.1.1"
14+
},
15+
"devDependencies": {
16+
"@types/react": "^19.1.9",
17+
"@types/react-dom": "^19.1.7",
18+
"@vitejs/plugin-react": "workspace:*"
19+
}
20+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function App() {
2+
return <div>ok</div>
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import ReactDOM from 'react-dom/client'
2+
import App from './App.jsx'
3+
4+
ReactDOM.createRoot(document.getElementById('app')!).render(<App />)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"include": ["src"],
3+
"compilerOptions": {
4+
"target": "ES2020",
5+
"useDefineForClassFields": true,
6+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
7+
"types": ["vite/client"],
8+
"module": "ESNext",
9+
"skipLibCheck": true,
10+
11+
/* Bundler mode */
12+
"moduleResolution": "bundler",
13+
"allowImportingTsExtensions": true,
14+
"resolveJsonModule": true,
15+
"isolatedModules": true,
16+
"noEmit": true,
17+
"jsx": "preserve",
18+
19+
/* Linting */
20+
"strict": true,
21+
"noUnusedLocals": true,
22+
"noUnusedParameters": true,
23+
"noFallthroughCasesInSwitch": true
24+
}
25+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import react from '@vitejs/plugin-react'
2+
import { defineConfig } from 'vite'
3+
4+
export default defineConfig({
5+
plugins: [react()],
6+
})

0 commit comments

Comments
 (0)