From a3618d7f708fcc803ab416699d64fbc7e376193c Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Fri, 6 Oct 2023 11:57:47 +0000 Subject: [PATCH] refactor(@angular-devkit/build-angular): move `SourceFileCache` into dedicated file This is to reduce the code in `compiler-plugin.ts` --- .../src/builders/application/execute-build.ts | 2 +- .../tools/esbuild/angular/compiler-plugin.ts | 37 +-------------- .../esbuild/angular/source-file-cache.ts | 46 +++++++++++++++++++ .../tools/esbuild/application-code-bundle.ts | 3 +- .../tools/esbuild/bundler-execution-result.ts | 2 +- .../tools/esbuild/compiler-plugin-options.ts | 3 +- 6 files changed, 54 insertions(+), 39 deletions(-) create mode 100644 packages/angular_devkit/build_angular/src/tools/esbuild/angular/source-file-cache.ts diff --git a/packages/angular_devkit/build_angular/src/builders/application/execute-build.ts b/packages/angular_devkit/build_angular/src/builders/application/execute-build.ts index 112467f89740..8bf80a083782 100644 --- a/packages/angular_devkit/build_angular/src/builders/application/execute-build.ts +++ b/packages/angular_devkit/build_angular/src/builders/application/execute-build.ts @@ -8,7 +8,7 @@ import { BuilderContext } from '@angular-devkit/architect'; import assert from 'node:assert'; -import { SourceFileCache } from '../../tools/esbuild/angular/compiler-plugin'; +import { SourceFileCache } from '../../tools/esbuild/angular/source-file-cache'; import { createBrowserCodeBundleOptions, createServerCodeBundleOptions, diff --git a/packages/angular_devkit/build_angular/src/tools/esbuild/angular/compiler-plugin.ts b/packages/angular_devkit/build_angular/src/tools/esbuild/angular/compiler-plugin.ts index 288fe68860ba..a226ec87b60b 100644 --- a/packages/angular_devkit/build_angular/src/tools/esbuild/angular/compiler-plugin.ts +++ b/packages/angular_devkit/build_angular/src/tools/esbuild/angular/compiler-plugin.ts @@ -16,13 +16,12 @@ import type { } from 'esbuild'; import assert from 'node:assert'; import { realpath } from 'node:fs/promises'; -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 { JavaScriptTransformer } from '../javascript-transformer'; -import { LoadResultCache, MemoryLoadResultCache } from '../load-result-cache'; +import { LoadResultCache } from '../load-result-cache'; import { logCumulativeDurations, profileAsync, @@ -33,39 +32,7 @@ import { BundleStylesheetOptions, bundleComponentStylesheet } from '../styleshee import { AngularHostOptions } from './angular-host'; import { AngularCompilation, AotCompilation, JitCompilation, NoopCompilation } from './compilation'; import { setupJitPluginCallbacks } from './jit-plugin-callbacks'; - -const USING_WINDOWS = platform() === 'win32'; -const WINDOWS_SEP_REGEXP = new RegExp(`\\${path.win32.sep}`, 'g'); - -export class SourceFileCache extends Map { - readonly modifiedFiles = new Set(); - readonly babelFileCache = new Map(); - readonly typeScriptFileCache = new Map(); - readonly loadResultCache = new MemoryLoadResultCache(); - - referencedFiles?: readonly string[]; - - constructor(readonly persistentCachePath?: string) { - super(); - } - - invalidate(files: Iterable): void { - this.modifiedFiles.clear(); - for (let file of files) { - this.babelFileCache.delete(file); - this.typeScriptFileCache.delete(pathToFileURL(file).href); - this.loadResultCache.invalidate(file); - - // Normalize separators to allow matching TypeScript Host paths - if (USING_WINDOWS) { - file = file.replace(WINDOWS_SEP_REGEXP, path.posix.sep); - } - - this.delete(file); - this.modifiedFiles.add(file); - } - } -} +import { SourceFileCache } from './source-file-cache'; export interface CompilerPluginOptions { sourcemap: boolean; diff --git a/packages/angular_devkit/build_angular/src/tools/esbuild/angular/source-file-cache.ts b/packages/angular_devkit/build_angular/src/tools/esbuild/angular/source-file-cache.ts new file mode 100644 index 000000000000..5288b6685f84 --- /dev/null +++ b/packages/angular_devkit/build_angular/src/tools/esbuild/angular/source-file-cache.ts @@ -0,0 +1,46 @@ +/** + * @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 { platform } from 'node:os'; +import * as path from 'node:path'; +import { pathToFileURL } from 'node:url'; +import ts from 'typescript'; +import { MemoryLoadResultCache } from '../load-result-cache'; + +const USING_WINDOWS = platform() === 'win32'; +const WINDOWS_SEP_REGEXP = new RegExp(`\\${path.win32.sep}`, 'g'); + +export class SourceFileCache extends Map { + readonly modifiedFiles = new Set(); + readonly babelFileCache = new Map(); + readonly typeScriptFileCache = new Map(); + readonly loadResultCache = new MemoryLoadResultCache(); + + referencedFiles?: readonly string[]; + + constructor(readonly persistentCachePath?: string) { + super(); + } + + invalidate(files: Iterable): void { + this.modifiedFiles.clear(); + for (let file of files) { + this.babelFileCache.delete(file); + this.typeScriptFileCache.delete(pathToFileURL(file).href); + this.loadResultCache.invalidate(file); + + // Normalize separators to allow matching TypeScript Host paths + if (USING_WINDOWS) { + file = file.replace(WINDOWS_SEP_REGEXP, path.posix.sep); + } + + this.delete(file); + this.modifiedFiles.add(file); + } + } +} diff --git a/packages/angular_devkit/build_angular/src/tools/esbuild/application-code-bundle.ts b/packages/angular_devkit/build_angular/src/tools/esbuild/application-code-bundle.ts index 317bb9e71b5e..438554b80d5e 100644 --- a/packages/angular_devkit/build_angular/src/tools/esbuild/application-code-bundle.ts +++ b/packages/angular_devkit/build_angular/src/tools/esbuild/application-code-bundle.ts @@ -13,7 +13,8 @@ import { readFile } from 'node:fs/promises'; import { extname, join, relative } from 'node:path'; import type { NormalizedApplicationBuildOptions } from '../../builders/application/options'; import { allowMangle } from '../../utils/environment-options'; -import { SourceFileCache, createCompilerPlugin } from './angular/compiler-plugin'; +import { createCompilerPlugin } from './angular/compiler-plugin'; +import { SourceFileCache } from './angular/source-file-cache'; import { createCompilerPluginOptions } from './compiler-plugin-options'; import { createAngularLocaleDataPlugin } from './i18n-locale-plugin'; import { createRxjsEsmResolutionPlugin } from './rxjs-esm-resolution-plugin'; diff --git a/packages/angular_devkit/build_angular/src/tools/esbuild/bundler-execution-result.ts b/packages/angular_devkit/build_angular/src/tools/esbuild/bundler-execution-result.ts index 2d2d53330aed..e977c172d4c4 100644 --- a/packages/angular_devkit/build_angular/src/tools/esbuild/bundler-execution-result.ts +++ b/packages/angular_devkit/build_angular/src/tools/esbuild/bundler-execution-result.ts @@ -7,7 +7,7 @@ */ import type { ChangedFiles } from '../../tools/esbuild/watcher'; -import type { SourceFileCache } from './angular/compiler-plugin'; +import type { SourceFileCache } from './angular/source-file-cache'; import type { BuildOutputFile, BuildOutputFileType, BundlerContext } from './bundler-context'; import { createOutputFileFromText } from './utils'; diff --git a/packages/angular_devkit/build_angular/src/tools/esbuild/compiler-plugin-options.ts b/packages/angular_devkit/build_angular/src/tools/esbuild/compiler-plugin-options.ts index 89cb3a756de5..b4e2f90c6423 100644 --- a/packages/angular_devkit/build_angular/src/tools/esbuild/compiler-plugin-options.ts +++ b/packages/angular_devkit/build_angular/src/tools/esbuild/compiler-plugin-options.ts @@ -7,7 +7,8 @@ */ import { NormalizedApplicationBuildOptions } from '../../builders/application/options'; -import type { SourceFileCache, createCompilerPlugin } from './angular/compiler-plugin'; +import type { createCompilerPlugin } from './angular/compiler-plugin'; +import type { SourceFileCache } from './angular/source-file-cache'; type CreateCompilerPluginParameters = Parameters;