Skip to content

Commit

Permalink
feat: add oxc jsxInclude and jsxExclude (#63)
Browse files Browse the repository at this point in the history
Co-authored-by: 翠 / green <green@sapphi.red>
  • Loading branch information
underfin and sapphi-red committed Nov 13, 2024
1 parent 259575f commit 122b8a5
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions packages/vite/src/node/plugins/oxc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export interface OxcOptions extends OxcTransformOptions {
include?: string | RegExp | string[] | RegExp[]
exclude?: string | RegExp | string[] | RegExp[]
jsxInject?: string
jsxInclude?: string | RegExp | string[] | RegExp[]
jsxExclude?: string | RegExp | string[] | RegExp[]
}

export async function transformWithOxc(
Expand Down Expand Up @@ -176,9 +178,26 @@ export async function transformWithOxc(

export function oxcPlugin(config: ResolvedConfig): Plugin {
const options = config.oxc as OxcOptions
const { jsxInject, include, exclude, ...oxcTransformOptions } = options
const {
jsxInject,
include,
exclude,
jsxInclude,
jsxExclude,
...oxcTransformOptions
} = options

const filter = createFilter(include || /\.(m?ts|[jt]sx)$/, exclude || /\.js$/)
const defaultInclude = Array.isArray(include)
? include
: [include || /\.(m?ts|[jt]sx)$/]
const filter = createFilter(
defaultInclude.concat(jsxInclude || []),
exclude || /\.js$/,
)
const jsxFilter = createFilter(
jsxInclude || /\.jsx$/,
jsxExclude || /\.(m?[jt]s|tsx)$/,
)

let server: ViteDevServer

Expand All @@ -189,6 +208,20 @@ export function oxcPlugin(config: ResolvedConfig): Plugin {
},
async transform(code, id) {
if (filter(id) || filter(cleanUrl(id))) {
// disable refresh at ssr
if (
this.environment.config.consumer === 'server' &&
oxcTransformOptions.jsx?.refresh
) {
oxcTransformOptions.jsx.refresh = false
}
if (
(jsxFilter(id) || jsxFilter(cleanUrl(id))) &&
!oxcTransformOptions.lang
) {
oxcTransformOptions.lang = 'jsx'
}

const result = await transformWithOxc(
this,
code,
Expand Down

0 comments on commit 122b8a5

Please sign in to comment.