77 */
88
99import { Logger } from '@angular/compiler-cli/ngcc' ;
10+ import { existsSync } from 'fs' ;
11+ import * as path from 'path' ;
1012import * as ts from 'typescript' ;
1113import { InputFileSystem } from 'webpack' ;
1214import { 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
2526export 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
106122class NgccLogger implements Logger {
0 commit comments