Skip to content

Commit 7fbf6a2

Browse files
clydinjosephperrott
authored andcommitted
refactor(@ngtools/webpack): remove Webpack 4 specific type casting
Webpack 5 contains improved types and exports that reduce the need to perform additional type casting throughout the Angular Webpack Plugin. (cherry picked from commit e8f22ab)
1 parent a11f464 commit 7fbf6a2

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

packages/ngtools/webpack/src/ivy/plugin.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ import { CompilerHost, CompilerOptions, readConfiguration } from '@angular/compi
99
import { NgtscProgram } from '@angular/compiler-cli/src/ngtsc/program';
1010
import { createHash } from 'crypto';
1111
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';
1320
import { NgccProcessor } from '../ngcc_processor';
1421
import { TypeScriptPathsPlugin } from '../paths-plugin';
1522
import { WebpackResourceLoader } from '../resource_loader';
@@ -59,7 +66,7 @@ function initializeNgccProcessor(
5966
tsconfig: string,
6067
): { processor: NgccProcessor; errors: string[]; warnings: string[] } {
6168
const { inputFileSystem, options: webpackOptions } = compiler;
62-
const mainFields = ([] as string[]).concat(...(webpackOptions.resolve?.mainFields || []));
69+
const mainFields = webpackOptions.resolve?.mainFields?.flat() ?? [];
6370

6471
const errors: string[] = [];
6572
const warnings: string[] = [];
@@ -111,8 +118,7 @@ export class AngularWebpackPlugin {
111118
return this.pluginOptions;
112119
}
113120

114-
apply(webpackCompiler: Compiler): void {
115-
const compiler = webpackCompiler as Compiler & { watchMode?: boolean };
121+
apply(compiler: Compiler): void {
116122
// Setup file replacements with webpack
117123
for (const [key, value] of Object.entries(this.pluginOptions.fileReplacements)) {
118124
new NormalModuleReplacementPlugin(
@@ -124,28 +130,21 @@ export class AngularWebpackPlugin {
124130
// Set resolver options
125131
const pathsPlugin = new TypeScriptPathsPlugin();
126132
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-
131133
// When Ivy is enabled we need to add the fields added by NGCC
132134
// to take precedence over the provided mainFields.
133135
// NGCC adds fields in package.json suffixed with '_ivy_ngcc'
134136
// Example: module -> module__ivy_ngcc
135-
resolverFactoryHooks.resolveOptions
137+
compiler.resolverFactory.hooks.resolveOptions
136138
.for('normal')
137-
.tap(PLUGIN_NAME, (resolveOptions: { mainFields: string[]; plugins: unknown[] }) => {
139+
.tap(PLUGIN_NAME, (resolveOptions) => {
138140
const originalMainFields = resolveOptions.mainFields;
139-
const ivyMainFields = originalMainFields.map((f) => `${f}_ivy_ngcc`);
141+
const ivyMainFields = originalMainFields?.flat().map((f) => `${f}_ivy_ngcc`) ?? [];
140142

141-
if (!resolveOptions.plugins) {
142-
resolveOptions.plugins = [];
143-
}
143+
resolveOptions.plugins ??= [];
144144
resolveOptions.plugins.push(pathsPlugin);
145145

146146
// https://github.com/webpack/webpack/issues/11635#issuecomment-707016779
147147
return util.cleverMerge(resolveOptions, { mainFields: [...ivyMainFields, '...'] });
148-
149148
});
150149
});
151150

@@ -356,7 +355,7 @@ export class AngularWebpackPlugin {
356355

357356
if (filesToRebuild.size > 0) {
358357
for (const webpackModule of [...modules]) {
359-
const resource = (webpackModule as Module & { resource?: string }).resource;
358+
const resource = (webpackModule as NormalModule).resource;
360359
if (resource && filesToRebuild.has(normalizePath(resource))) {
361360
await rebuild(webpackModule);
362361
}

0 commit comments

Comments
 (0)