diff --git a/packages/vite/src/node/plugins/esbuild.ts b/packages/vite/src/node/plugins/esbuild.ts index 82a2a1252a2f5f..0dc57c6328dee5 100644 --- a/packages/vite/src/node/plugins/esbuild.ts +++ b/packages/vite/src/node/plugins/esbuild.ts @@ -50,12 +50,15 @@ export type ESBuildTransformResult = Omit & { type TSConfigJSON = { extends?: string compilerOptions?: { - target?: string + alwaysStrict?: boolean + importsNotUsedAsValues?: 'remove' | 'preserve' | 'error' + jsx?: 'preserve' | 'react' | 'react-jsx' | 'react-jsxdev' jsxFactory?: string jsxFragmentFactory?: string - useDefineForClassFields?: boolean - importsNotUsedAsValues?: 'remove' | 'preserve' | 'error' + jsxImportSource?: string preserveValueImports?: boolean + target?: string + useDefineForClassFields?: boolean } [key: string]: any } @@ -92,12 +95,15 @@ export async function transformWithEsbuild( // these fields would affect the compilation result // https://esbuild.github.io/content-types/#tsconfig-json const meaningfulFields: Array = [ - 'target', + 'alwaysStrict', + 'importsNotUsedAsValues', + 'jsx', 'jsxFactory', 'jsxFragmentFactory', - 'useDefineForClassFields', - 'importsNotUsedAsValues', + 'jsxImportSource', 'preserveValueImports', + 'target', + 'useDefineForClassFields', ] const compilerOptionsForFile: TSCompilerOptions = {} if (loader === 'ts' || loader === 'tsx') { @@ -130,6 +136,25 @@ export async function transformWithEsbuild( tsconfigRaw, } as ESBuildOptions + // esbuild uses tsconfig fields when both the normal options and tsconfig was set + // but we want to prioritize the normal options + if ( + options && + typeof resolvedOptions.tsconfigRaw === 'object' && + resolvedOptions.tsconfigRaw.compilerOptions + ) { + options.jsx && (resolvedOptions.tsconfigRaw.compilerOptions.jsx = undefined) + options.jsxFactory && + (resolvedOptions.tsconfigRaw.compilerOptions.jsxFactory = undefined) + options.jsxFragment && + (resolvedOptions.tsconfigRaw.compilerOptions.jsxFragmentFactory = + undefined) + options.jsxImportSource && + (resolvedOptions.tsconfigRaw.compilerOptions.jsxImportSource = undefined) + options.target && + (resolvedOptions.tsconfigRaw.compilerOptions.target = undefined) + } + delete resolvedOptions.include delete resolvedOptions.exclude delete resolvedOptions.jsxInject