Skip to content

Commit 6f520a9

Browse files
committed
perf(@angular-devkit/build-angular): cache polyfills virtual module result
This commit adds caching to the polyfills virtual modules to avoid module resolutions on rebuilds which can take up to 3 seconds in some cases. Whilst these are done async, these can still slow down the build slightly due to IO limits.
1 parent 886cb31 commit 6f520a9

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@ export function createBrowserPolyfillBundleOptions(
7676
sourceFileCache?: SourceFileCache,
7777
): BuildOptions | undefined {
7878
const namespace = 'angular:polyfills';
79-
const polyfillBundleOptions = getEsBuildCommonPolyfillsOptions(options, namespace, true);
79+
const polyfillBundleOptions = getEsBuildCommonPolyfillsOptions(
80+
options,
81+
namespace,
82+
true,
83+
sourceFileCache,
84+
);
8085
if (!polyfillBundleOptions) {
8186
return;
8287
}
@@ -155,7 +160,7 @@ export function createServerCodeBundleOptions(
155160

156161
const ssrEntryPoint = ssrOptions?.entry;
157162
if (ssrEntryPoint) {
158-
entryPoints['server'] = ssrOptions?.entry;
163+
entryPoints['server'] = ssrEntryPoint;
159164
}
160165

161166
const buildOptions: BuildOptions = {
@@ -196,6 +201,7 @@ export function createServerCodeBundleOptions(
196201
buildOptions.plugins.push(
197202
createVirtualModulePlugin({
198203
namespace: mainServerNamespace,
204+
cache: sourceFileCache?.loadResultCache,
199205
loadContent: async () => {
200206
const contents: string[] = [
201207
`export { ɵConsole } from '@angular/core';`,
@@ -265,6 +271,7 @@ export function createServerPolyfillBundleOptions(
265271
},
266272
namespace,
267273
false,
274+
sourceFileCache,
268275
);
269276

270277
if (!polyfillBundleOptions) {
@@ -394,6 +401,7 @@ function getEsBuildCommonPolyfillsOptions(
394401
options: NormalizedApplicationBuildOptions,
395402
namespace: string,
396403
tryToResolvePolyfillsAsRelative: boolean,
404+
sourceFileCache: SourceFileCache | undefined,
397405
): BuildOptions | undefined {
398406
const { jit, workspaceRoot, i18nOptions } = options;
399407
const buildOptions: BuildOptions = {
@@ -449,6 +457,7 @@ function getEsBuildCommonPolyfillsOptions(
449457
buildOptions.plugins?.push(
450458
createVirtualModulePlugin({
451459
namespace,
460+
cache: sourceFileCache?.loadResultCache,
452461
loadContent: async (_, build) => {
453462
let hasLocalizePolyfill = false;
454463
let polyfillPaths = polyfills;

packages/angular_devkit/build_angular/src/tools/esbuild/virtual-module-plugin.ts

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

99
import type { OnLoadArgs, Plugin, PluginBuild } from 'esbuild';
10+
import { LoadResultCache, createCachedLoad } from './load-result-cache';
1011

1112
/**
1213
* Options for the createVirtualModulePlugin
@@ -26,6 +27,8 @@ export interface VirtualModulePluginOptions {
2627
) => ReturnType<Parameters<PluginBuild['onLoad']>[1]>;
2728
/** Restrict to only entry points. Defaults to `true`. */
2829
entryPointOnly?: boolean;
30+
/** Load results cache. */
31+
cache?: LoadResultCache;
2932
}
3033

3134
/**
@@ -39,6 +42,7 @@ export function createVirtualModulePlugin(options: VirtualModulePluginOptions):
3942
external,
4043
transformPath: pathTransformer,
4144
loadContent,
45+
cache,
4246
entryPointOnly = true,
4347
} = options;
4448

@@ -65,7 +69,10 @@ export function createVirtualModulePlugin(options: VirtualModulePluginOptions):
6569
});
6670
}
6771

68-
build.onLoad({ filter: /./, namespace }, (args) => loadContent(args, build));
72+
build.onLoad(
73+
{ filter: /./, namespace },
74+
createCachedLoad(cache, (args) => loadContent(args, build)),
75+
);
6976
},
7077
};
7178
}

0 commit comments

Comments
 (0)