Skip to content

Commit 3f8b01f

Browse files
committed
chore: update for oxc.jsx type change
1 parent d49efd5 commit 3f8b01f

File tree

2 files changed

+73
-61
lines changed

2 files changed

+73
-61
lines changed

packages/vite/src/node/config.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,10 +1526,13 @@ export async function resolveConfig(
15261526
? false
15271527
: {
15281528
...oxc,
1529-
jsx: {
1530-
development: !isProduction,
1531-
...oxc?.jsx,
1532-
},
1529+
jsx:
1530+
typeof oxc?.jsx === 'string'
1531+
? oxc.jsx
1532+
: {
1533+
development: !isProduction,
1534+
...oxc?.jsx,
1535+
},
15331536
},
15341537
server,
15351538
builder,

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

Lines changed: 66 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -85,36 +85,39 @@ export async function transformWithOxc(
8585
const loadedCompilerOptions = loadedTsconfig.compilerOptions ?? {}
8686
// tsc compiler experimentalDecorators/target/useDefineForClassFields
8787

88-
resolvedOptions.jsx ??= {}
89-
if (loadedCompilerOptions.jsxFactory) {
90-
resolvedOptions.jsx.pragma = loadedCompilerOptions.jsxFactory
91-
}
92-
if (loadedCompilerOptions.jsxFragmentFactory) {
93-
resolvedOptions.jsx.pragmaFrag =
94-
loadedCompilerOptions.jsxFragmentFactory
95-
}
96-
if (loadedCompilerOptions.jsxImportSource) {
97-
resolvedOptions.jsx.importSource = loadedCompilerOptions.jsxImportSource
98-
}
88+
if (loadedCompilerOptions.jsx === 'preserve') {
89+
resolvedOptions.jsx = 'preserve'
90+
} else {
91+
const jsxOptions = {
92+
...(resolvedOptions.jsx === 'preserve' ? {} : resolvedOptions.jsx),
93+
}
9994

100-
switch (loadedCompilerOptions.jsx) {
101-
case 'react-jsxdev':
102-
resolvedOptions.jsx.runtime = 'automatic'
103-
resolvedOptions.jsx.development = true
104-
break
105-
case 'react':
106-
resolvedOptions.jsx.runtime = 'classic'
107-
break
108-
case 'react-jsx':
109-
resolvedOptions.jsx.runtime = 'automatic'
110-
break
111-
case 'preserve':
112-
if (lang === 'tsx') {
113-
ctx?.warn('The tsconfig jsx preserve is not supported by oxc')
114-
}
115-
break
116-
default:
117-
break
95+
if (loadedCompilerOptions.jsxFactory) {
96+
jsxOptions.pragma = loadedCompilerOptions.jsxFactory
97+
}
98+
if (loadedCompilerOptions.jsxFragmentFactory) {
99+
jsxOptions.pragmaFrag = loadedCompilerOptions.jsxFragmentFactory
100+
}
101+
if (loadedCompilerOptions.jsxImportSource) {
102+
jsxOptions.importSource = loadedCompilerOptions.jsxImportSource
103+
}
104+
105+
switch (loadedCompilerOptions.jsx) {
106+
case 'react-jsxdev':
107+
jsxOptions.runtime = 'automatic'
108+
jsxOptions.development = true
109+
break
110+
case 'react':
111+
jsxOptions.runtime = 'classic'
112+
break
113+
case 'react-jsx':
114+
jsxOptions.runtime = 'automatic'
115+
break
116+
default:
117+
break
118+
}
119+
120+
resolvedOptions.jsx = jsxOptions
118121
}
119122

120123
/**
@@ -239,12 +242,14 @@ export function oxcPlugin(config: ResolvedConfig): Plugin {
239242
},
240243
async transform(code, id) {
241244
if (filter(id) || filter(cleanUrl(id))) {
245+
const oxcTransformJsxOptions = oxcTransformOptions.jsx
242246
// disable refresh at ssr
243247
if (
244248
this.environment.config.consumer === 'server' &&
245-
oxcTransformOptions.jsx?.refresh
249+
typeof oxcTransformJsxOptions === 'object' &&
250+
oxcTransformJsxOptions.refresh
246251
) {
247-
oxcTransformOptions.jsx.refresh = false
252+
oxcTransformJsxOptions.refresh = false
248253
}
249254
if (
250255
(jsxFilter(id) || jsxFilter(cleanUrl(id))) &&
@@ -445,6 +450,8 @@ async function generateRuntimeHelpers(
445450
return output.output[0].code
446451
}
447452

453+
type OxcJsxOptions = Exclude<OxcOptions['jsx'], string | undefined>
454+
448455
export function convertEsbuildConfigToOxcConfig(
449456
esbuildConfig: ESBuildOptions,
450457
logger: Logger,
@@ -456,38 +463,40 @@ export function convertEsbuildConfigToOxcConfig(
456463
jsxInject,
457464
include,
458465
exclude,
459-
jsx: {},
460466
}
461467

462-
switch (esbuildTransformOptions.jsx) {
463-
case 'automatic':
464-
oxcOptions.jsx!.runtime = 'automatic'
465-
break
466-
467-
case 'transform':
468-
oxcOptions.jsx!.runtime = 'classic'
469-
break
468+
if (esbuildTransformOptions.jsx === 'preserve') {
469+
oxcOptions.jsx = 'preserve'
470+
} else {
471+
const jsxOptions: OxcJsxOptions = {}
472+
473+
switch (esbuildTransformOptions.jsx) {
474+
case 'automatic':
475+
jsxOptions.runtime = 'automatic'
476+
break
477+
case 'transform':
478+
jsxOptions.runtime = 'classic'
479+
break
480+
default:
481+
break
482+
}
470483

471-
case 'preserve':
472-
logger.warn('The esbuild jsx preserve is not supported by oxc')
473-
break
484+
if (esbuildTransformOptions.jsxDev) {
485+
jsxOptions.development = true
486+
}
487+
if (esbuildTransformOptions.jsxFactory) {
488+
jsxOptions.pragma = esbuildTransformOptions.jsxFactory
489+
}
490+
if (esbuildTransformOptions.jsxFragment) {
491+
jsxOptions.pragmaFrag = esbuildTransformOptions.jsxFragment
492+
}
493+
if (esbuildTransformOptions.jsxImportSource) {
494+
jsxOptions.importSource = esbuildTransformOptions.jsxImportSource
495+
}
474496

475-
default:
476-
break
497+
oxcOptions.jsx = jsxOptions
477498
}
478499

479-
if (esbuildTransformOptions.jsxDev) {
480-
oxcOptions.jsx!.development = true
481-
}
482-
if (esbuildTransformOptions.jsxFactory) {
483-
oxcOptions.jsx!.pragma = esbuildTransformOptions.jsxFactory
484-
}
485-
if (esbuildTransformOptions.jsxFragment) {
486-
oxcOptions.jsx!.pragmaFrag = esbuildTransformOptions.jsxFragment
487-
}
488-
if (esbuildTransformOptions.jsxImportSource) {
489-
oxcOptions.jsx!.importSource = esbuildTransformOptions.jsxImportSource
490-
}
491500
if (esbuildTransformOptions.loader) {
492501
if (['js', 'jsx', 'ts', 'tsx'].includes(esbuildTransformOptions.loader)) {
493502
oxcOptions.lang = esbuildTransformOptions.loader as

0 commit comments

Comments
 (0)