Skip to content

Commit

Permalink
fix: add integrity to modulepreload links if defined (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
novucs authored Jan 6, 2025
1 parent 56db9b0 commit 5dfed49
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -837,11 +837,21 @@ export class Generator {
for (let dep of preloadDeps.sort()) {
if (first || whitespace) preloads += newlineTab;
if (first) first = false;
preloads += `<link rel="modulepreload" href="${
const url =
rootUrl || htmlUrl
? relativeUrl(new URL(dep), new URL(rootUrl || htmlUrl), !!rootUrl)
: dep
}" />`;
: dep;
preloads += `<link rel="modulepreload" href="${url}"${
integrity
? ` integrity="${await getIntegrity(
new Uint8Array(
await (
await fetch(url, this.traceMap.resolver.fetchOpts)
).arrayBuffer()
)
)}"`
: ""
} />`;
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/html/preload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ assert.strictEqual(
"<!-- Generated by @jspm/generator - https://github.com/jspm/generator -->\n" +
`<script async src="${esmsUrl}" crossorigin="anonymous" integrity="${esmsIntegrity}"></script>\n` +
`<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` +
'<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' +
`<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` +
'<script type="module">\n' +
" import 'react';\n" +
"</script>\n"
Expand Down

0 comments on commit 5dfed49

Please sign in to comment.