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`.
This commit is contained in:
Charles Lyding 2022-06-10 16:58:47 -04:00 committed by Alan Agius
parent 9b1a36a9d6
commit 263626280f

View File

@ -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));
}
});
},