Repository navigation
fix(@angular/build): add integrity metadata for all initial chunks beyond the module preload limit #34400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
fix(@angular/build): add integrity metadata for all initial chunks beyond the module preload limit #34400
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
packages/angular/build/src/builders/application/index-html-generator_spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /** | ||
| * @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.dev/license | ||
| */ | ||
|
|
||
| import assert from 'node:assert'; | ||
| import { mkdtemp, rm, writeFile } from 'node:fs/promises'; | ||
| import { join } from 'node:path'; | ||
| import { | ||
| BuildOutputFileType, | ||
| InitialFileRecord, | ||
| createOutputFile, | ||
| } from '../../tools/esbuild/bundler-files'; | ||
| import { generateIndexHtml } from './index-html-generator'; | ||
| import { NormalizedApplicationBuildOptions } from './options'; | ||
|
|
||
| describe('generateIndexHtml', () => { | ||
| let tempDir: string; | ||
|
|
||
| beforeEach(async () => { | ||
| const baseTmpDir = process.env['TEST_TMPDIR']; | ||
| assert(baseTmpDir, 'TEST_TMPDIR is not set'); | ||
| tempDir = await mkdtemp(join(baseTmpDir, 'index-html-generator-spec-')); | ||
| await writeFile( | ||
| join(tempDir, 'index.html'), | ||
| '<html><head><base href="/"></head><body><app-root></app-root></body></html>', | ||
| ); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await rm(tempDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| function createOptions( | ||
| subresourceIntegrity: boolean, | ||
| preloadInitial = true, | ||
| ): NormalizedApplicationBuildOptions { | ||
| return { | ||
| indexHtmlOptions: { | ||
| input: join(tempDir, 'index.html'), | ||
| insertionOrder: [['main.js', true]], | ||
| preloadInitial, | ||
| }, | ||
| subresourceIntegrity, | ||
| baseHref: '/', | ||
| security: {}, | ||
| } as unknown as NormalizedApplicationBuildOptions; | ||
| } | ||
|
|
||
| it('should add integrity metadata for every initial chunk when there are more than the module preload limit', async () => { | ||
| const chunkCount = 15; | ||
| const chunks = Array.from({ length: chunkCount }, (_, i) => `chunk-${i}.js`); | ||
| const initialFiles = new Map<string, InitialFileRecord>([ | ||
| ['main.js', { entrypoint: true, name: 'main', type: 'script', serverFile: false, depth: 0 }], | ||
| ...chunks.map( | ||
| (chunk, i) => | ||
| [chunk, { entrypoint: false, type: 'script', serverFile: false, depth: i + 1 }] as [ | ||
| string, | ||
| InitialFileRecord, | ||
| ], | ||
| ), | ||
| ]); | ||
| const outputFiles = ['main.js', ...chunks].map((path) => | ||
| createOutputFile(path, `console.log(${JSON.stringify(path)});`, BuildOutputFileType.Browser), | ||
| ); | ||
|
|
||
| const { csrContent, errors } = await generateIndexHtml( | ||
| initialFiles, | ||
| outputFiles, | ||
| createOptions(true), | ||
| ); | ||
| expect(errors).toHaveSize(0); | ||
|
|
||
| for (const chunk of chunks.slice(0, 10)) { | ||
| expect(csrContent).toContain(`<link rel="modulepreload" href="${chunk}" integrity="sha384`); | ||
| expect(csrContent).not.toContain(`./${chunk}`); | ||
| } | ||
|
|
||
| for (const chunk of chunks.slice(10)) { | ||
| expect(csrContent).toContain(`./${chunk}`); | ||
| expect(csrContent).not.toContain(`<link rel="modulepreload" href="${chunk}"`); | ||
| } | ||
| }); | ||
|
|
||
| it('should add integrity metadata for initial chunks when module preloads are disabled', async () => { | ||
| const initialFiles = new Map<string, InitialFileRecord>([ | ||
| ['main.js', { entrypoint: true, name: 'main', type: 'script', serverFile: false, depth: 0 }], | ||
| ['chunk-a.js', { entrypoint: false, type: 'script', serverFile: false, depth: 1 }], | ||
| ]); | ||
| const outputFiles = ['main.js', 'chunk-a.js'].map((path) => | ||
| createOutputFile(path, `console.log(${JSON.stringify(path)});`, BuildOutputFileType.Browser), | ||
| ); | ||
| const options = createOptions(true, false); | ||
|
|
||
| const { csrContent, errors } = await generateIndexHtml(initialFiles, outputFiles, options); | ||
| expect(errors).toHaveSize(0); | ||
|
|
||
| expect(csrContent).toContain(`"./${outputFiles[1].path}":`); | ||
| expect(csrContent).not.toContain(`"./${outputFiles[0].path}":`); | ||
| expect(csrContent).not.toContain('rel="modulepreload"'); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.