7
7
*/
8
8
9
9
import { Logger } from '@angular/compiler-cli/ngcc' ;
10
+ import { existsSync } from 'fs' ;
11
+ import * as path from 'path' ;
10
12
import * as ts from 'typescript' ;
11
13
import { InputFileSystem } from 'webpack' ;
12
14
import { time , timeEnd } from './benchmark' ;
13
- import { workaroundResolve } from './utils' ;
14
15
15
16
// We cannot create a plugin for this, because NGTSC requires addition type
16
17
// information which ngcc creates when processing a package which was compiled with NGC.
@@ -24,17 +25,19 @@ import { workaroundResolve } from './utils';
24
25
25
26
export class NgccProcessor {
26
27
private _processedModules = new Set < string > ( ) ;
27
-
28
28
private _logger : NgccLogger ;
29
+ private _nodeModulesDirectory : string ;
29
30
30
31
constructor (
31
32
private readonly ngcc : typeof import ( '@angular/compiler-cli/ngcc' ) ,
32
33
private readonly propertiesToConsider : string [ ] ,
33
34
private readonly inputFileSystem : InputFileSystem ,
34
35
private readonly compilationWarnings : ( Error | string ) [ ] ,
35
36
private readonly compilationErrors : ( Error | string ) [ ] ,
37
+ private readonly basePath : string ,
36
38
) {
37
39
this . _logger = new NgccLogger ( this . compilationWarnings , this . compilationErrors ) ;
40
+ this . _nodeModulesDirectory = this . findNodeModulesDirectory ( this . basePath ) ;
38
41
}
39
42
40
43
processModule (
@@ -57,13 +60,12 @@ export class NgccProcessor {
57
60
58
61
return ;
59
62
}
60
- const normalizedJsonPath = workaroundResolve ( packageJsonPath ) ;
61
63
62
64
const timeLabel = `NgccProcessor.processModule.ngcc.process+${ moduleName } ` ;
63
65
time ( timeLabel ) ;
64
66
this . ngcc . process ( {
65
- basePath : normalizedJsonPath . substring ( 0 , normalizedJsonPath . indexOf ( moduleName ) ) ,
66
- targetEntryPointPath : moduleName ,
67
+ basePath : this . _nodeModulesDirectory ,
68
+ targetEntryPointPath : path . dirname ( packageJsonPath ) ,
67
69
propertiesToConsider : this . propertiesToConsider ,
68
70
compileAllFormats : false ,
69
71
createNewEntryPointFormats : true ,
@@ -101,6 +103,20 @@ export class NgccProcessor {
101
103
return undefined ;
102
104
}
103
105
}
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
+ }
104
120
}
105
121
106
122
class NgccLogger implements Logger {
0 commit comments