Skip to content

Commit fd77525

Browse files
committed
Add integrity to modulepreload links if defined
1 parent 7814cac commit fd77525

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/generator.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -826,11 +826,21 @@ export class Generator {
826826
for (let dep of preloadDeps.sort()) {
827827
if (first || whitespace) preloads += newlineTab;
828828
if (first) first = false;
829-
preloads += `<link rel="modulepreload" href="${
829+
const url =
830830
rootUrl || htmlUrl
831831
? relativeUrl(new URL(dep), new URL(rootUrl || htmlUrl), !!rootUrl)
832-
: dep
833-
}" />`;
832+
: dep;
833+
preloads += `<link rel="modulepreload" href="${url}"${
834+
integrity
835+
? ` integrity="${await getIntegrity(
836+
new Uint8Array(
837+
await (
838+
await fetch(url, this.traceMap.resolver.fetchOpts)
839+
).arrayBuffer()
840+
)
841+
)}"`
842+
: ""
843+
} />`;
834844
}
835845
}
836846

test/html/preload.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ assert.strictEqual(
9797
"<!-- Generated by @jspm/generator - https://github.com/jspm/generator -->\n" +
9898
`<script async src="${esmsUrl}" crossorigin="anonymous" integrity="${esmsIntegrity}"></script>\n` +
9999
`<script type="importmap">{"imports":{"react":"https://ga.jspm.io/npm:[email protected]/index.js"},"scopes":{"https://ga.jspm.io/":{"object-assign":"https://ga.jspm.io/npm:[email protected]/index.js"}},"integrity":{"https://ga.jspm.io/npm:[email protected]/index.js":"sha384-iQp1zoaqIhfUYyYkz3UNk1QeFfmBGgt1Ojq0kZD5Prql1g7fgJVzVgsjDoR65lv8","https://ga.jspm.io/npm:[email protected]/cjs/react.production.min.js":"${reactProductionIntegrity}","https://ga.jspm.io/npm:[email protected]/index.js":"${reactIndexIntegrity}"}}</script>\n` +
100-
'<link rel="modulepreload" href="https://ga.jspm.io/npm:[email protected]/index.js" /><link rel="modulepreload" href="https://ga.jspm.io/npm:[email protected]/cjs/react.production.min.js" /><link rel="modulepreload" href="https://ga.jspm.io/npm:[email protected]/index.js" />\n' +
100+
`<link rel="modulepreload" href="https://ga.jspm.io/npm:[email protected]/index.js" integrity="sha384-iQp1zoaqIhfUYyYkz3UNk1QeFfmBGgt1Ojq0kZD5Prql1g7fgJVzVgsjDoR65lv8" /><link rel="modulepreload" href="https://ga.jspm.io/npm:[email protected]/cjs/react.production.min.js" integrity="${reactProductionIntegrity}" /><link rel="modulepreload" href="https://ga.jspm.io/npm:[email protected]/index.js" integrity="${reactIndexIntegrity}" />\n` +
101101
'<script type="module">\n' +
102102
" import 'react';\n" +
103103
"</script>\n"

0 commit comments

Comments
 (0)