Skip to content

Commit 2a3fc68

Browse files
clydinalan-agius4
authored andcommittedJun 9, 2023
feat(@angular-devkit/build-angular): add preload hints based on transitive 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.
1 parent 6abff16 commit 2a3fc68

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
 

‎packages/angular_devkit/build_angular/src/builders/browser-esbuild/index.ts

+20
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class ExecutionResult {
9292
}
9393
}
9494

95+
// eslint-disable-next-line max-lines-per-function
9596
async function execute(
9697
options: NormalizedBrowserOptions,
9798
context: BuilderContext,
@@ -184,6 +185,24 @@ async function execute(
184185

185186
// Generate index HTML file
186187
if (indexHtmlOptions) {
188+
// Analyze metafile for initial link-based hints.
189+
// Skip if the internal externalPackages option is enabled since this option requires
190+
// dev server cooperation to properly resolve and fetch imports.
191+
const hints = [];
192+
if (!options.externalPackages) {
193+
for (const [key, value] of initialFiles) {
194+
if (value.entrypoint) {
195+
// Entry points are already referenced in the HTML
196+
continue;
197+
}
198+
if (value.type === 'script') {
199+
hints.push({ url: key, mode: 'modulepreload' as const });
200+
} else if (value.type === 'style') {
201+
hints.push({ url: key, mode: 'preload' as const });
202+
}
203+
}
204+
}
205+
187206
// Create an index HTML generator that reads from the in-memory output files
188207
const indexHtmlGenerator = new IndexHtmlGenerator({
189208
indexPath: indexHtmlOptions.input,
@@ -215,6 +234,7 @@ async function execute(
215234
file,
216235
extension: path.extname(file),
217236
})),
237+
hints,
218238
});
219239

220240
for (const error of errors) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 { buildEsbuildBrowser } from '../../index';
10+
import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup';
11+
12+
describeBuilder(buildEsbuildBrowser, BROWSER_BUILDER_INFO, (harness) => {
13+
describe('Behavior: "Preload hints"', () => {
14+
it('should add preload hints for transitive global style imports', async () => {
15+
await harness.writeFile(
16+
'src/styles.css',
17+
`
18+
@import url('https://fonts.googleapis.com/css2?family=Roboto+Mono&family=Roboto:wght@300;400;500;700&display=swap');
19+
`,
20+
);
21+
22+
harness.useTarget('build', {
23+
...BASE_OPTIONS,
24+
styles: ['src/styles.css'],
25+
});
26+
27+
const { result } = await harness.executeOnce();
28+
expect(result?.success).toBe(true);
29+
30+
harness
31+
.expectFile('dist/index.html')
32+
.content.toContain(
33+
'<link rel="preload" href="https://fonts.googleapis.com/css2?family=Roboto+Mono&family=Roboto:wght@300;400;500;700&display=swap">',
34+
);
35+
});
36+
});
37+
});

0 commit comments

Comments
 (0)
Please sign in to comment.