Skip to content
Draft
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
Expand Up @@ -8,7 +8,10 @@

import { BuilderContext } from '@angular-devkit/architect';
import { createAngularCompilation } from '../../tools/angular/compilation';
import { AngularCompilationContext } from '../../tools/esbuild/angular/compilation-state';
import {
AngularCompilationContext,
PrimaryCompilationContext,
} from '../../tools/esbuild/angular/compilation-state';
import { SourceFileCache } from '../../tools/esbuild/angular/source-file-cache';
import { generateBudgetStats } from '../../tools/esbuild/budget-stats';
import { BundleContextResult, BundlerContext } from '../../tools/esbuild/bundler-context';
Expand Down Expand Up @@ -125,7 +128,7 @@ export async function executeBuild(
!!options.jit,
!options.serverEntryPoint,
);
angularCompilationContext = new AngularCompilationContext(angularCompilation);
angularCompilationContext = new PrimaryCompilationContext(angularCompilation);
bundlerContexts = setupBundlerContexts(
options,
target,
Expand Down
5 changes: 3 additions & 2 deletions packages/angular/build/src/private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
* their existence may change in any future version.
*/

import { NoopCompilation, createAngularCompilation } from './tools/angular/compilation';
import { createAngularCompilation } from './tools/angular/compilation';
import { SecondaryCompilationContext } from './tools/esbuild/angular/compilation-state';
import {
CompilerPluginOptions,
createCompilerPlugin as internalCreateCompilerPlugin,
Expand Down Expand Up @@ -52,7 +53,7 @@ export function createCompilerPlugin(
return internalCreateCompilerPlugin(
pluginOptions,
pluginOptions.noopTypeScriptCompilation
? new NoopCompilation()
? new SecondaryCompilationContext()
: () => createAngularCompilation(!!pluginOptions.jit, !!pluginOptions.browserOnlyBuild),
new ComponentStylesheetBundler(
styleOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
* found in the LICENSE file at https://angular.dev/license
*/

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

Expand Down Expand Up @@ -50,37 +48,6 @@ export enum DiagnosticModes {
}

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

static async loadCompilerCli(): Promise<typeof ng> {
AngularCompilation.#angularCompilerCliModule ??= await import('@angular/compiler-cli');

return AngularCompilation.#angularCompilerCliModule;
}

protected async loadConfiguration(tsconfig: string): Promise<ng.CompilerOptions> {
const { readConfiguration } = await AngularCompilation.loadCompilerCli();

return profileSync('NG_READ_CONFIG', () =>
readConfiguration(tsconfig, {
// Angular specific configuration defaults and overrides to ensure a functioning compilation.
suppressOutputPathCheck: true,
outDir: undefined,
sourceMap: false,
declaration: false,
declarationMap: false,
allowEmptyCodegenFiles: false,
annotationsAs: 'decorators',
enableResourceInlining: false,
supportTestBed: false,
supportJitMode: false,
// Disable removing of comments as TS is quite aggressive with these and can
// remove important annotations, such as /* @__PURE__ */ and comments like /* vite-ignore */.
removeComments: false,
}),
);
}

abstract initialize(
tsconfig: string,
hostOptions: AngularHostOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
AngularCompilation,
AngularCompilationResult,
DiagnosticModes,
NoopCompilation,
createAngularCompilation,
} from './index';

Expand Down Expand Up @@ -59,44 +58,6 @@ describe('AngularCompilation', () => {
expect(diagnostics).toEqual({});
});

describe('NoopCompilation', () => {
it('initializes with empty referencedFiles and compiler options', async () => {
const compilation = new NoopCompilation();
const mockHostOptions = {} as AngularHostOptions;
const result = await compilation.initialize('tsconfig.json', mockHostOptions);

expect(result.referencedFiles).toEqual([]);
expect(result.compilerOptions).toBeDefined();
});

it('initializes with CompilerOptionOverrides object', async () => {
const compilation = new NoopCompilation();
const mockHostOptions = {} as AngularHostOptions;
const result = await compilation.initialize('tsconfig.json', mockHostOptions, {
sourcemap: true,
enableHmr: true,
});

expect(result.referencedFiles).toEqual([]);
expect(result.compilerOptions.inlineSources).toBe(true);
expect(result.compilerOptions.inlineSourceMap).toBe(true);
expect(result.compilerOptions['_enableHmr']).toBe(true);
});

it('throws when calling emitAffectedFiles', () => {
const compilation = new NoopCompilation();
expect(() => compilation.emitAffectedFiles()).toThrowError(
'Not available when using noop compilation.',
);
});

it('returns empty diagnostics from diagnoseFiles', async () => {
const compilation = new NoopCompilation();
const diagnostics = await compilation.diagnoseFiles();
expect(diagnostics).toEqual({});
});
});

describe('TypeScriptCompilation', () => {
class MockTypeScriptCompilation extends TypeScriptCompilation {
async initialize(): Promise<AngularCompilationResult> {
Expand Down Expand Up @@ -167,6 +128,12 @@ describe('AngularCompilation', () => {
await compilation.update?.(new Set(['/src/test.ts']));
expect(compilation.getCachedSourceFiles().has('/src/test.ts')).toBeFalse();
});

it('dynamically loads the @angular/compiler-cli module', async () => {
const compilerCli = await TypeScriptCompilation.loadCompilerCli();
expect(compilerCli).toBeDefined();
expect(typeof compilerCli.readConfiguration).toBe('function');
});
});

describe('createAngularCompilation', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ import { replaceBootstrap } from '../transformers/jit-bootstrap-transformer';
import { lazyRoutesTransformer } from '../transformers/lazy-routes-transformer';
import { createWorkerTransformer } from '../transformers/web-worker-transformer';
import {
AngularCompilation,
AngularCompilationResult,
type AngularCompilationResult,
DiagnosticModes,
EmitFileResult,
type EmitFileResult,
} from './angular-compilation';
import { CompilerOptionOverrides, transformCompilerOptions } from './compiler-options';
import { collectHmrCandidates } from './hmr-candidates';
Expand Down Expand Up @@ -69,7 +68,7 @@ export class AotCompilation extends TypeScriptCompilation {
compilerOptionOverrides?: CompilerOptionOverrides,
): Promise<AngularCompilationResult> {
// Dynamically load the Angular compiler CLI package
const { NgtscProgram, OptimizeFor } = await AngularCompilation.loadCompilerCli();
const { NgtscProgram, OptimizeFor } = await TypeScriptCompilation.loadCompilerCli();

// Load the compiler configuration and transform as needed
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ export {
} from './angular-compilation';
export type { CompilerOptionOverrides } from './compiler-options';
export { createAngularCompilation, type AngularCompilationMode } from './factory';
export { NoopCompilation } from './noop-compilation';
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ import { createJitResourceTransformer } from '../transformers/jit-resource-trans
import { lazyRoutesTransformer } from '../transformers/lazy-routes-transformer';
import { createWorkerTransformer } from '../transformers/web-worker-transformer';
import {
AngularCompilation,
AngularCompilationResult,
type AngularCompilationResult,
DiagnosticModes,
EmitFileResult,
type EmitFileResult,
} from './angular-compilation';
import { CompilerOptionOverrides, transformCompilerOptions } from './compiler-options';
import { TypeScriptCompilation } from './typescript-compilation';
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,46 @@
* found in the LICENSE file at https://angular.dev/license
*/

import type * as ng from '@angular/compiler-cli';
import type { PartialMessage } from 'esbuild';
import ts from 'typescript';
import { toPosixPath } from '../../../utils/path';
import { profileAsync } from '../../esbuild/profiling';
import { profileAsync, profileSync } from '../../esbuild/profiling';
import { AngularCompilation, DiagnosticModes } from './angular-compilation';
import { convertTypeScriptDiagnostic } from './diagnostics';

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

static async loadCompilerCli(): Promise<typeof ng> {
TypeScriptCompilation.#angularCompilerCliModule ??= await import('@angular/compiler-cli');

return TypeScriptCompilation.#angularCompilerCliModule;
}

protected async loadConfiguration(tsconfig: string): Promise<ng.ParsedConfiguration> {
const { readConfiguration } = await TypeScriptCompilation.loadCompilerCli();

return profileSync('NG_READ_CONFIG', () =>
readConfiguration(tsconfig, {
// Angular specific configuration defaults and overrides to ensure a functioning compilation.
suppressOutputPathCheck: true,
outDir: undefined,
sourceMap: false,
declaration: false,
declarationMap: false,
allowEmptyCodegenFiles: false,
annotationsAs: 'decorators',
enableResourceInlining: false,
supportTestBed: false,
supportJitMode: false,
// Disable removing of comments as TS is quite aggressive with these and can
// remove important annotations, such as /* @__PURE__ */ and comments like /* vite-ignore */.
removeComments: false,
}),
);
}

protected readonly sourceFiles = new Map<string, ts.SourceFile>();

protected invalidateFiles(files: Iterable<string>): void {
Expand Down
Loading