From 263626280ff3e75db66d16ff5fc9781805fc45ca Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Fri, 10 Jun 2022 16:58:47 -0400 Subject: [PATCH] refactor(@ngtools/webpack): assert catch clause variable type before usage Prepares the `@ngtools/webpack` package for the eventual change of enabling the TypeScript `useUnknownInCatchVariables` option. This option provides additional code safety by ensuring that the catch clause variable is the proper type before attempting to access its properties. Similar changes will be needed in the other packages in the repository prior to enabling `useUnknownInCatchVariables`. --- packages/ngtools/webpack/src/resource_loader.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/ngtools/webpack/src/resource_loader.ts b/packages/ngtools/webpack/src/resource_loader.ts index 5219fd3dc8..17bd64b64e 100644 --- a/packages/ngtools/webpack/src/resource_loader.ts +++ b/packages/ngtools/webpack/src/resource_loader.ts @@ -6,6 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ +import assert from 'assert'; import * as path from 'path'; import * as vm from 'vm'; import type { Asset, Compilation } from 'webpack'; @@ -115,6 +116,7 @@ export class WebpackResourceLoader { const { EntryPlugin, NormalModule, + WebpackError, library, node, sources, @@ -208,8 +210,9 @@ export class WebpackResourceLoader { compilation.assets[outputFilePath] = new sources.RawSource(output); } } catch (error) { + assert(error instanceof Error, 'catch clause variable is not an Error instance'); // Use compilation errors, as otherwise webpack will choke - compilation.errors.push(error); + compilation.errors.push(new WebpackError(error.message)); } }); },