11import chalk from "chalk" ;
2- import { Plugin } from "esbuild" ;
2+ import { PartialMessage , Plugin } from "esbuild" ;
33import { existsSync , lstatSync } from "fs" ;
44import { resolve } from "path" ;
55import ts from "typescript" ;
@@ -38,14 +38,22 @@ export const dtsPlugin = (opts: DTSPluginOpts = {}) =>
3838 build . onLoad ( { filter : / ( \. t s x | \. t s ) $ / } , async ( args ) => {
3939 inputFiles . push ( args . path ) ;
4040
41+ const errors : PartialMessage [ ] = [ ] ;
42+
4143 compilerHost . getSourceFile (
4244 args . path ,
4345 compilerOptions . target ?? ts . ScriptTarget . Latest ,
44- ( m ) => console . log ( m ) ,
46+ ( m ) => {
47+ errors . push ( {
48+ detail : m ,
49+ } ) ;
50+ } ,
4551 true ,
4652 ) ;
4753
48- return { } ;
54+ return {
55+ errors,
56+ } ;
4957 } ) ;
5058
5159 build . onEnd ( ( ) => {
@@ -65,6 +73,41 @@ export const dtsPlugin = (opts: DTSPluginOpts = {}) =>
6573 ) ;
6674 }
6775
76+ const diagnostics = ts
77+ . getPreEmitDiagnostics ( compilerProgram as ts . Program )
78+ . map (
79+ ( d ) =>
80+ ( {
81+ text :
82+ typeof d . messageText === "string"
83+ ? d . messageText
84+ : d . messageText . messageText ,
85+ detail : d ,
86+ location : {
87+ file : d . file ?. fileName ,
88+ namespace : "file" ,
89+ } ,
90+ category : d . category ,
91+ } ) satisfies PartialMessage & {
92+ category : ts . DiagnosticCategory ;
93+ } ,
94+ ) ;
95+
96+ const errors = diagnostics
97+ . filter ( ( d ) => d . category === ts . DiagnosticCategory . Error )
98+ . map ( ( { category : _ , ...message } ) => message ) ;
99+
100+ const warnings = diagnostics
101+ . filter ( ( d ) => d . category === ts . DiagnosticCategory . Warning )
102+ . map ( ( { category : _ , ...message } ) => message ) ;
103+
104+ if ( errors . length > 0 ) {
105+ return {
106+ errors,
107+ warnings,
108+ } ;
109+ }
110+
68111 const startTime = Date . now ( ) ;
69112 const emitResult = compilerProgram . emit ( ) ;
70113
@@ -100,6 +143,10 @@ export const dtsPlugin = (opts: DTSPluginOpts = {}) =>
100143 Date . now ( ) - startTime
101144 } ms}`,
102145 ) ;
146+
147+ return {
148+ warnings,
149+ } ;
103150 } ) ;
104151 } ,
105152 } ) as Plugin ;
0 commit comments