Skip to content

Commit

Permalink
feat(node/plugins): esbuild options (vitejs#11049)
Browse files Browse the repository at this point in the history
Co-authored-by: sapphi-red <green@sapphi.red>
  • Loading branch information
2 people authored and futurGH committed Feb 26, 2023
1 parent e701a64 commit df1741e
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions packages/vite/src/node/plugins/esbuild.ts
Expand Up @@ -50,12 +50,15 @@ export type ESBuildTransformResult = Omit<TransformResult, 'map'> & {
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
}
Expand Down Expand Up @@ -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<keyof TSCompilerOptions> = [
'target',
'alwaysStrict',
'importsNotUsedAsValues',
'jsx',
'jsxFactory',
'jsxFragmentFactory',
'useDefineForClassFields',
'importsNotUsedAsValues',
'jsxImportSource',
'preserveValueImports',
'target',
'useDefineForClassFields',
]
const compilerOptionsForFile: TSCompilerOptions = {}
if (loader === 'ts' || loader === 'tsx') {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit df1741e

Please sign in to comment.