@@ -117,30 +117,39 @@ export async function transformWithEsbuild(
117
117
}
118
118
}
119
119
120
- tsconfigRaw = {
121
- ...tsconfigRaw ,
122
- compilerOptions : {
123
- ...compilerOptionsForFile ,
124
- ...tsconfigRaw ?. compilerOptions ,
125
- } ,
120
+ const compilerOptions = {
121
+ ...compilerOptionsForFile ,
122
+ ...tsconfigRaw ?. compilerOptions ,
126
123
}
127
124
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
142
136
}
143
137
}
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
+ }
144
153
}
145
154
146
155
const resolvedOptions = {
@@ -152,29 +161,6 @@ export async function transformWithEsbuild(
152
161
tsconfigRaw,
153
162
} as ESBuildOptions
154
163
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
-
178
164
try {
179
165
const result = await transform ( code , resolvedOptions )
180
166
let map : SourceMap
@@ -210,17 +196,16 @@ export async function transformWithEsbuild(
210
196
}
211
197
212
198
export function esbuildPlugin ( options : ESBuildOptions ) : Plugin {
213
- const filter = createFilter (
214
- options . include || / \. ( m ? t s | [ j t ] s x ) $ / ,
215
- options . exclude || / \. j s $ / ,
216
- )
199
+ const { jsxInject, include, exclude, ...esbuildTransformOptions } = options
200
+
201
+ const filter = createFilter ( include || / \. ( m ? t s | [ j t ] s x ) $ / , exclude || / \. j s $ / )
217
202
218
203
// Remove optimization options for dev as we only need to transpile them,
219
204
// and for build as the final optimization is in `buildEsbuildPlugin`
220
205
const transformOptions : TransformOptions = {
221
206
target : 'esnext' ,
222
207
charset : 'utf8' ,
223
- ...options ,
208
+ ...esbuildTransformOptions ,
224
209
minify : false ,
225
210
minifyIdentifiers : false ,
226
211
minifySyntax : false ,
@@ -256,8 +241,8 @@ export function esbuildPlugin(options: ESBuildOptions): Plugin {
256
241
this . warn ( prettifyMessage ( m , code ) )
257
242
} )
258
243
}
259
- if ( options . jsxInject && / \. (?: j | t ) s x \b / . test ( id ) ) {
260
- result . code = options . jsxInject + ';' + result . code
244
+ if ( jsxInject && / \. (?: j | t ) s x \b / . test ( id ) ) {
245
+ result . code = jsxInject + ';' + result . code
261
246
}
262
247
return {
263
248
code : result . code ,
0 commit comments