Skip to content

Commit bc47fa1

Browse files
committed
perf(@angular/build): improve rebuild time for file loader usage with prebundling
Rebuilds can now use the optimized external package execute path when using the `application` builder's `loader` option if only `file` type loaders are used. The Vite-based development server will now process any `file` type loader usage for third-party file references which removes the need for the build itself to handle the loader processing. This change only optimizes the case where only the `file` loader type is used. If any other loader types are present, the optimized execution path will not be used. Future further improvements may allow for all cases to use the optimized rebuild execution path.
1 parent 132f5e6 commit bc47fa1

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

packages/angular/build/src/builders/dev-server/vite-server.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,15 @@ export async function setupServer(
482482
css: {
483483
devSourcemap: true,
484484
},
485+
// Ensure custom 'file' loader build option entries are handled by Vite in application code that
486+
// reference third-party libraries. Relative usage is handled directly by the build and not Vite.
487+
// Only 'file' loader entries are currently supported directly by Vite.
488+
assetsInclude:
489+
prebundleLoaderExtensions &&
490+
Object.entries(prebundleLoaderExtensions)
491+
.filter(([, value]) => value === 'file')
492+
// Create a file extension glob for each key
493+
.map(([key]) => '*' + key),
485494
// Vite will normalize the `base` option by adding a leading slash.
486495
base: serverOptions.servePath,
487496
resolve: {

packages/angular/build/src/tools/esbuild/external-packages-plugin.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ export function createExternalPackagesPlugin(options?: { exclude?: string[] }):
2424
return {
2525
name: 'angular-external-packages',
2626
setup(build) {
27+
// Find all loader keys that are not using the 'file' loader.
28+
// The 'file' loader is automatically handled by Vite and does not need exclusion.
2729
const loaderOptionKeys =
28-
build.initialOptions.loader && Object.keys(build.initialOptions.loader);
30+
build.initialOptions.loader &&
31+
Object.entries(build.initialOptions.loader)
32+
.filter(([, value]) => value !== 'file')
33+
.map(([key]) => key);
2934

3035
// Safe to use native packages external option if no loader options or exclusions present
3136
if (!exclusions && !loaderOptionKeys?.length) {

0 commit comments

Comments
 (0)