Skip to content

Commit

Permalink
perf(@angular-devkit/build-angular): use Uint8Arrays for incremental …
Browse files Browse the repository at this point in the history
…caching with esbuild-based builder

When using the experimental esbuild-based browser application builder in watch mode, the in-memory
incremental caching will now use Uint8Arrays where possible to limit the amount of encoding needed
when passing data to esbuild. When passing a string to esbuild, it must encode the string prior to
sending it. By providing the pre-encoded data from the in-memory cache, this step can be skipped
for rebuilds of input files that have not changed.

(cherry picked from commit 2adeb6f)
  • Loading branch information
clydin authored and alan-agius4 committed Oct 13, 2022
1 parent 9a4e7ff commit 45a9422
Showing 1 changed file with 6 additions and 4 deletions.
Expand Up @@ -132,7 +132,7 @@ const WINDOWS_SEP_REGEXP = new RegExp(`\\${path.win32.sep}`, 'g');

export class SourceFileCache extends Map<string, ts.SourceFile> {
readonly modifiedFiles = new Set<string>();
readonly babelFileCache = new Map<string, string>();
readonly babelFileCache = new Map<string, Uint8Array>();

invalidate(files: Iterable<string>): void {
this.modifiedFiles.clear();
Expand Down Expand Up @@ -247,7 +247,7 @@ export function createCompilerPlugin(

let previousBuilder: ts.EmitAndSemanticDiagnosticsBuilderProgram | undefined;
let previousAngularProgram: NgtscProgram | undefined;
const babelDataCache = new Map<string, string>();
const babelDataCache = new Map<string, Uint8Array>();
const diagnosticCache = new WeakMap<ts.SourceFile, ts.Diagnostic[]>();

build.onStart(async () => {
Expand Down Expand Up @@ -466,7 +466,8 @@ export function createCompilerPlugin(
// would need to be added to the key as well.
let contents = babelDataCache.get(data);
if (contents === undefined) {
contents = await transformWithBabel(args.path, data, pluginOptions);
const transformedData = await transformWithBabel(args.path, data, pluginOptions);
contents = Buffer.from(transformedData, 'utf-8');
babelDataCache.set(data, contents);
}

Expand All @@ -490,7 +491,8 @@ export function createCompilerPlugin(
let contents = pluginOptions.sourceFileCache?.babelFileCache.get(args.path);
if (contents === undefined) {
const data = await fs.readFile(args.path, 'utf-8');
contents = await transformWithBabel(args.path, data, pluginOptions);
const transformedData = await transformWithBabel(args.path, data, pluginOptions);
contents = Buffer.from(transformedData, 'utf-8');
pluginOptions.sourceFileCache?.babelFileCache.set(args.path, contents);
}

Expand Down

0 comments on commit 45a9422

Please sign in to comment.