Skip to content

Commit 45b5b0f

Browse files
authoredMar 21, 2023
refactor: esbuild plugin config logic (#12493)
1 parent 3e60b77 commit 45b5b0f

File tree

1 file changed

+35
-50
lines changed

1 file changed

+35
-50
lines changed
 

‎packages/vite/src/node/plugins/esbuild.ts

+35-50
Original file line numberDiff line numberDiff line change
@@ -117,30 +117,39 @@ export async function transformWithEsbuild(
117117
}
118118
}
119119

120-
tsconfigRaw = {
121-
...tsconfigRaw,
122-
compilerOptions: {
123-
...compilerOptionsForFile,
124-
...tsconfigRaw?.compilerOptions,
125-
},
120+
const compilerOptions = {
121+
...compilerOptionsForFile,
122+
...tsconfigRaw?.compilerOptions,
126123
}
127124

128-
const { compilerOptions } = tsconfigRaw
129-
if (compilerOptions) {
130-
// esbuild derives `useDefineForClassFields` from `target` instead of `tsconfig.compilerOptions.target`
131-
// https://github.com/evanw/esbuild/issues/2584
132-
// but we want `useDefineForClassFields` to be derived from `tsconfig.compilerOptions.target`
133-
if (compilerOptions.useDefineForClassFields === undefined) {
134-
const lowercaseTarget = compilerOptions.target?.toLowerCase() ?? 'es3'
135-
if (lowercaseTarget.startsWith('es')) {
136-
const esVersion = lowercaseTarget.slice(2)
137-
compilerOptions.useDefineForClassFields =
138-
esVersion === 'next' || +esVersion >= 2022
139-
} else {
140-
compilerOptions.useDefineForClassFields = false
141-
}
125+
// esbuild derives `useDefineForClassFields` from `target` instead of `tsconfig.compilerOptions.target`
126+
// https://github.com/evanw/esbuild/issues/2584
127+
// but we want `useDefineForClassFields` to be derived from `tsconfig.compilerOptions.target`
128+
if (compilerOptions.useDefineForClassFields === undefined) {
129+
const lowercaseTarget = compilerOptions.target?.toLowerCase() ?? 'es3'
130+
if (lowercaseTarget.startsWith('es')) {
131+
const esVersion = lowercaseTarget.slice(2)
132+
compilerOptions.useDefineForClassFields =
133+
esVersion === 'next' || +esVersion >= 2022
134+
} else {
135+
compilerOptions.useDefineForClassFields = false
142136
}
143137
}
138+
139+
// esbuild uses tsconfig fields when both the normal options and tsconfig was set
140+
// but we want to prioritize the normal options
141+
if (options) {
142+
options.jsx && (compilerOptions.jsx = undefined)
143+
options.jsxFactory && (compilerOptions.jsxFactory = undefined)
144+
options.jsxFragment && (compilerOptions.jsxFragmentFactory = undefined)
145+
options.jsxImportSource && (compilerOptions.jsxImportSource = undefined)
146+
options.target && (compilerOptions.target = undefined)
147+
}
148+
149+
tsconfigRaw = {
150+
...tsconfigRaw,
151+
compilerOptions,
152+
}
144153
}
145154

146155
const resolvedOptions = {
@@ -152,29 +161,6 @@ export async function transformWithEsbuild(
152161
tsconfigRaw,
153162
} as ESBuildOptions
154163

155-
// esbuild uses tsconfig fields when both the normal options and tsconfig was set
156-
// but we want to prioritize the normal options
157-
if (
158-
options &&
159-
typeof resolvedOptions.tsconfigRaw === 'object' &&
160-
resolvedOptions.tsconfigRaw.compilerOptions
161-
) {
162-
options.jsx && (resolvedOptions.tsconfigRaw.compilerOptions.jsx = undefined)
163-
options.jsxFactory &&
164-
(resolvedOptions.tsconfigRaw.compilerOptions.jsxFactory = undefined)
165-
options.jsxFragment &&
166-
(resolvedOptions.tsconfigRaw.compilerOptions.jsxFragmentFactory =
167-
undefined)
168-
options.jsxImportSource &&
169-
(resolvedOptions.tsconfigRaw.compilerOptions.jsxImportSource = undefined)
170-
options.target &&
171-
(resolvedOptions.tsconfigRaw.compilerOptions.target = undefined)
172-
}
173-
174-
delete resolvedOptions.include
175-
delete resolvedOptions.exclude
176-
delete resolvedOptions.jsxInject
177-
178164
try {
179165
const result = await transform(code, resolvedOptions)
180166
let map: SourceMap
@@ -210,17 +196,16 @@ export async function transformWithEsbuild(
210196
}
211197

212198
export function esbuildPlugin(options: ESBuildOptions): Plugin {
213-
const filter = createFilter(
214-
options.include || /\.(m?ts|[jt]sx)$/,
215-
options.exclude || /\.js$/,
216-
)
199+
const { jsxInject, include, exclude, ...esbuildTransformOptions } = options
200+
201+
const filter = createFilter(include || /\.(m?ts|[jt]sx)$/, exclude || /\.js$/)
217202

218203
// Remove optimization options for dev as we only need to transpile them,
219204
// and for build as the final optimization is in `buildEsbuildPlugin`
220205
const transformOptions: TransformOptions = {
221206
target: 'esnext',
222207
charset: 'utf8',
223-
...options,
208+
...esbuildTransformOptions,
224209
minify: false,
225210
minifyIdentifiers: false,
226211
minifySyntax: false,
@@ -256,8 +241,8 @@ export function esbuildPlugin(options: ESBuildOptions): Plugin {
256241
this.warn(prettifyMessage(m, code))
257242
})
258243
}
259-
if (options.jsxInject && /\.(?:j|t)sx\b/.test(id)) {
260-
result.code = options.jsxInject + ';' + result.code
244+
if (jsxInject && /\.(?:j|t)sx\b/.test(id)) {
245+
result.code = jsxInject + ';' + result.code
261246
}
262247
return {
263248
code: result.code,

0 commit comments

Comments
 (0)
Please sign in to comment.