Skip to content

Commit

Permalink
fix(@ngtools/webpack): better AoTPlugin instance error
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
filipesilva authored and hansl committed Jul 12, 2017
1 parent 60146ac commit e0f2406
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions packages/@ngtools/webpack/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,16 @@ export function ngcLoader(this: LoaderContext & { _compilation: any }, source: s
const sourceFileName: string = this.resourcePath;

const plugin = this._compilation._ngToolsWebpackPluginInstance as AotPlugin;
// We must verify that AotPlugin is an instance of the right class.
if (plugin && plugin instanceof AotPlugin) {
if (plugin) {
// We must verify that AotPlugin is an instance of the right class.
// Throw an error if it isn't, that often means multiple @ngtools/webpack installs.
if (!(plugin instanceof AotPlugin)) {
throw new Error('AotPlugin was detected but it was an instance of the wrong class.\n'
+ '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.'
);
}

if (plugin.compilerHost.readFile(sourceFileName) == source) {
// In the case where the source is the same as the one in compilerHost, we don't have
// extra TS loaders and there's no need to do any trickery.
Expand Down Expand Up @@ -514,6 +522,13 @@ export function ngcLoader(this: LoaderContext & { _compilation: any }, source: s
} else {
const options = loaderUtils.getOptions(this) || {};
const tsConfigPath = options.tsConfigPath;

if (tsConfigPath === undefined) {
throw new Error('@ngtools/webpack is being used as a loader but no `tsConfigPath` option nor '
+ 'AotPlugin was detected. You must provide at least one of these.'
);
}

const tsConfig = ts.readConfigFile(tsConfigPath, ts.sys.readFile);

if (tsConfig.error) {
Expand Down

0 comments on commit e0f2406

Please sign in to comment.