8
8
9
9
import { fork } from 'child_process' ;
10
10
import { dirname , resolve } from 'path' ;
11
-
12
- function resolveNgccFrom ( directory : string ) : string | null {
13
- try {
14
- return require . resolve ( `@angular/compiler-cli/ngcc/main-ngcc.js` , {
15
- paths : [ directory ] ,
16
- } ) ;
17
- } catch {
18
- return null ;
19
- }
20
- }
11
+ import { resolveNgcc , Version } from './version_provider' ;
21
12
22
13
interface Progress {
23
14
report ( msg : string ) : void ;
@@ -29,27 +20,29 @@ interface Progress {
29
20
*/
30
21
export async function resolveAndRunNgcc ( tsconfig : string , progress : Progress ) : Promise < void > {
31
22
const directory = dirname ( tsconfig ) ;
32
- const ngcc = resolveNgccFrom ( directory ) ;
23
+ const ngcc = resolveNgcc ( directory ) ;
33
24
if ( ! ngcc ) {
34
25
throw new Error ( `Failed to resolve ngcc from ${ directory } ` ) ;
35
26
}
36
- const index = ngcc . lastIndexOf ( 'node_modules' ) ;
27
+ const index = ngcc . resolvedPath . lastIndexOf ( 'node_modules' ) ;
37
28
// By default, ngcc assumes the node_modules directory that it needs to process
38
29
// is in the cwd. In our case, we should set cwd to the directory where ngcc
39
30
// is resolved to, not the directory where tsconfig.json is located. See
40
31
// https://github.com/angular/angular/blob/e23fd1f38205410e0ecb601ec73847cea2dea2a8/packages/compiler-cli/ngcc/src/command_line_options.ts#L18-L24
41
- const cwd = index > 0 ? ngcc . slice ( 0 , index ) : process . cwd ( ) ;
42
- const childProcess = fork (
43
- ngcc ,
44
- [
45
- '--tsconfig' ,
46
- tsconfig ,
47
- ] ,
48
- {
49
- cwd : resolve ( cwd ) ,
50
- silent : true , // pipe stderr and stdout so that we can report progress
51
- execArgv : [ ] , // do not inherit flags like --inspect from parent process
52
- } ) ;
32
+ const cwd = index > 0 ? ngcc . resolvedPath . slice ( 0 , index ) : process . cwd ( ) ;
33
+ const args : string [ ] = [
34
+ '--tsconfig' ,
35
+ tsconfig ,
36
+ ] ;
37
+ if ( ngcc . version . greaterThanOrEqual ( new Version ( '11.2.4' ) ) ) {
38
+ // See https://github.com/angular/angular/commit/241784bde8582bcbc00b8a95acdeb3b0d38fbec6
39
+ args . push ( '--typings-only' ) ;
40
+ }
41
+ const childProcess = fork ( ngcc . resolvedPath , args , {
42
+ cwd : resolve ( cwd ) ,
43
+ silent : true , // pipe stderr and stdout so that we can report progress
44
+ execArgv : [ ] , // do not inherit flags like --inspect from parent process
45
+ } ) ;
53
46
54
47
let stderr = '' ;
55
48
childProcess . stderr ?. on ( 'data' , ( data : Buffer ) => {
0 commit comments