@@ -74,6 +74,7 @@ interface FileEmitHistoryItem {
7474export class AngularWebpackPlugin {
7575 private readonly pluginOptions : AngularWebpackPluginOptions ;
7676 private compilerCliModule ?: typeof import ( '@angular/compiler-cli' ) ;
77+ private compilerCliToolingModule ?: typeof import ( '@angular/compiler-cli/private/tooling' ) ;
7778 private watchMode ?: boolean ;
7879 private ngtscNextProgram ?: NgtscProgram ;
7980 private builder ?: ts . EmitAndSemanticDiagnosticsBuilderProgram ;
@@ -107,6 +108,18 @@ export class AngularWebpackPlugin {
107108 return this . compilerCliModule ;
108109 }
109110
111+ private get compilerCliTooling ( ) : typeof import ( '@angular/compiler-cli/private/tooling' ) {
112+ // The compilerCliToolingModule field is guaranteed to be defined during a compilation
113+ // due to the `beforeCompile` hook. Usage of this property accessor prior to the
114+ // hook execution is an implementation error.
115+ assert . ok (
116+ this . compilerCliToolingModule ,
117+ `'@angular/compiler-cli' used prior to Webpack compilation.` ,
118+ ) ;
119+
120+ return this . compilerCliToolingModule ;
121+ }
122+
110123 get options ( ) : AngularWebpackPluginOptions {
111124 return this . pluginOptions ;
112125 }
@@ -688,18 +701,17 @@ export class AngularWebpackPlugin {
688701 }
689702
690703 private async initializeCompilerCli ( ) : Promise < void > {
691- if ( this . compilerCliModule ) {
692- return ;
693- }
694-
695704 // This uses a dynamic import to load `@angular/compiler-cli` which may be ESM.
696705 // CommonJS code can load ESM code via a dynamic import. Unfortunately, TypeScript
697706 // will currently, unconditionally downlevel dynamic import into a require call.
698707 // require calls cannot load ESM code and will result in a runtime error. To workaround
699708 // this, a Function constructor is used to prevent TypeScript from changing the dynamic import.
700709 // Once TypeScript provides support for keeping the dynamic import this workaround can
701710 // be dropped.
702- this . compilerCliModule = await new Function ( `return import('@angular/compiler-cli');` ) ( ) ;
711+ this . compilerCliModule ??= await new Function ( `return import('@angular/compiler-cli');` ) ( ) ;
712+ this . compilerCliToolingModule ??= await new Function (
713+ `return import('@angular/compiler-cli/private/tooling');` ,
714+ ) ( ) ;
703715 }
704716
705717 private async addFileEmitHistory (
0 commit comments