Skip to content

Commit

Permalink
fix: don't modify oxc options in each transform
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Jan 17, 2025
1 parent 796d5b3 commit 608039f
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions packages/vite/src/node/plugins/oxc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import type { ResolvedConfig } from '../config'
import type { Plugin, PluginContext } from '../plugin'
import { cleanUrl } from '../../shared/utils'
import type { Logger } from '..'
import type { Environment, Logger } from '..'
import type { ViteDevServer } from '../server'
import type { ESBuildOptions } from './esbuild'
import { loadTsconfigJsonForFile } from './esbuild'
Expand Down Expand Up @@ -301,6 +301,34 @@ export function oxcPlugin(config: ResolvedConfig): Plugin {
jsxExclude || /\.(m?[jt]s|tsx)$/,
)

const getModifiedOxcTransformOptions = (
oxcTransformOptions: OxcTransformOptions,
id: string,
environment: Environment,
): OxcTransformOptions => {
const result: OxcTransformOptions = {
...oxcTransformOptions,
sourcemap:
environment.mode !== 'build' || !!environment.config.build.sourcemap,
}

const jsxOptions = result.jsx
// disable refresh at ssr
if (
environment.config.consumer === 'server' &&
typeof jsxOptions === 'object' &&
jsxOptions.refresh
) {
result.jsx = { ...jsxOptions, refresh: false }
}

if ((jsxFilter(id) || jsxFilter(cleanUrl(id))) && !result.lang) {
result.lang = 'jsx'
}

return result
}

let server: ViteDevServer

return {
Expand All @@ -310,30 +338,16 @@ export function oxcPlugin(config: ResolvedConfig): Plugin {
},
async transform(code, id) {
if (filter(id) || filter(cleanUrl(id))) {
const oxcTransformJsxOptions = oxcTransformOptions.jsx
// disable refresh at ssr
if (
this.environment.config.consumer === 'server' &&
typeof oxcTransformJsxOptions === 'object' &&
oxcTransformJsxOptions.refresh
) {
oxcTransformJsxOptions.refresh = false
}
if (
(jsxFilter(id) || jsxFilter(cleanUrl(id))) &&
!oxcTransformOptions.lang
) {
oxcTransformOptions.lang = 'jsx'
}
oxcTransformOptions.sourcemap =
this.environment.mode !== 'build' ||
!!this.environment.config.build.sourcemap

const modifiedOxcTransformOptions = getModifiedOxcTransformOptions(
oxcTransformOptions,
id,
this.environment,
)
const result = await transformWithOxc(
this,
code,
id,
oxcTransformOptions,
modifiedOxcTransformOptions,
undefined,
config,
server?.watcher,
Expand Down

0 comments on commit 608039f

Please sign in to comment.