Skip to content

Commit 7517103

Browse files
fix(react): inject __self and __source by babel for rolldown-vite (#467)
Co-authored-by: Arnaud Barré <[email protected]>
1 parent 2337d2f commit 7517103

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

packages/plugin-react/src/index.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { readFileSync } from 'node:fs'
44
import type * as babelCore from '@babel/core'
55
import type { ParserOptions, TransformOptions } from '@babel/core'
66
import { createFilter } from 'vite'
7+
import * as vite from 'vite'
78
import type { Plugin, PluginOption, ResolvedConfig } from 'vite'
89
import {
910
addRefreshWrapper,
@@ -124,10 +125,24 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
124125
enforce: 'pre',
125126
config() {
126127
if (opts.jsxRuntime === 'classic') {
127-
return {
128-
esbuild: {
129-
jsx: 'transform',
130-
},
128+
if ('rolldownVersion' in vite) {
129+
return {
130+
oxc: {
131+
jsx: {
132+
runtime: 'classic',
133+
// disable __self and __source injection even in dev
134+
// as this plugin injects them by babel and oxc will throw
135+
// if development is enabled and those properties are already present
136+
development: false,
137+
},
138+
},
139+
}
140+
} else {
141+
return {
142+
esbuild: {
143+
jsx: 'transform',
144+
},
145+
}
131146
}
132147
} else {
133148
return {
@@ -203,14 +218,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
203218
])
204219
}
205220

206-
if (
207-
opts.jsxRuntime === 'classic' &&
208-
isJSX &&
209-
// OXC injects self and source so these plugins are not needed for rolldown-vite
210-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
211-
// @ts-ignore -- this.meta.rolldownVersion only exists in rolldown-vite
212-
!this.meta.rolldownVersion
213-
) {
221+
if (opts.jsxRuntime === 'classic' && isJSX) {
214222
if (!isProduction) {
215223
// These development plugins are only needed for the classic runtime.
216224
plugins.push(

0 commit comments

Comments
 (0)