Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
const importmapFiles = Object.keys(importmap.integrity);
expect(importmapFiles.sort())
.withContext('importmap integrity keys should match emitted lazy JS files')
.toEqual(lazyJsFiles.sort());
.toEqual(lazyJsFiles.map((f) => `./${f}`).sort());

for (const [file, integrity] of Object.entries(importmap.integrity)) {
const expectedSri =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import { extname } from 'node:path';
import { htmlRewritingStream } from './html-rewriting-stream';
import { VALID_SELF_CLOSING_TAGS } from './valid-self-closing-tags';

/**
* RegExp to check if a URL is resolvable.
* A URL is resolvable if it is absolute (starting with http/https) or relative (starting with `./`, `../`, or `/`).
*/
const RESOLVABLE_URL_REGEXP = /^(?:\.{0,2}\/|https?:\/\/)/i;

export type LoadOutputFileFunctionType = (file: string) => Promise<string>;

export type CrossOriginValue = 'none' | 'anonymous' | 'use-credentials';
Expand Down Expand Up @@ -168,7 +174,9 @@ export async function augmentIndexHtml(
keyA.localeCompare(keyB),
);
for (const [url, integrityHash] of sortedEntries) {
integrity[generateUrl(url, deployUrl)] = integrityHash;
const resolvedUrl = generateUrl(url, deployUrl);
const key = RESOLVABLE_URL_REGEXP.test(resolvedUrl) ? resolvedUrl : `./${resolvedUrl}`;
integrity[key] = integrityHash;
}
const importMapJson = JSON.stringify({ integrity }).replace(/</g, '\\u003c');
subResourceIntegrityTag = `<script type="importmap">${importMapJson}</script>`;
Expand Down Expand Up @@ -268,9 +276,11 @@ export async function augmentIndexHtml(
if (isString(baseHref)) {
updateAttribute(tag, 'href', baseHref);
}

if (subResourceIntegrityTag) {
rewriter.emitRaw(subResourceIntegrityTag);
}

break;
case 'link':
if (readAttribute(tag, 'rel') === 'preconnect') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,10 @@ describe('augment-index-html', () => {

const match = content.match(/<script type="importmap">([^<]+)<\/script>/);
expect(match).withContext('importmap script tag missing').not.toBeNull();
expect(match?.[1]).toContain('lazy\\u003cchunk.js');
expect(match?.[1]).not.toContain('lazy<chunk.js');
expect(match?.[1]).toContain('./lazy\\u003cchunk.js');
expect(match?.[1]).not.toContain('./lazy<chunk.js');
expect(JSON.parse(match?.[1] ?? '{}')).toEqual({
integrity: { 'lazy<chunk.js': 'sha384-abc' },
integrity: { './lazy<chunk.js': 'sha384-abc' },
});
});

Expand Down
Loading