Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/plugin-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

### Respect tsconfig `jsxImportSource` ([#726](https://github.com/vitejs/vite-plugin-react/pull/726))

### Fix `reactRefreshHost` option on rolldown-vite ([#716](https://github.com/vitejs/vite-plugin-react/pull/716))

### Fix `RefreshRuntime` being injected twice for class components on rolldown-vite ([#708](https://github.com/vitejs/vite-plugin-react/pull/708))
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default defineConfig({

### jsxImportSource

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

```js
react({ jsxImportSource: '@emotion/react' })
Expand Down
5 changes: 3 additions & 2 deletions packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default function viteReact(opts: Options = {}): Plugin[] {
oxc: {
jsx: {
runtime: 'automatic',
importSource: jsxImportSource,
importSource: opts.jsxImportSource,
refresh: command === 'serve',
},
jsxRefreshInclude: include,
Expand All @@ -174,7 +174,8 @@ export default function viteReact(opts: Options = {}): Plugin[] {
return {
esbuild: {
jsx: 'automatic',
jsxImportSource: jsxImportSource,
// keep undefined by default so that vite's esbuild transform can prioritize jsxImportSource from tsconfig
jsxImportSource: opts.jsxImportSource,
},
optimizeDeps: { esbuildOptions: { jsx: 'automatic' } },
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { expect, test } from 'vitest'
import { page } from '~utils'

test('override tsconfig jsx preserve', async () => {
await expect.poll(() => page.textContent('#app')).toBe('ok')
})
2 changes: 2 additions & 0 deletions playground/tsconfig-jsx-preserve/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div id="app"></div>
<script type="module" src="./src/main.tsx"></script>
20 changes: 20 additions & 0 deletions playground/tsconfig-jsx-preserve/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@vitejs/test-react-tsconfig-jsx-preserve",
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk vite",
"preview": "vite preview"
},
"dependencies": {
"react": "^19.1.1",
"react-dom": "^19.1.1"
},
"devDependencies": {
"@types/react": "^19.1.9",
"@types/react-dom": "^19.1.7",
"@vitejs/plugin-react": "workspace:*"
}
}
3 changes: 3 additions & 0 deletions playground/tsconfig-jsx-preserve/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function App() {
return <div>ok</div>
}
4 changes: 4 additions & 0 deletions playground/tsconfig-jsx-preserve/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import ReactDOM from 'react-dom/client'
import App from './App.jsx'

ReactDOM.createRoot(document.getElementById('app')!).render(<App />)
25 changes: 25 additions & 0 deletions playground/tsconfig-jsx-preserve/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"include": ["src"],
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"types": ["vite/client"],
"module": "ESNext",
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
}
}
6 changes: 6 additions & 0 deletions playground/tsconfig-jsx-preserve/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'

export default defineConfig({
plugins: [react()],
})
10 changes: 10 additions & 0 deletions playground/tsconfig/__tests__/tsconfig.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { expect, test } from 'vitest'
import { page } from '~utils'

test('respect tsconfig jsxImportSource', async () => {
await expect
.poll(() =>
page.getByTestId('emotion').evaluate((el) => getComputedStyle(el).color),
)
.toBe('rgb(255, 0, 0)')
})
2 changes: 2 additions & 0 deletions playground/tsconfig/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div id="app"></div>
<script type="module" src="./src/main.tsx"></script>
21 changes: 21 additions & 0 deletions playground/tsconfig/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@vitejs/test-react-tsconfig",
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk vite",
"preview": "vite preview"
},
"dependencies": {
"@emotion/react": "^11.14.0",
"react": "^19.1.1",
"react-dom": "^19.1.1"
},
"devDependencies": {
"@types/react": "^19.1.9",
"@types/react-dom": "^19.1.7",
"@vitejs/plugin-react": "workspace:*"
}
}
7 changes: 7 additions & 0 deletions playground/tsconfig/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function App() {
return (
<div data-testid="emotion" css={{ color: 'rgb(255, 0, 0)' }}>
This should be red
</div>
)
}
4 changes: 4 additions & 0 deletions playground/tsconfig/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import ReactDOM from 'react-dom/client'
import App from './App.jsx'

ReactDOM.createRoot(document.getElementById('app')!).render(<App />)
26 changes: 26 additions & 0 deletions playground/tsconfig/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"include": ["src"],
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"types": ["vite/client"],
"module": "ESNext",
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"jsxImportSource": "@emotion/react",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can update the playground/emotion to comment the manual jsxImportSource (saying we expect to be infered from tsconfig)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I should reuse that but I'm not sure how to get esbuild to pickup tsconfig for jsx. I'll update the example to tsx in a later PR and remove this one.


/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
}
}
6 changes: 6 additions & 0 deletions playground/tsconfig/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'

export default defineConfig({
plugins: [react()],
})
Loading
Loading