Add Data.Artifacts to css.Build and js.Build

Artifacts are the additional output files published as part of the build:
source maps and files emitted by ESBuild's file loader (e.g. fonts). Each
artifact provides Permalink, RelPermalink and MediaType, so e.g. font
preload links can be constructed in templates.

Also add media type definitions for source maps (application/source-map),
font/woff and font/woff2.

Fixes #15173

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Bjørn Erik Pedersen
2026-08-08 13:23:24 +02:00
parent 2ffaf1fc1e
commit 44da086082
7 changed files with 182 additions and 6 deletions
+51
View File
@@ -458,6 +458,8 @@ fakefontdata
{{ with . | css.Build (dict "minify" true) }}
INLINE: <style>{{ .Content | safeCSS }}</style>
LINKED: <link rel="stylesheet" href="{{ .RelPermalink }}" />
{{ range .Data.Artifacts }}ARTIFACT: {{ .RelPermalink }}|{{ .MediaType.Type }}
{{ end }}
{{ end }}
{{ end }}
`
@@ -471,6 +473,9 @@ LINKED: <link rel="stylesheet" href="{{ .RelPermalink }}" />
b.AssertFileContent("public/fr/css/main.css", `url("/docs/css/ComicNeue-Regular-`)
b.AssertPublishDir("en/css/ComicNeue-Regular-UA4ODE7N.ttf")
b.AssertPublishDir("fr/docs/css/ComicNeue-Regular-UA4ODE7N.ttf")
// The artifact URL is the same on every host. See issue #15173.
b.AssertFileContent("public/en/index.html", "ARTIFACT: /docs/css/ComicNeue-Regular-UA4ODE7N.ttf|font/ttf")
b.AssertFileContent("public/fr/index.html", "ARTIFACT: /docs/css/ComicNeue-Regular-UA4ODE7N.ttf|font/ttf")
}
t.Run("en first", func(t *testing.T) {
@@ -486,6 +491,52 @@ LINKED: <link rel="stylesheet" href="{{ .RelPermalink }}" />
})
}
// Issue #15173.
func TestCSSBuildDataArtifacts(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
baseURL = "https://example.org/mysite/"
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
-- assets/css/main.css --
@import "/fonts/fonts.css";
body { color: #222; }
-- assets/fonts/fonts.css --
@font-face {
font-family: 'Comic Neue';
src: url(ComicNeue-Regular.woff2) format('woff2'), url(ComicNeue-Regular.ttf) format('truetype');
}
-- assets/fonts/ComicNeue-Regular.ttf --
fakefontdata
-- assets/fonts/ComicNeue-Regular.woff2 --
fakefontdata2
-- layouts/home.html --
{{ with resources.Get "css/main.css" }}
{{ with . | css.Build (dict "minify" true "sourcemap" "external") }}
{{ range .Data.Artifacts }}
ARTIFACT: {{ .RelPermalink }}|{{ .Permalink }}|{{ .MediaType.Type }}
{{ end }}
{{ range .Data.Artifacts }}{{ if eq .MediaType.MainType "font" }}
<link rel="preload" href="{{ .RelPermalink }}" as="font" type="{{ .MediaType.Type }}" crossorigin>
{{ end }}{{ end }}
<link rel="stylesheet" href="{{ .RelPermalink }}">
{{ end }}
{{ end }}
`
b := hugolib.Test(t, files, hugolib.TestOptOsFs())
b.AssertFileContent("public/index.html",
"ARTIFACT: /mysite/css/main.css.map|https://example.org/mysite/css/main.css.map|application/source-map",
".ttf|https://example.org/mysite/css/ComicNeue-Regular-",
".woff2|https://example.org/mysite/css/ComicNeue-Regular-",
"|font/ttf",
"|font/woff2",
`as="font" type="font/woff2" crossorigin`,
)
b.AssertFileExists("public/css/main.css.map", true)
}
// Issue #14623
func TestCSSBuildLoadersPartial(t *testing.T) {
t.Parallel()