From 122b8a52fc837e5cbce04f69dbba0ae91d70a013 Mon Sep 17 00:00:00 2001 From: underfin Date: Fri, 18 Oct 2024 17:56:28 +0800 Subject: [PATCH] feat: add oxc jsxInclude and jsxExclude (#63) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 翠 / green --- packages/vite/src/node/plugins/oxc.ts | 37 +++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/plugins/oxc.ts b/packages/vite/src/node/plugins/oxc.ts index e0578bb314d391..d7d5b03ec2f80e 100644 --- a/packages/vite/src/node/plugins/oxc.ts +++ b/packages/vite/src/node/plugins/oxc.ts @@ -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( @@ -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 @@ -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,