@@ -3,7 +3,7 @@ import path from 'node:path'
3
3
import { performance } from 'node:perf_hooks'
4
4
import glob from 'fast-glob'
5
5
import type { Loader , OnLoadResult , Plugin } from 'esbuild'
6
- import { build , transform } from 'esbuild'
6
+ import { build , formatMessages , transform } from 'esbuild'
7
7
import colors from 'picocolors'
8
8
import type { ResolvedConfig } from '..'
9
9
import {
@@ -106,19 +106,37 @@ export async function scanImports(config: ResolvedConfig): Promise<{
106
106
const { plugins = [ ] , ...esbuildOptions } =
107
107
config . optimizeDeps ?. esbuildOptions ?? { }
108
108
109
- await build ( {
110
- absWorkingDir : process . cwd ( ) ,
111
- write : false ,
112
- stdin : {
113
- contents : entries . map ( ( e ) => `import ${ JSON . stringify ( e ) } ` ) . join ( '\n' ) ,
114
- loader : 'js' ,
115
- } ,
116
- bundle : true ,
117
- format : 'esm' ,
118
- logLevel : 'error' ,
119
- plugins : [ ...plugins , plugin ] ,
120
- ...esbuildOptions ,
121
- } )
109
+ try {
110
+ await build ( {
111
+ absWorkingDir : process . cwd ( ) ,
112
+ write : false ,
113
+ stdin : {
114
+ contents : entries . map ( ( e ) => `import ${ JSON . stringify ( e ) } ` ) . join ( '\n' ) ,
115
+ loader : 'js' ,
116
+ } ,
117
+ bundle : true ,
118
+ format : 'esm' ,
119
+ logLevel : 'silent' ,
120
+ plugins : [ ...plugins , plugin ] ,
121
+ ...esbuildOptions ,
122
+ } )
123
+ } catch ( e ) {
124
+ const prependMessage = colors . red ( `\
125
+ Failed to scan for dependencies from entries:
126
+ ${ entries . join ( '\n' ) }
127
+
128
+ ` )
129
+ if ( e . errors ) {
130
+ const msgs = await formatMessages ( e . errors , {
131
+ kind : 'error' ,
132
+ color : true ,
133
+ } )
134
+ e . message = prependMessage + msgs . join ( '\n' )
135
+ } else {
136
+ e . message = prependMessage + e . message
137
+ }
138
+ throw e
139
+ }
122
140
123
141
debug ( `Scan completed in ${ ( performance . now ( ) - start ) . toFixed ( 2 ) } ms:` , deps )
124
142
0 commit comments