Skip to content

Commit e0f2406

Browse files
filipesilvahansl
authored andcommitted
fix(@ngtools/webpack): better AoTPlugin instance error
Currently, having multiple @ngtools/webpack installed yields a cryptic error in typescript: ``` ERROR in ./src/main.ts Module build failed: TypeError: Cannot read property 'newLine' of undefined ``` This happens because a plugin is found, but it isn't an instance of the right class. This makes the loader assume it's being ran without a plugin (just transpiling TS). Then it tries to load a missing tsconfig and the error above is shown. This PR introduced better errors for both the wrong plugin instance and missing tsconfig. ``` ERROR in ./src/main.ts Module build failed: Error: AotPlugin was detected but it was an instance of the wrong class. This likely means you have several @ngtools/webpack packages installed.You can check this with `npm ls @ngtools/webpack`, and then remove the extra copies. at Object.ngcLoader (D:\work\cli\packages\@ngtools\webpack\src\loader.ts:371:19) @ multi ./src/main.ts ``` ``` ERROR in ./src/main.ts Module build failed: Error: @ngtools/webpack is being used as a loader but no `tsConfigPath` option norAotPlugin was detected. You must provide at least one of these. at Object.ngcLoader (D:\work\cli\packages\@ngtools\webpack\src\loader.ts:442:19) @ multi ./src/main.ts ``` See #3781 (comment) and #3781 (comment) for context.
1 parent 60146ac commit e0f2406

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

packages/@ngtools/webpack/src/loader.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,16 @@ export function ngcLoader(this: LoaderContext & { _compilation: any }, source: s
441441
const sourceFileName: string = this.resourcePath;
442442

443443
const plugin = this._compilation._ngToolsWebpackPluginInstance as AotPlugin;
444-
// We must verify that AotPlugin is an instance of the right class.
445-
if (plugin && plugin instanceof AotPlugin) {
444+
if (plugin) {
445+
// We must verify that AotPlugin is an instance of the right class.
446+
// Throw an error if it isn't, that often means multiple @ngtools/webpack installs.
447+
if (!(plugin instanceof AotPlugin)) {
448+
throw new Error('AotPlugin was detected but it was an instance of the wrong class.\n'
449+
+ 'This likely means you have several @ngtools/webpack packages installed. '
450+
+ 'You can check this with `npm ls @ngtools/webpack`, and then remove the extra copies.'
451+
);
452+
}
453+
446454
if (plugin.compilerHost.readFile(sourceFileName) == source) {
447455
// In the case where the source is the same as the one in compilerHost, we don't have
448456
// extra TS loaders and there's no need to do any trickery.
@@ -514,6 +522,13 @@ export function ngcLoader(this: LoaderContext & { _compilation: any }, source: s
514522
} else {
515523
const options = loaderUtils.getOptions(this) || {};
516524
const tsConfigPath = options.tsConfigPath;
525+
526+
if (tsConfigPath === undefined) {
527+
throw new Error('@ngtools/webpack is being used as a loader but no `tsConfigPath` option nor '
528+
+ 'AotPlugin was detected. You must provide at least one of these.'
529+
);
530+
}
531+
517532
const tsConfig = ts.readConfigFile(tsConfigPath, ts.sys.readFile);
518533

519534
if (tsConfig.error) {

0 commit comments

Comments
 (0)