Skip to content
Open
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
35 changes: 23 additions & 12 deletions weft/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,24 +167,27 @@
}

// CreateImportMap generates an import map script tag which maps JS module asset filenames to their
// respectful hash-prefixed path name. eg:
// respectful hash-prefixed path name. Also includes subresource integrity values for these files. eg:
//
// <script type="importmap" nonce="abcdefghijklmnop">
// {
// "imports":{
// "geonet-map.mjs":"/assets/js/77da7c4e-geonet-map.mjs"
// <script type="importmap" nonce="abcdefghijklmnop">
// {
// "imports":{
// "geonet-map.mjs":"/assets/js/77da7c4e-geonet-map.mjs"
// },
// "integrity":{
// "/assets/js/77da7c4e-geonet-map.mjs":"sha384-VbVf44SP6Q7kBOpKwzEQ3qhLRurPJ04Nrzv1JlaXnmSBXClEC94+WLmc97N8GfM1"
// }
// }
// }
// </script>
// </script>
func CreateImportMap(nonce string) template.HTML {

importMapping := make(map[string]string, 0)
for k, v := range assetHashes {
importMapping := make(map[string]*asset, 0)
for k, _ := range assetHashes {

Check failure on line 185 in weft/assets.go

View workflow job for this annotation

GitHub Actions / build / build-app / golangci-lint / lint

S1005: unnecessary assignment to the blank identifier (gosimple)
if !strings.HasSuffix(k, ".mjs") {
continue
}
filename := path.Base(k)
importMapping[filename] = v
importMapping[filename] = assets[k]
}
if len(importMapping) == 0 {
return template.HTML("")
Expand All @@ -196,7 +199,7 @@

// createImportMapTag returns the <script> tag of type "importmap" to faciliate browser with
// module resolution. Formatted to make readable in resulting source file.
func createImportMapTag(importMapping map[string]string, nonce string) string {
func createImportMapTag(importMapping map[string]*asset, nonce string) string {

importMap := "<script type=\"importmap\""
if nonce != "" {
Expand All @@ -212,9 +215,17 @@
sort.Strings(keys)

for _, k := range keys {
importMap += fmt.Sprintf("\n\t\t\"%s\":\"%s\",", k, importMapping[k])
importMap += fmt.Sprintf("\n\t\t\"%s\":\"%s\",", k, importMapping[k].hashedPath)
}
importMap = strings.TrimSuffix(importMap, ",")

// Add subresource integrity values
importMap += "\n\t},\n\t\"integrity\":{"
for _, k := range keys {
importMap += fmt.Sprintf("\n\t\t\"%s\":\"%s\",", importMapping[k].hashedPath, importMapping[k].sri)
}
importMap = strings.TrimSuffix(importMap, ",")

importMap += "\n\t}\n}\n</script>"

return importMap
Expand Down
28 changes: 22 additions & 6 deletions weft/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,35 +185,51 @@ func TestCreateImportTag(t *testing.T) {
work := []struct {
testName string
nonce string
importMapping map[string]string
importMapping map[string]*asset
expected string
}{
{
"No nonce, one module file",
"",
map[string]string{
"test.mjs": "/assets/js/hashprefix-test.mjs",
map[string]*asset{
"test.mjs": &asset{
hashedPath: "/assets/js/hashprefix-test.mjs",
sri: "sha384-abcd",
},
},
`<script type="importmap">
{
"imports":{
"test.mjs":"/assets/js/hashprefix-test.mjs"
},
"integrity":{
"/assets/js/hashprefix-test.mjs":"sha384-abcd"
}
}
</script>`,
},
{
"Nonce present, two module files",
"abcdefg",
map[string]string{
"test1.mjs": "/assets/js/hashprefix-test1.mjs",
"test2.mjs": "/assets/js/hashprefix-test2.mjs",
map[string]*asset{
"test1.mjs": &asset{
hashedPath: "/assets/js/hashprefix-test1.mjs",
sri: "sha384-efgh",
},
"test2.mjs": &asset{
hashedPath: "/assets/js/hashprefix-test2.mjs",
sri: "sha384-ijkl",
},
},
`<script type="importmap" nonce="abcdefg">
{
"imports":{
"test1.mjs":"/assets/js/hashprefix-test1.mjs",
"test2.mjs":"/assets/js/hashprefix-test2.mjs"
},
"integrity":{
"/assets/js/hashprefix-test1.mjs":"sha384-efgh",
"/assets/js/hashprefix-test2.mjs":"sha384-ijkl"
}
}
</script>`,
Expand Down
Loading