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 @@ -169,24 +169,27 @@ func CreateSubResourcePreload(args ...string) (template.HTML, error) {
}

// 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 {
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 @@ -198,7 +201,7 @@ func CreateImportMap(nonce string) template.HTML {

// 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 @@ -214,9 +217,17 @@ func createImportMapTag(importMapping map[string]string, nonce string) string {
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