Skip to content

Commit 3cc6d6d

Browse files
committed
refactor: run ngcc --typings-only if version >= 11.2.4
ngcc v11.2.4 and above supports `--typings-only` mode, which updates the TypeScript declaration files but keep Javascript files unmodified. This will speed up ngcc operation in the extension.
1 parent 6f924b2 commit 3cc6d6d

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

server/src/ngcc.ts

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,7 @@
88

99
import {fork} from 'child_process';
1010
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';
2112

2213
interface Progress {
2314
report(msg: string): void;
@@ -29,27 +20,29 @@ interface Progress {
2920
*/
3021
export async function resolveAndRunNgcc(tsconfig: string, progress: Progress): Promise<void> {
3122
const directory = dirname(tsconfig);
32-
const ngcc = resolveNgccFrom(directory);
23+
const ngcc = resolveNgcc(directory);
3324
if (!ngcc) {
3425
throw new Error(`Failed to resolve ngcc from ${directory}`);
3526
}
36-
const index = ngcc.lastIndexOf('node_modules');
27+
const index = ngcc.resolvedPath.lastIndexOf('node_modules');
3728
// By default, ngcc assumes the node_modules directory that it needs to process
3829
// is in the cwd. In our case, we should set cwd to the directory where ngcc
3930
// is resolved to, not the directory where tsconfig.json is located. See
4031
// 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+
});
5346

5447
let stderr = '';
5548
childProcess.stderr?.on('data', (data: Buffer) => {

server/src/version_provider.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ export function resolveNgLangSvc(probeLocations: string[], ivy: boolean): NodeMo
124124
return resolveWithMinVersion(packageName, MIN_NG_VERSION, probeLocations, NGLANGSVC);
125125
}
126126

127+
export function resolveNgcc(directory: string): NodeModule|undefined {
128+
return resolve('@angular/compiler-cli/ngcc/main-ngcc.js', directory, '@angular/compiler-cli');
129+
}
130+
127131
/**
128132
* Converts the specified string `a` to non-negative integer.
129133
* Returns -1 if the result is NaN.

0 commit comments

Comments
 (0)