Skip to content

Commit 1541cfd

Browse files
committed
refactor(@angular-devkit/build-angular): move SourceFileCache into dedicated file
This is to reduce the code in `compiler-plugin.ts`
1 parent 9265d47 commit 1541cfd

File tree

6 files changed

+54
-39
lines changed

6 files changed

+54
-39
lines changed

packages/angular_devkit/build_angular/src/builders/application/execute-build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import { BuilderContext } from '@angular-devkit/architect';
1010
import assert from 'node:assert';
11-
import { SourceFileCache } from '../../tools/esbuild/angular/compiler-plugin';
11+
import { SourceFileCache } from '../../tools/esbuild/angular/source-file-cache';
1212
import {
1313
createBrowserCodeBundleOptions,
1414
createServerCodeBundleOptions,

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

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ import type {
1616
} from 'esbuild';
1717
import assert from 'node:assert';
1818
import { realpath } from 'node:fs/promises';
19-
import { platform } from 'node:os';
2019
import * as path from 'node:path';
2120
import { pathToFileURL } from 'node:url';
2221
import ts from 'typescript';
2322
import { maxWorkers } from '../../../utils/environment-options';
2423
import { JavaScriptTransformer } from '../javascript-transformer';
25-
import { LoadResultCache, MemoryLoadResultCache } from '../load-result-cache';
24+
import { LoadResultCache } from '../load-result-cache';
2625
import {
2726
logCumulativeDurations,
2827
profileAsync,
@@ -33,39 +32,7 @@ import { BundleStylesheetOptions, bundleComponentStylesheet } from '../styleshee
3332
import { AngularHostOptions } from './angular-host';
3433
import { AngularCompilation, AotCompilation, JitCompilation, NoopCompilation } from './compilation';
3534
import { setupJitPluginCallbacks } from './jit-plugin-callbacks';
36-
37-
const USING_WINDOWS = platform() === 'win32';
38-
const WINDOWS_SEP_REGEXP = new RegExp(`\\${path.win32.sep}`, 'g');
39-
40-
export class SourceFileCache extends Map<string, ts.SourceFile> {
41-
readonly modifiedFiles = new Set<string>();
42-
readonly babelFileCache = new Map<string, Uint8Array>();
43-
readonly typeScriptFileCache = new Map<string, string | Uint8Array>();
44-
readonly loadResultCache = new MemoryLoadResultCache();
45-
46-
referencedFiles?: readonly string[];
47-
48-
constructor(readonly persistentCachePath?: string) {
49-
super();
50-
}
51-
52-
invalidate(files: Iterable<string>): void {
53-
this.modifiedFiles.clear();
54-
for (let file of files) {
55-
this.babelFileCache.delete(file);
56-
this.typeScriptFileCache.delete(pathToFileURL(file).href);
57-
this.loadResultCache.invalidate(file);
58-
59-
// Normalize separators to allow matching TypeScript Host paths
60-
if (USING_WINDOWS) {
61-
file = file.replace(WINDOWS_SEP_REGEXP, path.posix.sep);
62-
}
63-
64-
this.delete(file);
65-
this.modifiedFiles.add(file);
66-
}
67-
}
68-
}
35+
import { SourceFileCache } from './source-file-cache';
6936

7037
export interface CompilerPluginOptions {
7138
sourcemap: boolean;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 { platform } from 'node:os';
10+
import * as path from 'node:path';
11+
import { pathToFileURL } from 'node:url';
12+
import ts from 'typescript';
13+
import { MemoryLoadResultCache } from '../load-result-cache';
14+
15+
const USING_WINDOWS = platform() === 'win32';
16+
const WINDOWS_SEP_REGEXP = new RegExp(`\\${path.win32.sep}`, 'g');
17+
18+
export class SourceFileCache extends Map<string, ts.SourceFile> {
19+
readonly modifiedFiles = new Set<string>();
20+
readonly babelFileCache = new Map<string, Uint8Array>();
21+
readonly typeScriptFileCache = new Map<string, string | Uint8Array>();
22+
readonly loadResultCache = new MemoryLoadResultCache();
23+
24+
referencedFiles?: readonly string[];
25+
26+
constructor(readonly persistentCachePath?: string) {
27+
super();
28+
}
29+
30+
invalidate(files: Iterable<string>): void {
31+
this.modifiedFiles.clear();
32+
for (let file of files) {
33+
this.babelFileCache.delete(file);
34+
this.typeScriptFileCache.delete(pathToFileURL(file).href);
35+
this.loadResultCache.invalidate(file);
36+
37+
// Normalize separators to allow matching TypeScript Host paths
38+
if (USING_WINDOWS) {
39+
file = file.replace(WINDOWS_SEP_REGEXP, path.posix.sep);
40+
}
41+
42+
this.delete(file);
43+
this.modifiedFiles.add(file);
44+
}
45+
}
46+
}

packages/angular_devkit/build_angular/src/tools/esbuild/application-code-bundle.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import { readFile } from 'node:fs/promises';
1313
import { extname, join, relative } from 'node:path';
1414
import type { NormalizedApplicationBuildOptions } from '../../builders/application/options';
1515
import { allowMangle } from '../../utils/environment-options';
16-
import { SourceFileCache, createCompilerPlugin } from './angular/compiler-plugin';
16+
import { createCompilerPlugin } from './angular/compiler-plugin';
17+
import { SourceFileCache } from './angular/source-file-cache';
1718
import { createCompilerPluginOptions } from './compiler-plugin-options';
1819
import { createAngularLocaleDataPlugin } from './i18n-locale-plugin';
1920
import { createRxjsEsmResolutionPlugin } from './rxjs-esm-resolution-plugin';

packages/angular_devkit/build_angular/src/tools/esbuild/bundler-execution-result.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import type { ChangedFiles } from '../../tools/esbuild/watcher';
10-
import type { SourceFileCache } from './angular/compiler-plugin';
10+
import type { SourceFileCache } from './angular/source-file-cache';
1111
import type { BuildOutputFile, BuildOutputFileType, BundlerContext } from './bundler-context';
1212
import { createOutputFileFromText } from './utils';
1313

packages/angular_devkit/build_angular/src/tools/esbuild/compiler-plugin-options.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
*/
88

99
import { NormalizedApplicationBuildOptions } from '../../builders/application/options';
10-
import type { SourceFileCache, createCompilerPlugin } from './angular/compiler-plugin';
10+
import type { createCompilerPlugin } from './angular/compiler-plugin';
11+
import type { SourceFileCache } from './angular/source-file-cache';
1112

1213
type CreateCompilerPluginParameters = Parameters<typeof createCompilerPlugin>;
1314

0 commit comments

Comments
 (0)