Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import type ng from '@angular/compiler-cli';
import type ts from 'typescript';
import { loadEsmModule } from '../../../utils/load-esm';
import type { AngularHostOptions } from './angular-host';

export interface EmitFileResult {
content?: string;
map?: string;
dependencies: readonly string[];
}

export type FileEmitter = (file: string) => Promise<EmitFileResult | undefined>;

export abstract class AngularCompilation {
static #angularCompilerCliModule?: typeof ng;

static async loadCompilerCli(): Promise<typeof ng> {
// This uses a wrapped dynamic import to load `@angular/compiler-cli` which is ESM.
// Once TypeScript provides support for retaining dynamic imports this workaround can be dropped.
AngularCompilation.#angularCompilerCliModule ??= await loadEsmModule<typeof ng>(
'@angular/compiler-cli',
);

return AngularCompilation.#angularCompilerCliModule;
}

abstract initialize(
rootNames: string[],
compilerOptions: ts.CompilerOptions,
hostOptions: AngularHostOptions,
configurationDiagnostics?: ts.Diagnostic[],
): Promise<{ affectedFiles: ReadonlySet<ts.SourceFile> }>;

abstract collectDiagnostics(): Iterable<ts.Diagnostic>;

abstract createFileEmitter(onAfterEmit?: (sourceFile: ts.SourceFile) => void): FileEmitter;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import ng from '@angular/compiler-cli';
import type ng from '@angular/compiler-cli';
import ts from 'typescript';

export type AngularCompilerOptions = ng.CompilerOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import type ng from '@angular/compiler-cli';
import assert from 'node:assert';
import ts from 'typescript';
import { loadEsmModule } from '../../utils/load-esm';
import { profileAsync, profileSync } from '../profiling';
import { AngularCompilation, FileEmitter } from './angular-compilation';
import {
AngularHostOptions,
createAngularCompilerHost,
ensureSourceFileVersions,
} from './angular-host';
import { profileAsync, profileSync } from './profiling';

// Temporary deep import for transformer support
// TODO: Move these to a private exports location or move the implementation into this package.
Expand All @@ -35,28 +35,9 @@ class AngularCompilationState {
}
}

export interface EmitFileResult {
content?: string;
map?: string;
dependencies: readonly string[];
}
export type FileEmitter = (file: string) => Promise<EmitFileResult | undefined>;

export class AngularCompilation {
static #angularCompilerCliModule?: typeof ng;

export class AotCompilation extends AngularCompilation {
#state?: AngularCompilationState;

static async loadCompilerCli(): Promise<typeof ng> {
// This uses a wrapped dynamic import to load `@angular/compiler-cli` which is ESM.
// Once TypeScript provides support for retaining dynamic imports this workaround can be dropped.
this.#angularCompilerCliModule ??= await loadEsmModule<typeof ng>('@angular/compiler-cli');

return this.#angularCompilerCliModule;
}

constructor() {}

async initialize(
rootNames: string[],
compilerOptions: ng.CompilerOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ import { platform } from 'node:os';
import * as path from 'node:path';
import { pathToFileURL } from 'node:url';
import ts from 'typescript';
import { maxWorkers } from '../../utils/environment-options';
import { JitCompilation } from './angular/jit-compilation';
import { setupJitPluginCallbacks } from './angular/jit-plugin-callbacks';
import { AngularCompilation, FileEmitter } from './angular-compilation';
import { AngularHostOptions } from './angular-host';
import { JavaScriptTransformer } from './javascript-transformer';
import { maxWorkers } from '../../../utils/environment-options';
import { JavaScriptTransformer } from '../javascript-transformer';
import {
logCumulativeDurations,
profileAsync,
profileSync,
resetCumulativeDurations,
} from './profiling';
import { BundleStylesheetOptions, bundleComponentStylesheet } from './stylesheets';
} from '../profiling';
import { BundleStylesheetOptions, bundleComponentStylesheet } from '../stylesheets';
import { AngularCompilation, FileEmitter } from './angular-compilation';
import { AngularHostOptions } from './angular-host';
import { AotCompilation } from './aot-compilation';
import { JitCompilation } from './jit-compilation';
import { setupJitPluginCallbacks } from './jit-plugin-callbacks';

/**
* Converts TypeScript Diagnostic related information into an esbuild compatible note object.
Expand Down Expand Up @@ -291,7 +292,7 @@ export function createCompilerPlugin(
if (pluginOptions.jit) {
compilation ??= new JitCompilation();
} else {
compilation ??= new AngularCompilation();
compilation ??= new AotCompilation();
}

// Initialize the Angular compilation for the current build.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

import assert from 'node:assert';
import ts from 'typescript';
import { AngularCompilation } from '../angular-compilation';
import { AngularHostOptions, createAngularCompilerHost } from '../angular-host';
import { profileSync } from '../profiling';
import { AngularCompilation, FileEmitter } from './angular-compilation';
import { AngularHostOptions, createAngularCompilerHost } from './angular-host';
import { createJitResourceTransformer } from './jit-resource-transformer';

class JitCompilationState {
Expand All @@ -21,14 +21,7 @@ class JitCompilationState {
) {}
}

export interface EmitFileResult {
content?: string;
map?: string;
dependencies: readonly string[];
}
export type FileEmitter = (file: string) => Promise<EmitFileResult | undefined>;

export class JitCompilation {
export class JitCompilation extends AngularCompilation {
#state?: JitCompilationState;

async initialize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import { augmentAppWithServiceWorkerEsbuild } from '../../utils/service-worker';
import { Spinner } from '../../utils/spinner';
import { getSupportedBrowsers } from '../../utils/supported-browsers';
import { BundleStats, generateBuildStatsTable } from '../../webpack/utils/stats';
import { SourceFileCache, createCompilerPlugin } from './angular/compiler-plugin';
import { checkCommonJSModules } from './commonjs-checker';
import { SourceFileCache, createCompilerPlugin } from './compiler-plugin';
import { BundlerContext, logMessages } from './esbuild';
import { logExperimentalWarnings } from './experimental-warnings';
import { createGlobalScriptsBundleOptions } from './global-scripts';
Expand Down