Skip to content

Commit

Permalink
feat(@angular-devkit/build-angular): add preload hints based on trans…
Browse files Browse the repository at this point in the history
…itive initial files

When using the esbuild-based browser application builder, the pre-existing initial file
analysis is now used to generate preload hints for any transitive initial files required
by the application. These hints are generated for both the initial JavaScript chunks and
any initial global stylesheets that may be present. These hints provide additional
information to the browser so that it can start and better prioritize fetching of files
needed to start the application.
  • Loading branch information
clydin authored and alan-agius4 committed Jun 9, 2023
1 parent 6abff16 commit 2a3fc68
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class ExecutionResult {
}
}

// eslint-disable-next-line max-lines-per-function
async function execute(
options: NormalizedBrowserOptions,
context: BuilderContext,
Expand Down Expand Up @@ -184,6 +185,24 @@ async function execute(

// Generate index HTML file
if (indexHtmlOptions) {
// Analyze metafile for initial link-based hints.
// Skip if the internal externalPackages option is enabled since this option requires
// dev server cooperation to properly resolve and fetch imports.
const hints = [];
if (!options.externalPackages) {
for (const [key, value] of initialFiles) {
if (value.entrypoint) {
// Entry points are already referenced in the HTML
continue;
}
if (value.type === 'script') {
hints.push({ url: key, mode: 'modulepreload' as const });
} else if (value.type === 'style') {
hints.push({ url: key, mode: 'preload' as const });
}
}
}

// Create an index HTML generator that reads from the in-memory output files
const indexHtmlGenerator = new IndexHtmlGenerator({
indexPath: indexHtmlOptions.input,
Expand Down Expand Up @@ -215,6 +234,7 @@ async function execute(
file,
extension: path.extname(file),
})),
hints,
});

for (const error of errors) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @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 { buildEsbuildBrowser } from '../../index';
import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup';

describeBuilder(buildEsbuildBrowser, BROWSER_BUILDER_INFO, (harness) => {
describe('Behavior: "Preload hints"', () => {
it('should add preload hints for transitive global style imports', async () => {
await harness.writeFile(
'src/styles.css',
`
@import url('https://fonts.googleapis.com/css2?family=Roboto+Mono&family=Roboto:wght@300;400;500;700&display=swap');
`,
);

harness.useTarget('build', {
...BASE_OPTIONS,
styles: ['src/styles.css'],
});

const { result } = await harness.executeOnce();
expect(result?.success).toBe(true);

harness
.expectFile('dist/index.html')
.content.toContain(
'<link rel="preload" href="https://fonts.googleapis.com/css2?family=Roboto+Mono&family=Roboto:wght@300;400;500;700&display=swap">',
);
});
});
});

0 comments on commit 2a3fc68

Please sign in to comment.