1
1
import path from 'node:path'
2
2
import colors from 'picocolors'
3
3
import type {
4
- Loader ,
5
- Message ,
6
- TransformOptions ,
7
- TransformResult ,
8
- } from 'esbuild'
9
- import { transform } from 'esbuild'
4
+ EsbuildLoader ,
5
+ EsbuildMessage ,
6
+ EsbuildTransformOptions ,
7
+ EsbuildTransformResult as RawEsbuildTransformResult ,
8
+ } from 'types/internal/esbuildOptions'
10
9
import type { RawSourceMap } from '@ampproject/remapping'
11
10
import type { InternalModuleFormat , SourceMap } from 'rolldown'
12
11
import type { TSConfckParseResult } from 'tsconfck'
@@ -43,7 +42,7 @@ export const defaultEsbuildSupported = {
43
42
'import-meta' : true ,
44
43
}
45
44
46
- export interface ESBuildOptions extends TransformOptions {
45
+ export interface ESBuildOptions extends EsbuildTransformOptions {
47
46
include ?: string | RegExp | ReadonlyArray < string | RegExp >
48
47
exclude ?: string | RegExp | ReadonlyArray < string | RegExp >
49
48
jsxInject ?: string
@@ -53,7 +52,7 @@ export interface ESBuildOptions extends TransformOptions {
53
52
minify ?: never
54
53
}
55
54
56
- export type ESBuildTransformResult = Omit < TransformResult , 'map' > & {
55
+ export type ESBuildTransformResult = Omit < RawEsbuildTransformResult , 'map' > & {
57
56
map : SourceMap
58
57
}
59
58
@@ -76,10 +75,16 @@ type TSConfigJSON = {
76
75
}
77
76
type TSCompilerOptions = NonNullable < TSConfigJSON [ 'compilerOptions' ] >
78
77
78
+ let esbuild : Promise < typeof import ( 'esbuild' ) > | undefined
79
+ const importEsbuild = ( ) => {
80
+ esbuild ||= import ( 'esbuild' )
81
+ return esbuild
82
+ }
83
+
79
84
export async function transformWithEsbuild (
80
85
code : string ,
81
86
filename : string ,
82
- options ?: TransformOptions ,
87
+ options ?: EsbuildTransformOptions ,
83
88
inMap ?: object ,
84
89
config ?: ResolvedConfig ,
85
90
watcher ?: FSWatcher ,
@@ -98,7 +103,7 @@ export async function transformWithEsbuild(
98
103
} else if ( ext === 'cts' || ext === 'mts' ) {
99
104
loader = 'ts'
100
105
} else {
101
- loader = ext as Loader
106
+ loader = ext as EsbuildLoader
102
107
}
103
108
}
104
109
@@ -179,7 +184,7 @@ export async function transformWithEsbuild(
179
184
}
180
185
}
181
186
182
- const resolvedOptions : TransformOptions = {
187
+ const resolvedOptions : EsbuildTransformOptions = {
183
188
sourcemap : true ,
184
189
// ensure source file name contains full query
185
190
sourcefile : filename ,
@@ -198,6 +203,7 @@ export async function transformWithEsbuild(
198
203
delete resolvedOptions . jsxInject
199
204
200
205
try {
206
+ const { transform } = await importEsbuild ( )
201
207
const result = await transform ( code , resolvedOptions )
202
208
let map : SourceMap
203
209
if ( inMap && resolvedOptions . sourcemap ) {
@@ -222,7 +228,7 @@ export async function transformWithEsbuild(
222
228
// patch error information
223
229
if ( e . errors ) {
224
230
e . frame = ''
225
- e . errors . forEach ( ( m : Message ) => {
231
+ e . errors . forEach ( ( m : EsbuildMessage ) => {
226
232
if (
227
233
m . text === 'Experimental decorators are not currently enabled' ||
228
234
m . text ===
@@ -247,7 +253,7 @@ export function esbuildPlugin(config: ResolvedConfig): Plugin {
247
253
248
254
// Remove optimization options for dev as we only need to transpile them,
249
255
// and for build as the final optimization is in `buildEsbuildPlugin`
250
- const transformOptions : TransformOptions = {
256
+ const transformOptions : EsbuildTransformOptions = {
251
257
target : 'esnext' ,
252
258
charset : 'utf8' ,
253
259
...esbuildTransformOptions ,
@@ -303,7 +309,7 @@ export function esbuildPlugin(config: ResolvedConfig): Plugin {
303
309
304
310
const rollupToEsbuildFormatMap : Record <
305
311
string ,
306
- TransformOptions [ 'format' ] | undefined
312
+ EsbuildTransformOptions [ 'format' ] | undefined
307
313
> = {
308
314
es : 'esm' ,
309
315
cjs : 'cjs' ,
@@ -381,7 +387,7 @@ export const buildEsbuildPlugin = (): Plugin => {
381
387
export function resolveEsbuildTranspileOptions (
382
388
config : ResolvedConfig ,
383
389
format : InternalModuleFormat ,
384
- ) : TransformOptions | null {
390
+ ) : EsbuildTransformOptions | null {
385
391
const target = config . build . target
386
392
const minify = config . build . minify === 'esbuild'
387
393
@@ -395,7 +401,7 @@ export function resolveEsbuildTranspileOptions(
395
401
const isEsLibBuild = config . build . lib && format === 'es'
396
402
const esbuildOptions = config . esbuild || { }
397
403
398
- const options : TransformOptions = {
404
+ const options : EsbuildTransformOptions = {
399
405
charset : 'utf8' ,
400
406
...esbuildOptions ,
401
407
loader : 'js' ,
@@ -467,7 +473,7 @@ export function resolveEsbuildTranspileOptions(
467
473
}
468
474
}
469
475
470
- function prettifyMessage ( m : Message , code : string ) : string {
476
+ function prettifyMessage ( m : EsbuildMessage , code : string ) : string {
471
477
let res = colors . yellow ( m . text )
472
478
if ( m . location ) {
473
479
res += `\n` + generateCodeFrame ( code , m . location )
0 commit comments