diff --git a/packages/angular/build/src/tools/angular/compilation/parallel-compilation.ts b/packages/angular/build/src/tools/angular/compilation/parallel-compilation.ts index 8fa33860b5b4..84bd53891d92 100644 --- a/packages/angular/build/src/tools/angular/compilation/parallel-compilation.ts +++ b/packages/angular/build/src/tools/angular/compilation/parallel-compilation.ts @@ -8,7 +8,7 @@ import type { CompilerOptions } from '@angular/compiler-cli'; import type { PartialMessage } from 'esbuild'; -import { createRequire } from 'node:module'; +import { createRequire, getCompileCacheDir } from 'node:module'; import { MessageChannel } from 'node:worker_threads'; import Piscina from 'piscina'; import type { SourceFile } from 'typescript'; @@ -41,6 +41,11 @@ export class ParallelCompilation extends AngularCompilation { useAtomics: !process.versions.webcontainer, filename: localRequire.resolve('./parallel-worker'), recordTiming: false, + env: { + ...process.env, + // Enable compile code caching if enabled for the main process (only exists on Node.js v22.8+) + 'NODE_COMPILE_CACHE': getCompileCacheDir?.(), + }, }); } diff --git a/packages/angular/build/src/tools/esbuild/javascript-transformer.ts b/packages/angular/build/src/tools/esbuild/javascript-transformer.ts index ce4b0aa91356..6dbf2a9596a9 100644 --- a/packages/angular/build/src/tools/esbuild/javascript-transformer.ts +++ b/packages/angular/build/src/tools/esbuild/javascript-transformer.ts @@ -8,6 +8,7 @@ import { createHash } from 'node:crypto'; import { readFile } from 'node:fs/promises'; +import { getCompileCacheDir } from 'node:module'; import Piscina from 'piscina'; import { Cache } from './cache'; @@ -62,6 +63,11 @@ export class JavaScriptTransformer { // Shutdown idle threads after 1 second of inactivity idleTimeout: 1000, recordTiming: false, + env: { + ...process.env, + // Enable compile code caching if enabled for the main process (only exists on Node.js v22.8+) + 'NODE_COMPILE_CACHE': getCompileCacheDir?.(), + }, }); return this.#workerPool; diff --git a/packages/angular/build/src/tools/sass/sass-service.ts b/packages/angular/build/src/tools/sass/sass-service.ts index 545e777a4b70..ce6f18cadbfe 100644 --- a/packages/angular/build/src/tools/sass/sass-service.ts +++ b/packages/angular/build/src/tools/sass/sass-service.ts @@ -7,6 +7,7 @@ */ import assert from 'node:assert'; +import { getCompileCacheDir } from 'node:module'; import { fileURLToPath, pathToFileURL } from 'node:url'; import { MessageChannel } from 'node:worker_threads'; import { Piscina } from 'piscina'; @@ -101,6 +102,11 @@ export class SassWorkerImplementation { // Shutdown idle threads after 1 second of inactivity idleTimeout: 1000, recordTiming: false, + env: { + ...process.env, + // Enable compile code caching if enabled for the main process (only exists on Node.js v22.8+) + 'NODE_COMPILE_CACHE': getCompileCacheDir?.(), + }, }); return this.#workerPool; diff --git a/packages/angular/build/src/typings.d.ts b/packages/angular/build/src/typings.d.ts index c784b3c4220a..7eaadcafc536 100644 --- a/packages/angular/build/src/typings.d.ts +++ b/packages/angular/build/src/typings.d.ts @@ -17,3 +17,10 @@ declare module 'esbuild' { export * from 'esbuild-wasm'; } + +/** + * Augment the Node.js module builtin types to support the v22.8+ compile cache functions + */ +declare module 'node:module' { + function getCompileCacheDir(): string | undefined; +} diff --git a/packages/angular/cli/bin/bootstrap.js b/packages/angular/cli/bin/bootstrap.js index 96b978296dcc..1eefb72f05c1 100644 --- a/packages/angular/cli/bin/bootstrap.js +++ b/packages/angular/cli/bin/bootstrap.js @@ -18,4 +18,12 @@ * range. */ -import('../lib/init.js'); +// Enable on-disk code caching if available (Node.js 22.8+) +try { + const { enableCompileCache } = require('node:module'); + + enableCompileCache?.(); +} catch {} + +// Initialize the Angular CLI +void import('../lib/init.js');