Skip to content

Commit 608039f

Browse files
committed
fix: don't modify oxc options in each transform
1 parent 796d5b3 commit 608039f

File tree

1 file changed

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

1 file changed

+35
-21
lines changed

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

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
import type { ResolvedConfig } from '../config'
2020
import type { Plugin, PluginContext } from '../plugin'
2121
import { cleanUrl } from '../../shared/utils'
22-
import type { Logger } from '..'
22+
import type { Environment, Logger } from '..'
2323
import type { ViteDevServer } from '../server'
2424
import type { ESBuildOptions } from './esbuild'
2525
import { loadTsconfigJsonForFile } from './esbuild'
@@ -301,6 +301,34 @@ export function oxcPlugin(config: ResolvedConfig): Plugin {
301301
jsxExclude || /\.(m?[jt]s|tsx)$/,
302302
)
303303

304+
const getModifiedOxcTransformOptions = (
305+
oxcTransformOptions: OxcTransformOptions,
306+
id: string,
307+
environment: Environment,
308+
): OxcTransformOptions => {
309+
const result: OxcTransformOptions = {
310+
...oxcTransformOptions,
311+
sourcemap:
312+
environment.mode !== 'build' || !!environment.config.build.sourcemap,
313+
}
314+
315+
const jsxOptions = result.jsx
316+
// disable refresh at ssr
317+
if (
318+
environment.config.consumer === 'server' &&
319+
typeof jsxOptions === 'object' &&
320+
jsxOptions.refresh
321+
) {
322+
result.jsx = { ...jsxOptions, refresh: false }
323+
}
324+
325+
if ((jsxFilter(id) || jsxFilter(cleanUrl(id))) && !result.lang) {
326+
result.lang = 'jsx'
327+
}
328+
329+
return result
330+
}
331+
304332
let server: ViteDevServer
305333

306334
return {
@@ -310,30 +338,16 @@ export function oxcPlugin(config: ResolvedConfig): Plugin {
310338
},
311339
async transform(code, id) {
312340
if (filter(id) || filter(cleanUrl(id))) {
313-
const oxcTransformJsxOptions = oxcTransformOptions.jsx
314-
// disable refresh at ssr
315-
if (
316-
this.environment.config.consumer === 'server' &&
317-
typeof oxcTransformJsxOptions === 'object' &&
318-
oxcTransformJsxOptions.refresh
319-
) {
320-
oxcTransformJsxOptions.refresh = false
321-
}
322-
if (
323-
(jsxFilter(id) || jsxFilter(cleanUrl(id))) &&
324-
!oxcTransformOptions.lang
325-
) {
326-
oxcTransformOptions.lang = 'jsx'
327-
}
328-
oxcTransformOptions.sourcemap =
329-
this.environment.mode !== 'build' ||
330-
!!this.environment.config.build.sourcemap
331-
341+
const modifiedOxcTransformOptions = getModifiedOxcTransformOptions(
342+
oxcTransformOptions,
343+
id,
344+
this.environment,
345+
)
332346
const result = await transformWithOxc(
333347
this,
334348
code,
335349
id,
336-
oxcTransformOptions,
350+
modifiedOxcTransformOptions,
337351
undefined,
338352
config,
339353
server?.watcher,

0 commit comments

Comments
 (0)