Skip to content

Commit 42b9de6

Browse files
clydindgp1130
authored andcommitted
refactor(@angular-devkit/build-angular): use base class for esbuild Angular JIT/AOT compilation
Types and functionality that were used by both the AOT and JIT compilation classes within the esbuild Angular compiler plugin have been extracted into a common base class that both now extend. This removes duplicate code from both classes. (cherry picked from commit 587c4d0)
1 parent 3e0eba1 commit 42b9de6

File tree

5 files changed

+55
-35
lines changed

5 files changed

+55
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import type ng from '@angular/compiler-cli';
10+
import type ts from 'typescript';
11+
import { loadEsmModule } from '../../../utils/load-esm';
12+
import type { AngularHostOptions } from './angular-host';
13+
14+
export interface EmitFileResult {
15+
content?: string;
16+
map?: string;
17+
dependencies: readonly string[];
18+
}
19+
20+
export type FileEmitter = (file: string) => Promise<EmitFileResult | undefined>;
21+
22+
export abstract class AngularCompilation {
23+
static #angularCompilerCliModule?: typeof ng;
24+
25+
static async loadCompilerCli(): Promise<typeof ng> {
26+
// This uses a wrapped dynamic import to load `@angular/compiler-cli` which is ESM.
27+
// Once TypeScript provides support for retaining dynamic imports this workaround can be dropped.
28+
AngularCompilation.#angularCompilerCliModule ??= await loadEsmModule<typeof ng>(
29+
'@angular/compiler-cli',
30+
);
31+
32+
return AngularCompilation.#angularCompilerCliModule;
33+
}
34+
35+
abstract initialize(
36+
rootNames: string[],
37+
compilerOptions: ts.CompilerOptions,
38+
hostOptions: AngularHostOptions,
39+
configurationDiagnostics?: ts.Diagnostic[],
40+
): Promise<{ affectedFiles: ReadonlySet<ts.SourceFile> }>;
41+
42+
abstract collectDiagnostics(): Iterable<ts.Diagnostic>;
43+
44+
abstract createFileEmitter(onAfterEmit?: (sourceFile: ts.SourceFile) => void): FileEmitter;
45+
}

packages/angular_devkit/build_angular/src/builders/browser-esbuild/angular/angular-host.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import ng from '@angular/compiler-cli';
9+
import type ng from '@angular/compiler-cli';
1010
import ts from 'typescript';
1111

1212
export type AngularCompilerOptions = ng.CompilerOptions;

packages/angular_devkit/build_angular/src/builders/browser-esbuild/angular/aot-compilation.ts

+3-22
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import type ng from '@angular/compiler-cli';
1010
import assert from 'node:assert';
1111
import ts from 'typescript';
12-
import { loadEsmModule } from '../../../utils/load-esm';
1312
import { profileAsync, profileSync } from '../profiling';
13+
import { AngularCompilation, FileEmitter } from './angular-compilation';
1414
import {
1515
AngularHostOptions,
1616
createAngularCompilerHost,
@@ -35,36 +35,17 @@ class AngularCompilationState {
3535
}
3636
}
3737

38-
export interface EmitFileResult {
39-
content?: string;
40-
map?: string;
41-
dependencies: readonly string[];
42-
}
43-
export type FileEmitter = (file: string) => Promise<EmitFileResult | undefined>;
44-
45-
export class AotCompilation {
46-
static #angularCompilerCliModule?: typeof ng;
47-
38+
export class AotCompilation extends AngularCompilation {
4839
#state?: AngularCompilationState;
4940

50-
static async loadCompilerCli(): Promise<typeof ng> {
51-
// This uses a wrapped dynamic import to load `@angular/compiler-cli` which is ESM.
52-
// Once TypeScript provides support for retaining dynamic imports this workaround can be dropped.
53-
this.#angularCompilerCliModule ??= await loadEsmModule<typeof ng>('@angular/compiler-cli');
54-
55-
return this.#angularCompilerCliModule;
56-
}
57-
58-
constructor() {}
59-
6041
async initialize(
6142
rootNames: string[],
6243
compilerOptions: ng.CompilerOptions,
6344
hostOptions: AngularHostOptions,
6445
configurationDiagnostics?: ts.Diagnostic[],
6546
): Promise<{ affectedFiles: ReadonlySet<ts.SourceFile> }> {
6647
// Dynamically load the Angular compiler CLI package
67-
const { NgtscProgram, OptimizeFor } = await AotCompilation.loadCompilerCli();
48+
const { NgtscProgram, OptimizeFor } = await AngularCompilation.loadCompilerCli();
6849

6950
// Create Angular compiler host
7051
const host = createAngularCompilerHost(compilerOptions, hostOptions);

packages/angular_devkit/build_angular/src/builders/browser-esbuild/angular/compiler-plugin.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ import {
2929
resetCumulativeDurations,
3030
} from '../profiling';
3131
import { BundleStylesheetOptions, bundleComponentStylesheet } from '../stylesheets';
32+
import { AngularCompilation, FileEmitter } from './angular-compilation';
3233
import { AngularHostOptions } from './angular-host';
33-
import { AotCompilation, FileEmitter } from './aot-compilation';
34+
import { AotCompilation } from './aot-compilation';
3435
import { JitCompilation } from './jit-compilation';
3536
import { setupJitPluginCallbacks } from './jit-plugin-callbacks';
3637

@@ -166,7 +167,7 @@ export function createCompilerPlugin(
166167
const javascriptTransformer = new JavaScriptTransformer(pluginOptions, maxWorkers);
167168

168169
const { GLOBAL_DEFS_FOR_TERSER_WITH_AOT, readConfiguration } =
169-
await AotCompilation.loadCompilerCli();
170+
await AngularCompilation.loadCompilerCli();
170171

171172
// Setup defines based on the values provided by the Angular compiler-cli
172173
build.initialOptions.define ??= {};
@@ -239,7 +240,7 @@ export function createCompilerPlugin(
239240

240241
let stylesheetMetafiles: Metafile[];
241242

242-
let compilation: AotCompilation | undefined;
243+
let compilation: AngularCompilation | undefined;
243244

244245
build.onStart(async () => {
245246
const result: OnStartResult = {

packages/angular_devkit/build_angular/src/builders/browser-esbuild/angular/jit-compilation.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import assert from 'node:assert';
1010
import ts from 'typescript';
1111
import { profileSync } from '../profiling';
12-
import { AngularCompilation } from './angular-compilation';
12+
import { AngularCompilation, FileEmitter } from './angular-compilation';
1313
import { AngularHostOptions, createAngularCompilerHost } from './angular-host';
1414
import { createJitResourceTransformer } from './jit-resource-transformer';
1515

@@ -21,14 +21,7 @@ class JitCompilationState {
2121
) {}
2222
}
2323

24-
export interface EmitFileResult {
25-
content?: string;
26-
map?: string;
27-
dependencies: readonly string[];
28-
}
29-
export type FileEmitter = (file: string) => Promise<EmitFileResult | undefined>;
30-
31-
export class JitCompilation {
24+
export class JitCompilation extends AngularCompilation {
3225
#state?: JitCompilationState;
3326

3427
async initialize(

0 commit comments

Comments
 (0)