Skip to content

Commit de24b6b

Browse files
authored
fix: disable refresh for non-jsx without react import (#318)
1 parent 15690fc commit de24b6b

File tree

1 file changed

+21
-1
lines changed
  • packages/vite/src/node/plugins

1 file changed

+21
-1
lines changed

packages/vite/src/node/plugins/oxc.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,17 @@ export function oxcPlugin(config: ResolvedConfig): Plugin {
327327
? createFilter(jsxRefreshInclude, jsxRefreshExclude)
328328
: undefined
329329

330+
const jsxImportSource =
331+
(typeof oxcTransformOptions.jsx === 'object' &&
332+
oxcTransformOptions.jsx.importSource) ||
333+
'react'
334+
const jsxImportRuntime = `${jsxImportSource}/jsx-runtime`
335+
const jsxImportDevRuntime = `${jsxImportSource}/jsx-dev-runtime`
336+
330337
const getModifiedOxcTransformOptions = (
331338
oxcTransformOptions: OxcTransformOptions,
332339
id: string,
340+
code: string,
333341
environment: Environment,
334342
): OxcTransformOptions => {
335343
const result: OxcTransformOptions = {
@@ -339,11 +347,22 @@ export function oxcPlugin(config: ResolvedConfig): Plugin {
339347
}
340348

341349
const jsxOptions = result.jsx
350+
351+
// disable refresh based by the same condition as @vitejs/plugin-react
352+
// https://github.com/vitejs/vite-plugin-react/blob/c8ecad052001b6fc42e508f18433e6b305bca641/packages/plugin-react/src/index.ts#L261-L269
353+
const [filepath] = id.split('?')
354+
const isJSX = filepath.endsWith('x')
355+
342356
if (
343357
typeof jsxOptions === 'object' &&
344358
jsxOptions.refresh &&
345359
(environment.config.consumer === 'server' ||
346-
(jsxRefreshFilter && !jsxRefreshFilter(id)))
360+
(jsxRefreshFilter && !jsxRefreshFilter(id)) ||
361+
!(
362+
isJSX ||
363+
code.includes(jsxImportRuntime) ||
364+
code.includes(jsxImportDevRuntime)
365+
))
347366
) {
348367
result.jsx = { ...jsxOptions, refresh: false }
349368
}
@@ -380,6 +399,7 @@ export function oxcPlugin(config: ResolvedConfig): Plugin {
380399
const modifiedOxcTransformOptions = getModifiedOxcTransformOptions(
381400
oxcTransformOptions,
382401
id,
402+
code,
383403
this.environment,
384404
)
385405
const result = await transformWithOxc(

0 commit comments

Comments
 (0)