Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add oxc jsxInclude and jsxExclude #63

Merged
merged 6 commits into from
Oct 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -25,6 +25,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 @@ -159,9 +161,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)$/,
)

return {
name: 'vite:oxc',
Expand All @@ -178,6 +197,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