@@ -69,7 +69,7 @@ export function compile(
6969 const tsProject = gulpTS . createProject ( absTsConfigPath , tsCompilerOptions ) ;
7070 const hasAlias = Object . keys ( tsProject . config . compilerOptions ?. paths || { } ) . length > 0 ;
7171
72- return new Promise ( resolve => {
72+ return new Promise ( ( resolve , reject ) => {
7373 let sourcesStream = gulp . src ( sources ) ;
7474 for ( const [ key , value ] of Object . entries ( envs ) ) {
7575 sourcesStream = sourcesStream
@@ -95,20 +95,39 @@ export function compile(
9595 )
9696 )
9797 . pipe ( gulpIF ( sourceMap , gulpSourceMaps . init ( ) ) )
98- . pipe ( tsProject ( gulpTS . reporter . longReporter ( ) ) )
99- . on ( 'error' , function _err ( err ) {
100- // 确保错误信息完整输出
98+ . pipe ( tsProject ( gulpTS . reporter . longReporter ( ) ) ) ;
99+
100+ // 监听编译错误,输出完整的 TypeScript 诊断信息
101+ tsResult . on ( 'error' , function _err ( err ) {
102+ // 获取完整的 TypeScript 诊断信息
103+ const diagnostics = ( tsResult as any ) . diagnostics || [ ] ;
104+ if ( diagnostics . length > 0 ) {
101105 // eslint-disable-next-line no-console
102- console . error ( 'TypeScript compilation failed: ' ) ;
106+ console . error ( '\n=== TypeScript Compilation Errors === ' ) ;
103107 // eslint-disable-next-line no-console
104- console . error ( err ) ;
105- if ( noEmitOnError ) {
106- process . exit ( 1 ) ;
107- } else {
108- // @ts -ignore
109- ( this as NodeJS . ReadWriteStream ) . emit ( 'end' ) ;
110- }
111- } ) ;
108+ console . error ( ts . formatDiagnostics ( diagnostics , ts . createCompilerHost ( ts . getDefaultCompilerOptions ( ) ) ) ) ;
109+ }
110+ // 输出原始错误信息
111+ // eslint-disable-next-line no-console
112+ console . error ( '\n=== Build Error ===' ) ;
113+ // eslint-disable-next-line no-console
114+ console . error ( err ) ;
115+ // 输出完整的错误堆栈
116+ if ( err && ( err as Error ) . stack ) {
117+ // eslint-disable-next-line no-console
118+ console . error ( '\n=== Error Stack ===' ) ;
119+ // eslint-disable-next-line no-console
120+ console . error ( ( err as Error ) . stack ) ;
121+ }
122+
123+ if ( noEmitOnError ) {
124+ reject ( err ) ;
125+ process . exit ( 1 ) ;
126+ } else {
127+ // @ts -ignore
128+ ( this as NodeJS . ReadWriteStream ) . emit ( 'end' ) ;
129+ }
130+ } ) ;
112131
113132 merge ( [
114133 tsResult . js
@@ -159,8 +178,32 @@ export function compile(
159178 . pipe ( gulp . dest ( outputPath ) ) ,
160179 tsResult . js . pipe ( gulpIF ( sourceMap , gulpSourceMaps . write ( '.' ) ) ) . pipe ( gulp . dest ( outputPath ) ) ,
161180 tsResult . dts . pipe ( gulp . dest ( outputPath ) )
162- ] ) . on ( 'finish' , ( ) => {
163- resolve ( undefined ) ;
164- } ) ;
181+ ] )
182+ . on ( 'finish' , ( ) => {
183+ // 检查是否有编译错误(在 finish 时检查,因为有些错误可能不会触发 error 事件)
184+ const diagnostics = ( tsResult as any ) . diagnostics || [ ] ;
185+ if ( diagnostics . length > 0 && noEmitOnError ) {
186+ // eslint-disable-next-line no-console
187+ console . error ( '\n=== TypeScript Compilation Errors (detected on finish) ===' ) ;
188+ // eslint-disable-next-line no-console
189+ console . error ( ts . formatDiagnostics ( diagnostics , ts . createCompilerHost ( ts . getDefaultCompilerOptions ( ) ) ) ) ;
190+ const error = new Error ( 'TypeScript compilation failed with errors' ) ;
191+ reject ( error ) ;
192+ process . exit ( 1 ) ;
193+ } else {
194+ resolve ( undefined ) ;
195+ }
196+ } )
197+ . on ( 'error' , err => {
198+ // eslint-disable-next-line no-console
199+ console . error ( '\n=== Merge Stream Error ===' ) ;
200+ // eslint-disable-next-line no-console
201+ console . error ( err ) ;
202+ if ( err && ( err as Error ) . stack ) {
203+ // eslint-disable-next-line no-console
204+ console . error ( ( err as Error ) . stack ) ;
205+ }
206+ reject ( err ) ;
207+ } ) ;
165208 } ) ;
166209}
0 commit comments