@@ -10,10 +10,12 @@ import { type PartialMessage, formatMessages } from 'esbuild';
1010import { existsSync } from 'node:fs' ;
1111import {
1212 type AngularCompilation ,
13+ DiagnosticModes ,
1314 createAngularCompilation ,
1415} from '../../../tools/angular/compilation' ;
1516import type { ComponentStylesheetBundler } from '../../../tools/esbuild/angular/component-stylesheets' ;
1617import { useTypeChecking } from '../../../utils/environment-options' ;
18+ import { assertIsError } from '../../../utils/error' ;
1719import { toPosixPath } from '../../../utils/path' ;
1820import type { BuilderState } from '../builder-state' ;
1921import type { LibraryCompilationOutput , NormalizedLibraryOptions } from '../types' ;
@@ -70,12 +72,19 @@ export async function compileLibrary(
7072
7173 if ( effectiveModifiedFiles && effectiveModifiedFiles . size > 0 ) {
7274 await compilationInstance . update ?.( effectiveModifiedFiles ) ;
73- buildState . clearFileReferences ( effectiveModifiedFiles ) ;
7475 }
7576
7677 const stylesheetWarnings : PartialMessage [ ] = [ ] ;
7778 const stylesheetErrors : PartialMessage [ ] = [ ] ;
7879 const failedFiles = new Set < string > ( ) ;
80+ const clearedReferenceFiles = new Set < string > ( ) ;
81+ const clearPreviousReferencesOnce = ( targetFile : string ) : void => {
82+ const posixTarget = toPosixPath ( targetFile ) ;
83+ if ( ! clearedReferenceFiles . has ( posixTarget ) ) {
84+ clearedReferenceFiles . add ( posixTarget ) ;
85+ buildState . clearFileReferences ( [ posixTarget ] ) ;
86+ }
87+ } ;
7988
8089 const hostOptions = {
8190 modifiedFiles : effectiveModifiedFiles ,
@@ -86,20 +95,24 @@ export async function compileLibrary(
8695 ) : Promise < string > {
8796 const result = stylesheetFile
8897 ? await stylesheetBundler . bundleFile ( stylesheetFile )
89- : await stylesheetBundler . bundleInline ( data , containingFile ) ;
90-
91- if ( result . referencedFiles ) {
92- buildState . addFileReferences ( containingFile , result . referencedFiles ) ;
93- if ( stylesheetFile ) {
94- buildState . addFileReferences ( stylesheetFile , result . referencedFiles ) ;
95- }
96- }
98+ : await stylesheetBundler . bundleInline (
99+ data ,
100+ containingFile ,
101+ containingFile . endsWith ( '.html' ) ? 'css' : undefined ,
102+ ) ;
97103
98104 if ( result . warnings . length > 0 ) {
99105 stylesheetWarnings . push ( ...result . warnings ) ;
100106 }
101107
102108 if ( result . errors ?. length ) {
109+ if ( result . referencedFiles ) {
110+ buildState . addFileReferences ( containingFile , result . referencedFiles ) ;
111+ if ( stylesheetFile ) {
112+ buildState . addFileReferences ( stylesheetFile , result . referencedFiles ) ;
113+ }
114+ }
115+
103116 stylesheetErrors . push ( ...result . errors ) ;
104117 failedFiles . add ( toPosixPath ( containingFile ) ) ;
105118 if ( stylesheetFile ) {
@@ -109,12 +122,28 @@ export async function compileLibrary(
109122 return '' ;
110123 }
111124
125+ clearPreviousReferencesOnce ( containingFile ) ;
126+ if ( stylesheetFile ) {
127+ clearPreviousReferencesOnce ( stylesheetFile ) ;
128+ }
129+
130+ if ( result . referencedFiles ) {
131+ buildState . addFileReferences ( containingFile , result . referencedFiles ) ;
132+ if ( stylesheetFile ) {
133+ buildState . addFileReferences ( stylesheetFile , result . referencedFiles ) ;
134+ }
135+ }
136+
112137 return result . contents ;
113138 } ,
114139 processWebWorker : ( ) => '' ,
115140 } ;
116141
117- const { referencedFiles, compilerOptions } = await compilationInstance . initialize (
142+ const {
143+ referencedFiles,
144+ compilerOptions,
145+ warnings : initWarnings ,
146+ } = await compilationInstance . initialize (
118147 tsConfigPath ,
119148 hostOptions ,
120149 {
@@ -129,15 +158,12 @@ export async function compileLibrary(
129158 'library' ,
130159 ) ;
131160
161+ if ( initWarnings ?. length ) {
162+ stylesheetWarnings . unshift ( ...initWarnings ) ;
163+ }
164+
132165 const hasStylesheetErrors = stylesheetErrors . length > 0 ;
133166 const emittedFiles = hasStylesheetErrors ? [ ] : await compilationInstance . emitAffectedFiles ( ) ;
134- const diagnosePromise = runDiagnosticsAndFormat (
135- compilationInstance ,
136- stylesheetErrors ,
137- stylesheetWarnings ,
138- colors ,
139- buildState ,
140- ) ;
141167
142168 const esmFiles = cached ?. esmFiles ?? new Map < string , string > ( ) ;
143169 const dtsFiles = cached ?. dtsFiles ?? new Map < string , string > ( ) ;
@@ -179,12 +205,22 @@ export async function compileLibrary(
179205
180206 if ( targetFiles . get ( normalized ) !== contents ) {
181207 targetFiles . set ( normalized , contents ) ;
182- ( isDts ? changedDtsFiles : changedEsmFiles ) . add (
183- isMap ? normalized . slice ( 0 , - 4 ) : normalized ,
184- ) ;
208+ if ( ! isDts ) {
209+ changedEsmFiles . add ( isMap ? normalized . slice ( 0 , - 4 ) : normalized ) ;
210+ } else if ( ! isMap ) {
211+ changedDtsFiles . add ( normalized ) ;
212+ }
185213 }
186214 }
187215
216+ const diagnosePromise = runDiagnosticsAndFormat (
217+ compilationInstance ,
218+ stylesheetErrors ,
219+ stylesheetWarnings ,
220+ colors ,
221+ buildState ,
222+ ) ;
223+
188224 return {
189225 esmFiles,
190226 dtsFiles,
@@ -216,7 +252,7 @@ export async function compileLibrary(
216252 *
217253 * @param compilationInstance The active Angular compilation instance.
218254 * @param stylesheetErrors List of stylesheet bundling errors.
219- * @param stylesheetWarnings List of stylesheet bundling warnings.
255+ * @param stylesheetWarnings List of initialization and stylesheet bundling warnings.
220256 * @param colors Whether diagnostic messages should be formatted with ANSI colors.
221257 * @param buildState The build state whose execution result receives the formatted diagnostics.
222258 */
@@ -231,23 +267,39 @@ async function runDiagnosticsAndFormat(
231267 const errors = [ ...stylesheetErrors ] ;
232268 const warnings : PartialMessage [ ] = [ ] ;
233269
234- if ( errors . length === 0 && useTypeChecking ) {
235- const diagnostics = await compilationInstance . diagnoseFiles ( ) ;
270+ try {
271+ const diagnostics = await compilationInstance . diagnoseFiles (
272+ useTypeChecking ? DiagnosticModes . All : DiagnosticModes . All & ~ DiagnosticModes . Semantic ,
273+ ) ;
236274 if ( diagnostics . errors ?. length ) {
237275 errors . push ( ...diagnostics . errors ) ;
238276 }
239277 if ( diagnostics . warnings ?. length ) {
240278 warnings . push ( ...diagnostics . warnings ) ;
241279 }
280+ } catch ( error ) {
281+ assertIsError ( error ) ;
282+ errors . push ( {
283+ text : 'Angular compilation diagnostics failed.' ,
284+ location : null ,
285+ notes : [ { text : error . stack ?? error . message , location : null } ] ,
286+ } ) ;
242287 }
243288
244289 warnings . push ( ...stylesheetWarnings ) ;
245290
246- if ( errors . length > 0 ) {
247- executionResult . addErrors ( await formatMessages ( errors , { kind : 'error' , color : colors } ) ) ;
248- }
291+ try {
292+ if ( errors . length > 0 ) {
293+ executionResult . addErrors ( await formatMessages ( errors , { kind : 'error' , color : colors } ) ) ;
294+ }
249295
250- if ( warnings . length > 0 ) {
251- executionResult . addWarnings ( await formatMessages ( warnings , { kind : 'warning' , color : colors } ) ) ;
296+ if ( warnings . length > 0 ) {
297+ executionResult . addWarnings (
298+ await formatMessages ( warnings , { kind : 'warning' , color : colors } ) ,
299+ ) ;
300+ }
301+ } catch ( error ) {
302+ assertIsError ( error ) ;
303+ executionResult . addError ( error . message ) ;
252304 }
253305}
0 commit comments