@@ -9,7 +9,14 @@ import { CompilerHost, CompilerOptions, readConfiguration } from '@angular/compi
9
9
import { NgtscProgram } from '@angular/compiler-cli/src/ngtsc/program' ;
10
10
import { createHash } from 'crypto' ;
11
11
import * as ts from 'typescript' ;
12
- import { Compilation , Compiler , Module , NormalModuleReplacementPlugin , util } from 'webpack' ;
12
+ import {
13
+ Compilation ,
14
+ Compiler ,
15
+ Module ,
16
+ NormalModule ,
17
+ NormalModuleReplacementPlugin ,
18
+ util ,
19
+ } from 'webpack' ;
13
20
import { NgccProcessor } from '../ngcc_processor' ;
14
21
import { TypeScriptPathsPlugin } from '../paths-plugin' ;
15
22
import { WebpackResourceLoader } from '../resource_loader' ;
@@ -59,7 +66,7 @@ function initializeNgccProcessor(
59
66
tsconfig : string ,
60
67
) : { processor : NgccProcessor ; errors : string [ ] ; warnings : string [ ] } {
61
68
const { inputFileSystem, options : webpackOptions } = compiler ;
62
- const mainFields = ( [ ] as string [ ] ) . concat ( ... ( webpackOptions . resolve ?. mainFields || [ ] ) ) ;
69
+ const mainFields = webpackOptions . resolve ?. mainFields ?. flat ( ) ?? [ ] ;
63
70
64
71
const errors : string [ ] = [ ] ;
65
72
const warnings : string [ ] = [ ] ;
@@ -111,8 +118,7 @@ export class AngularWebpackPlugin {
111
118
return this . pluginOptions ;
112
119
}
113
120
114
- apply ( webpackCompiler : Compiler ) : void {
115
- const compiler = webpackCompiler as Compiler & { watchMode ?: boolean } ;
121
+ apply ( compiler : Compiler ) : void {
116
122
// Setup file replacements with webpack
117
123
for ( const [ key , value ] of Object . entries ( this . pluginOptions . fileReplacements ) ) {
118
124
new NormalModuleReplacementPlugin (
@@ -124,28 +130,21 @@ export class AngularWebpackPlugin {
124
130
// Set resolver options
125
131
const pathsPlugin = new TypeScriptPathsPlugin ( ) ;
126
132
compiler . hooks . afterResolvers . tap ( 'angular-compiler' , ( compiler ) => {
127
- // 'resolverFactory' is not present in the Webpack typings
128
- // tslint:disable-next-line: no-any
129
- const resolverFactoryHooks = ( compiler as any ) . resolverFactory . hooks ;
130
-
131
133
// When Ivy is enabled we need to add the fields added by NGCC
132
134
// to take precedence over the provided mainFields.
133
135
// NGCC adds fields in package.json suffixed with '_ivy_ngcc'
134
136
// Example: module -> module__ivy_ngcc
135
- resolverFactoryHooks . resolveOptions
137
+ compiler . resolverFactory . hooks . resolveOptions
136
138
. for ( 'normal' )
137
- . tap ( PLUGIN_NAME , ( resolveOptions : { mainFields : string [ ] ; plugins : unknown [ ] } ) => {
139
+ . tap ( PLUGIN_NAME , ( resolveOptions ) => {
138
140
const originalMainFields = resolveOptions . mainFields ;
139
- const ivyMainFields = originalMainFields . map ( ( f ) => `${ f } _ivy_ngcc` ) ;
141
+ const ivyMainFields = originalMainFields ?. flat ( ) . map ( ( f ) => `${ f } _ivy_ngcc` ) ?? [ ] ;
140
142
141
- if ( ! resolveOptions . plugins ) {
142
- resolveOptions . plugins = [ ] ;
143
- }
143
+ resolveOptions . plugins ??= [ ] ;
144
144
resolveOptions . plugins . push ( pathsPlugin ) ;
145
145
146
146
// https://github.com/webpack/webpack/issues/11635#issuecomment-707016779
147
147
return util . cleverMerge ( resolveOptions , { mainFields : [ ...ivyMainFields , '...' ] } ) ;
148
-
149
148
} ) ;
150
149
} ) ;
151
150
@@ -356,7 +355,7 @@ export class AngularWebpackPlugin {
356
355
357
356
if ( filesToRebuild . size > 0 ) {
358
357
for ( const webpackModule of [ ...modules ] ) {
359
- const resource = ( webpackModule as Module & { resource ?: string } ) . resource ;
358
+ const resource = ( webpackModule as NormalModule ) . resource ;
360
359
if ( resource && filesToRebuild . has ( normalizePath ( resource ) ) ) {
361
360
await rebuild ( webpackModule ) ;
362
361
}
0 commit comments