Skip to content

Commit 88e3fc0

Browse files
committed
refactor(@angular/build): move compiler-cli loading to TypeScriptCompilation
AngularCompilation serves as the top-level compilation contract used across the build pipeline, including on the main thread via ParallelCompilation. Neither AngularCompilation nor ParallelCompilation requires loading @angular/compiler-cli or reading tsconfig.json configurations. The loadCompilerCli and loadConfiguration methods are now moved to TypeScriptCompilation, which is the base class for in-process TypeScript compilations (AotCompilation and JitCompilation). This completely decouples AngularCompilation from @angular/compiler-cli and ensures that compiler-cli loading and configuration parsing are localized exclusively to the worker compilation hierarchy.
1 parent c35b43c commit 88e3fc0

5 files changed

Lines changed: 44 additions & 41 deletions

File tree

packages/angular/build/src/tools/angular/compilation/angular-compilation.ts

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import type * as ng from '@angular/compiler-cli';
109
import type { PartialMessage } from 'esbuild';
11-
import { profileSync } from '../../esbuild/profiling';
1210
import type { AngularHostOptions } from '../angular-host';
1311
import type { CompilerOptionOverrides } from './compiler-options';
1412

@@ -50,37 +48,6 @@ export enum DiagnosticModes {
5048
}
5149

5250
export abstract class AngularCompilation {
53-
static #angularCompilerCliModule?: typeof ng;
54-
55-
static async loadCompilerCli(): Promise<typeof ng> {
56-
AngularCompilation.#angularCompilerCliModule ??= await import('@angular/compiler-cli');
57-
58-
return AngularCompilation.#angularCompilerCliModule;
59-
}
60-
61-
protected async loadConfiguration(tsconfig: string): Promise<ng.CompilerOptions> {
62-
const { readConfiguration } = await AngularCompilation.loadCompilerCli();
63-
64-
return profileSync('NG_READ_CONFIG', () =>
65-
readConfiguration(tsconfig, {
66-
// Angular specific configuration defaults and overrides to ensure a functioning compilation.
67-
suppressOutputPathCheck: true,
68-
outDir: undefined,
69-
sourceMap: false,
70-
declaration: false,
71-
declarationMap: false,
72-
allowEmptyCodegenFiles: false,
73-
annotationsAs: 'decorators',
74-
enableResourceInlining: false,
75-
supportTestBed: false,
76-
supportJitMode: false,
77-
// Disable removing of comments as TS is quite aggressive with these and can
78-
// remove important annotations, such as /* @__PURE__ */ and comments like /* vite-ignore */.
79-
removeComments: false,
80-
}),
81-
);
82-
}
83-
8451
abstract initialize(
8552
tsconfig: string,
8653
hostOptions: AngularHostOptions,

packages/angular/build/src/tools/angular/compilation/angular-compilation_spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ describe('AngularCompilation', () => {
128128
await compilation.update?.(new Set(['/src/test.ts']));
129129
expect(compilation.getCachedSourceFiles().has('/src/test.ts')).toBeFalse();
130130
});
131+
132+
it('dynamically loads the @angular/compiler-cli module', async () => {
133+
const compilerCli = await TypeScriptCompilation.loadCompilerCli();
134+
expect(compilerCli).toBeDefined();
135+
expect(typeof compilerCli.readConfiguration).toBe('function');
136+
});
131137
});
132138

133139
describe('createAngularCompilation', () => {

packages/angular/build/src/tools/angular/compilation/aot-compilation.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ import { replaceBootstrap } from '../transformers/jit-bootstrap-transformer';
2222
import { lazyRoutesTransformer } from '../transformers/lazy-routes-transformer';
2323
import { createWorkerTransformer } from '../transformers/web-worker-transformer';
2424
import {
25-
AngularCompilation,
26-
AngularCompilationResult,
25+
type AngularCompilationResult,
2726
DiagnosticModes,
28-
EmitFileResult,
27+
type EmitFileResult,
2928
} from './angular-compilation';
3029
import { CompilerOptionOverrides, transformCompilerOptions } from './compiler-options';
3130
import { collectHmrCandidates } from './hmr-candidates';
@@ -69,7 +68,7 @@ export class AotCompilation extends TypeScriptCompilation {
6968
compilerOptionOverrides?: CompilerOptionOverrides,
7069
): Promise<AngularCompilationResult> {
7170
// Dynamically load the Angular compiler CLI package
72-
const { NgtscProgram, OptimizeFor } = await AngularCompilation.loadCompilerCli();
71+
const { NgtscProgram, OptimizeFor } = await TypeScriptCompilation.loadCompilerCli();
7372

7473
// Load the compiler configuration and transform as needed
7574
const {

packages/angular/build/src/tools/angular/compilation/jit-compilation.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ import { createJitResourceTransformer } from '../transformers/jit-resource-trans
1616
import { lazyRoutesTransformer } from '../transformers/lazy-routes-transformer';
1717
import { createWorkerTransformer } from '../transformers/web-worker-transformer';
1818
import {
19-
AngularCompilation,
20-
AngularCompilationResult,
19+
type AngularCompilationResult,
2120
DiagnosticModes,
22-
EmitFileResult,
21+
type EmitFileResult,
2322
} from './angular-compilation';
2423
import { CompilerOptionOverrides, transformCompilerOptions } from './compiler-options';
2524
import { TypeScriptCompilation } from './typescript-compilation';

packages/angular/build/src/tools/angular/compilation/typescript-compilation.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,46 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import type * as ng from '@angular/compiler-cli';
910
import type { PartialMessage } from 'esbuild';
1011
import ts from 'typescript';
1112
import { toPosixPath } from '../../../utils/path';
12-
import { profileAsync } from '../../esbuild/profiling';
13+
import { profileAsync, profileSync } from '../../esbuild/profiling';
1314
import { AngularCompilation, DiagnosticModes } from './angular-compilation';
1415
import { convertTypeScriptDiagnostic } from './diagnostics';
1516

1617
export abstract class TypeScriptCompilation extends AngularCompilation {
18+
static #angularCompilerCliModule?: typeof ng;
19+
20+
static async loadCompilerCli(): Promise<typeof ng> {
21+
TypeScriptCompilation.#angularCompilerCliModule ??= await import('@angular/compiler-cli');
22+
23+
return TypeScriptCompilation.#angularCompilerCliModule;
24+
}
25+
26+
protected async loadConfiguration(tsconfig: string): Promise<ng.ParsedConfiguration> {
27+
const { readConfiguration } = await TypeScriptCompilation.loadCompilerCli();
28+
29+
return profileSync('NG_READ_CONFIG', () =>
30+
readConfiguration(tsconfig, {
31+
// Angular specific configuration defaults and overrides to ensure a functioning compilation.
32+
suppressOutputPathCheck: true,
33+
outDir: undefined,
34+
sourceMap: false,
35+
declaration: false,
36+
declarationMap: false,
37+
allowEmptyCodegenFiles: false,
38+
annotationsAs: 'decorators',
39+
enableResourceInlining: false,
40+
supportTestBed: false,
41+
supportJitMode: false,
42+
// Disable removing of comments as TS is quite aggressive with these and can
43+
// remove important annotations, such as /* @__PURE__ */ and comments like /* vite-ignore */.
44+
removeComments: false,
45+
}),
46+
);
47+
}
48+
1749
protected readonly sourceFiles = new Map<string, ts.SourceFile>();
1850

1951
protected invalidateFiles(files: Iterable<string>): void {

0 commit comments

Comments
 (0)