Skip to content
This repository was archived by the owner on May 17, 2025. It is now read-only.

Commit bf9aff0

Browse files
committed
Add integrity to modulepreload links if defined
1 parent 56db9b0 commit bf9aff0

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/generator.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -837,11 +837,21 @@ export class Generator {
837837
for (let dep of preloadDeps.sort()) {
838838
if (first || whitespace) preloads += newlineTab;
839839
if (first) first = false;
840-
preloads += `<link rel="modulepreload" href="${
840+
const url =
841841
rootUrl || htmlUrl
842842
? relativeUrl(new URL(dep), new URL(rootUrl || htmlUrl), !!rootUrl)
843-
: dep
844-
}" />`;
843+
: dep;
844+
preloads += `<link rel="modulepreload" href="${url}"${
845+
integrity
846+
? ` integrity="${await getIntegrity(
847+
new Uint8Array(
848+
await (
849+
await fetch(url, this.traceMap.resolver.fetchOpts)
850+
).arrayBuffer()
851+
)
852+
)}"`
853+
: ""
854+
} />`;
845855
}
846856
}
847857

test/html/preload.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ assert.strictEqual(
105105
"<!-- Generated by @jspm/generator - https://github.com/jspm/generator -->\n" +
106106
`<script async src="${esmsUrl}" crossorigin="anonymous" integrity="${esmsIntegrity}"></script>\n` +
107107
`<script type="importmap">{"imports":{"react":"https://ga.jspm.io/npm:react@16.14.0/index.js"},"scopes":{"https://ga.jspm.io/":{"object-assign":"https://ga.jspm.io/npm:object-assign@4.1.1/index.js"}},"integrity":{"https://ga.jspm.io/npm:object-assign@4.1.1/index.js":"sha384-iQp1zoaqIhfUYyYkz3UNk1QeFfmBGgt1Ojq0kZD5Prql1g7fgJVzVgsjDoR65lv8","https://ga.jspm.io/npm:react@16.14.0/cjs/react.production.min.js":"${reactProductionIntegrity}","https://ga.jspm.io/npm:react@16.14.0/index.js":"${reactIndexIntegrity}"}}</script>\n` +
108-
'<link rel="modulepreload" href="https://ga.jspm.io/npm:object-assign@4.1.1/index.js" /><link rel="modulepreload" href="https://ga.jspm.io/npm:react@16.14.0/cjs/react.production.min.js" /><link rel="modulepreload" href="https://ga.jspm.io/npm:react@16.14.0/index.js" />\n' +
108+
`<link rel="modulepreload" href="https://ga.jspm.io/npm:object-assign@4.1.1/index.js" integrity="sha384-iQp1zoaqIhfUYyYkz3UNk1QeFfmBGgt1Ojq0kZD5Prql1g7fgJVzVgsjDoR65lv8" /><link rel="modulepreload" href="https://ga.jspm.io/npm:react@16.14.0/cjs/react.production.min.js" integrity="${reactProductionIntegrity}" /><link rel="modulepreload" href="https://ga.jspm.io/npm:react@16.14.0/index.js" integrity="${reactIndexIntegrity}" />\n` +
109109
'<script type="module">\n' +
110110
" import 'react';\n" +
111111
"</script>\n"

0 commit comments

Comments
 (0)