Skip to content

Commit 4263cc6

Browse files
alan-agius4alexeagle
authored andcommitted
fix(@ngtools/webpack): fixes ngcc error when project name is the same or partially the same as a module name
FIxes #14317
1 parent 8e13c95 commit 4263cc6

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

packages/ngtools/webpack/src/angular_compiler_plugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ export class AngularCompilerPlugin {
674674
compilerWithFileSystems.inputFileSystem,
675675
this._warnings,
676676
this._errors,
677+
this._basePath,
677678
);
678679
}
679680
}

packages/ngtools/webpack/src/ngcc_processor.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
*/
88

99
import { Logger } from '@angular/compiler-cli/ngcc';
10+
import { existsSync } from 'fs';
11+
import * as path from 'path';
1012
import * as ts from 'typescript';
1113
import { InputFileSystem } from 'webpack';
1214
import { time, timeEnd } from './benchmark';
13-
import { workaroundResolve } from './utils';
1415

1516
// We cannot create a plugin for this, because NGTSC requires addition type
1617
// information which ngcc creates when processing a package which was compiled with NGC.
@@ -24,17 +25,19 @@ import { workaroundResolve } from './utils';
2425

2526
export class NgccProcessor {
2627
private _processedModules = new Set<string>();
27-
2828
private _logger: NgccLogger;
29+
private _nodeModulesDirectory: string;
2930

3031
constructor(
3132
private readonly ngcc: typeof import('@angular/compiler-cli/ngcc'),
3233
private readonly propertiesToConsider: string[],
3334
private readonly inputFileSystem: InputFileSystem,
3435
private readonly compilationWarnings: (Error | string)[],
3536
private readonly compilationErrors: (Error | string)[],
37+
private readonly basePath: string,
3638
) {
3739
this._logger = new NgccLogger(this.compilationWarnings, this.compilationErrors);
40+
this._nodeModulesDirectory = this.findNodeModulesDirectory(this.basePath);
3841
}
3942

4043
processModule(
@@ -57,13 +60,12 @@ export class NgccProcessor {
5760

5861
return;
5962
}
60-
const normalizedJsonPath = workaroundResolve(packageJsonPath);
6163

6264
const timeLabel = `NgccProcessor.processModule.ngcc.process+${moduleName}`;
6365
time(timeLabel);
6466
this.ngcc.process({
65-
basePath: normalizedJsonPath.substring(0, normalizedJsonPath.indexOf(moduleName)),
66-
targetEntryPointPath: moduleName,
67+
basePath: this._nodeModulesDirectory,
68+
targetEntryPointPath: path.dirname(packageJsonPath),
6769
propertiesToConsider: this.propertiesToConsider,
6870
compileAllFormats: false,
6971
createNewEntryPointFormats: true,
@@ -101,6 +103,20 @@ export class NgccProcessor {
101103
return undefined;
102104
}
103105
}
106+
107+
private findNodeModulesDirectory(startPoint: string): string {
108+
let current = startPoint;
109+
while (path.dirname(current) !== current) {
110+
const nodePath = path.join(current, 'node_modules');
111+
if (existsSync(nodePath)) {
112+
return nodePath;
113+
}
114+
115+
current = path.dirname(current);
116+
}
117+
118+
throw new Error(`Cannot locate the 'node_modules' directory.`);
119+
}
104120
}
105121

106122
class NgccLogger implements Logger {

0 commit comments

Comments
 (0)