Skip to content

Commit a6aac50

Browse files
committed
fix(react): respect tsconfig jsxImportSource by default
1 parent 74ec0e0 commit a6aac50

File tree

9 files changed

+109
-2
lines changed

9 files changed

+109
-2
lines changed

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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { expect, test } from 'vitest'
2+
import { page } from '~utils'
3+
4+
test('respect tsconfig jsxImportSource', async () => {
5+
await expect
6+
.poll(() =>
7+
page.getByTestId('emotion').evaluate((el) => getComputedStyle(el).color),
8+
)
9+
.toBe('rgb(255, 0, 0)')
10+
})

playground/tsconfig/index.html

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>

playground/tsconfig/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "@vitejs/test-react-tsconfig",
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+
"@emotion/react": "^11.14.0",
13+
"react": "^19.1.1",
14+
"react-dom": "^19.1.1"
15+
},
16+
"devDependencies": {
17+
"@types/react": "^19.1.9",
18+
"@types/react-dom": "^19.1.7",
19+
"@vitejs/plugin-react": "workspace:*"
20+
}
21+
}

playground/tsconfig/src/App.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function App() {
2+
return (
3+
<div data-testid="emotion" css={{ color: 'rgb(255, 0, 0)' }}>
4+
This should be red
5+
</div>
6+
)
7+
}

playground/tsconfig/src/main.tsx

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 />)

playground/tsconfig/tsconfig.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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": "react-jsx",
18+
"jsxImportSource": "@emotion/react",
19+
20+
/* Linting */
21+
"strict": true,
22+
"noUnusedLocals": true,
23+
"noUnusedParameters": true,
24+
"noFallthroughCasesInSwitch": true
25+
}
26+
}

playground/tsconfig/vite.config.ts

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+
})

pnpm-lock.yaml

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)