Debian Codegraph @

tpl/css/build_integration_test.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Copyright 2026 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package css_test
15
16 import (
17 "strings"
18 "testing"
19
20 qt "github.com/frankban/quicktest"
21 "github.com/gohugoio/hugo/htesting"
22 "github.com/gohugoio/hugo/hugolib"
23 "github.com/gohugoio/hugo/internal/js/esbuild"
24 )
25
26 func TestCSSBuild(t *testing.T) {
27 t.Parallel()
28
29 files := `
30 -- hugo.toml --
31 -- assets/a/pixel.png --
32 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
33 -- assets/css/main.css --
34 @import "tailwindcss";
35 @import url('https://example.org/foo.css');
36 @import "./foo.css";
37 @import "./bar.css" layer(mylayer);
38 @import "./bar.css" layer;
39 @import './baz.css' screen and (min-width: 800px);
40 @import 'relnodot.css' supports(display: grid);
41 @import 'relnodotboth.css';
42 @import '/relnodotboth2.css';
43 body {
44 background-image: url("a/pixel.png");
45 }
46 .mask1 {
47 mask-image: url(a/pixel.png);
48 mask-repeat: no-repeat;
49 }
50 -- assets/css/foo.css --
51 p { background: red; }
52 -- assets/css/bar.css --
53 div { background: blue; }
54 -- assets/css/baz.css --
55 span { background: green; }
56 -- assets/css/relnodot.css --
57 article { background: yellow; }
58 -- assets/css/relnodotboth.css --
59 .relnodotbothrelative { background: green; }
60 -- assets/relnodotboth.css --
61 .relnodotbothroot { background: blue; }
62 -- assets/css/relnodotboth2.css --
63 .relnodotboth2relative { background: green; }
64 -- assets/relnodotboth2.css --
65 .relnodotboth2root { background: blue; }
66 -- layouts/home.html --
67 {{ with resources.Get "css/main.css" }}
68 {{ $opts := (dict "minify" true "target" (slice "chrome108" "firefox116" "safari16.4" "edge115" "ios16.4" "opera101") "loaders" (dict ".png" "dataurl") "externals" (slice "tailwindcss"))}}
69 {{ with . | css.Build $opts }}
70 <link rel="stylesheet" href="{{ .RelPermalink }}" />
71 {{ end }}
72 {{ end }}
73 `
74
75 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
76 b.AssertFileContent("public/css/main.css",
77 `-webkit-mask-repeat`, // browser prefix.
78 "data:image/png;base64", // dataurl loader
79 `@import"tailwindcss";`, // external
80 `@import"https://example.org/foo.css";`, // external
81 "mylayer{div", // imported layer
82 "@media screen and (min-width:800px){span", // imported media query
83 "@supports (display: grid){article", // imported supports
84 "relnodotbothrelative", //@import 'relnodotboth.css'; file exists in both assets and project root.
85 "relnodotboth2root", //@import '/relnodotboth2.css'; file exists in both assets and project root.
86 )
87 }
88
89 func TestCSSBuildEdit(t *testing.T) {
90 t.Parallel()
91
92 files := `
93 -- hugo.toml --
94 disableKinds = ["taxonomy", "term", "rss"]
95 -- assets/images/bar.svg --
96 barsvg
97 -- assets/images/foo.svg --
98 foosvg
99 -- assets/css/main.css --
100 @import "./foo.css";
101 @import "./bar.css" layer(mylayer);
102 .main { background: red; }
103 -- assets/css/foo.css --
104 .foo { background: green; }
105 -- assets/css/bar.css --
106 .bar { background-image: url("images/bar.svg"); }
107 -- layouts/all.html --
108 All. No CSS here.
109 -- layouts/home.html --
110 {{ with resources.Get "css/main.css" }}
111 {{ $opts := (dict "minify" true) }}
112 {{ with . | css.Build $opts }}
113 <link rel="stylesheet" href="{{ .RelPermalink }}" />
114 {{ end }}
115 {{ end }}
116 `
117 b := hugolib.TestRunning(t, files, hugolib.TestOptOsFs())
118
119 b.AssertFileContent("public/css/main.css", `.foo{background:green}@layer mylayer{.bar{background-image:url("/css/bar-Y35ORVQM.svg")}}`)
120
121 // Edit svg
122 b.EditFileReplaceAll("assets/images/bar.svg", "barsvg", "newbarsvg").Build()
123 b.AssertRenderCountPage(1)
124 b.AssertFileContent("public/css/main.css", `/css/bar-LVHHRPN5.svg`) // new hash.
125 b.AssertFileContent("public/css/bar-LVHHRPN5.svg", "newbarsvg")
126
127 // Edit foo.css
128 b.EditFileReplaceAll("assets/css/foo.css", "green", "red").Build()
129 b.AssertRenderCountPage(1)
130 b.AssertFileContent("public/css/main.css", `.foo{background:red}`) // updated content.
131
132 // Edit bar.css
133 b.EditFileReplaceAll("assets/css/bar.css", "bar.svg", "foo.svg").Build()
134 b.AssertRenderCountPage(1)
135 b.AssertFileContent("public/css/main.css", `/css/foo-52JTT5GU.svg`)
136 b.AssertFileContent("public/css/foo-52JTT5GU.svg", "foosvg")
137
138 // Edit main.css
139 b.EditFileReplaceAll("assets/css/main.css", "red", "blue").Build()
140 b.AssertRenderCountPage(1)
141 b.AssertFileContent("public/css/main.css", `.main{background:#00f}`) // updated content, blue gets minified to #00f.
142 }
143
144 func TestCSSBuildEditOptions(t *testing.T) {
145 t.Parallel()
146
147 files := `
148 -- hugo.toml --
149 disableKinds = ["taxonomy", "term", "rss"]
150 disableLiveReload = true
151 -- assets/css/main.css --
152 body {
153 background: red;
154 }
155 -- layouts/home.html --
156 Home.
157 {{ with resources.Get "css/main.css" }}
158 {{ $opts := dict "minify" false }}
159 {{ with . | css.Build $opts }}
160 <link rel="stylesheet" href="{{ .RelPermalink }}" />
161 {{ end }}
162 {{ end }}
163 `
164
165 b := hugolib.TestRunning(t, files, hugolib.TestOptOsFs())
166 b.AssertFileContent("public/css/main.css", ` background: red;`)
167
168 b.EditFileReplaceAll("layouts/home.html", `"minify" false`, `"minify" true`).Build()
169 b.AssertFileContent("public/css/main.css", `{background:red}`)
170 }
171
172 func TestCSSBuildEditOptionsMultiple(t *testing.T) {
173 t.Parallel()
174
175 files := `
176 -- hugo.toml --
177 disableKinds = ["taxonomy", "term", "rss"]
178 disableLiveReload = true
179 -- assets/css/main.css --
180 body {
181 background: red;
182 }
183 -- layouts/_partials/css.html --
184 {{ with resources.Get "css/main.css" }}
185 {{ $opts := dict "minify" false }}
186 {{ with . | css.Build $opts }}
187 <link rel="stylesheet" href="{{ .RelPermalink }}" />
188 {{ end }}
189 {{ end }}
190 -- layouts/all.html --
191 All. {{ partial "css.html" . }}
192 -- content/p1.md --
193 -- content/p2.md --
194 -- content/p3.md --
195
196
197 `
198
199 b := hugolib.TestRunning(t, files, hugolib.TestOptOsFs())
200
201 for range 3 {
202 b.AssertFileContent("public/css/main.css", ` background: red;`)
203 b.EditFileReplaceAll("layouts/_partials/css.html", `"minify" false`, `"minify" true`).Build()
204 b.AssertFileContent("public/css/main.css", `{background:red}`)
205 b.EditFileReplaceAll("layouts/_partials/css.html", `"minify" true`, `"minify" false`).Build()
206 }
207 }
208
209 func TestCSSBuildSourceMaps(t *testing.T) {
210 t.Parallel()
211
212 filesTemplate := `
213 -- hugo.toml --
214 -- assets/a/pixel.png --
215 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
216 -- assets/css/main.css --
217 @import "./foo.css";
218 @import "./bar.css" layer(mylayer);
219 @import "./bar.css" layer;
220 @import './baz.css' screen and (min-width: 800px);
221
222 .main {
223 background-color: red;
224 }
225 -- assets/css/foo.css --
226 p { background: red; }
227 -- assets/css/bar.css --
228 div { background: blue; }
229 -- assets/css/baz.css --
230 span { background: green; }
231 -- assets/css/qux.css --
232 article { background: yellow; }
233 -- layouts/home.html --
234 {{ with resources.Get "css/main.css" }}
235 {{ $opts := (dict "minify" MINIFY "sourceMap" "SOURCE_MAP" "sourcesContent" SOURCES_CONTENT "loaders" (dict ".png" "dataurl"))}}
236 {{ with . | css.Build $opts }}
237 <link rel="stylesheet" href="{{ .RelPermalink }}" />
238 {{ end }}
239 {{ end }}
240 `
241
242 var (
243 r *strings.Replacer
244 files string
245 b *hugolib.IntegrationTestBuilder
246 )
247
248 for _, minify := range []string{"true", "false"} {
249
250 r = strings.NewReplacer(
251 "SOURCE_MAP", "linked",
252 "SOURCES_CONTENT", "true",
253 "MINIFY", minify,
254 )
255
256 files = r.Replace(filesTemplate)
257
258 b = hugolib.Test(t, files, hugolib.TestOptOsFs())
259 b.AssertFileContent("public/css/main.css", "/*# sourceMappingURL=main.css.map */")
260 b.AssertFileContent("public/css/main.css.map",
261 `"sourcesContent":["`,
262 `"mappings":"`,
263 )
264
265 sources := esbuild.SourcesFromSourceMap(b.FileContent("public/css/main.css.map"))
266 // main.css + foo.css + bar.css + baz.css = 4 sources.
267 b.Assert(len(sources), qt.Equals, 4)
268 }
269
270 r = strings.NewReplacer(
271 "SOURCE_MAP", "external",
272 "SOURCES_CONTENT", "true",
273 "MINIFY", "false",
274 )
275
276 files = r.Replace(filesTemplate)
277
278 b = hugolib.Test(t, files, hugolib.TestOptOsFs())
279 b.AssertFileContent("public/css/main.css", "! sourceMappingURL")
280 b.AssertFileContent("public/css/main.css.map",
281 `"sourcesContent":["p { background: red; }"`,
282 "AAAA;AAAI,cAAY;AAAK",
283 )
284
285 r = strings.NewReplacer(
286 "SOURCE_MAP", "external",
287 "SOURCES_CONTENT", "false",
288 "MINIFY", "false",
289 )
290
291 files = r.Replace(filesTemplate)
292
293 b = hugolib.Test(t, files, hugolib.TestOptOsFs())
294 b.AssertFileContent("public/css/main.css.map",
295 `"sourcesContent":null,"`,
296 "AAAA;AAAI,cAAY;AAAK",
297 )
298
299 r = strings.NewReplacer(
300 "SOURCE_MAP", "inline",
301 "SOURCES_CONTENT", "false",
302 "MINIFY", "false",
303 )
304
305 files = r.Replace(filesTemplate)
306
307 b = hugolib.Test(t, files, hugolib.TestOptOsFs())
308 b.AssertFileExists("public/css/main.css.map", false)
309 b.AssertFileContent("public/css/main.css", "sourceMappingURL=data:application/json;base64,")
310 }
311
312 func TestCSSBuildMultihost(t *testing.T) {
313 t.Parallel()
314
315 files := `
316 -- hugo.toml --
317 disableKinds = ["taxonomy", "term"]
318 defaultContentLanguage = "en"
319 defaultContentLanguageInSubDir = true
320 [languages]
321 [languages.en]
322 baseURL = "https://example.en"
323 weight = 1
324 [languages.fr]
325 baseURL = "https://example.fr"
326 weight = 2
327 -- assets/a/pixel.png --
328 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
329 -- assets/css/main.css --
330 body {
331 background-image: url("a/pixel.png");
332 color: red;
333 }
334 -- layouts/home.html --
335 {{ with resources.Get "css/main.css" }}
336 {{ with . | css.Build (dict "minify" true) }}
337 CSS: {{ .RelPermalink }}|{{ .Content }}
338 {{ end }}
339 {{ end }}
340 `
341
342 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
343
344 for _, lang := range []string{"en", "fr"} {
345 b.AssertFileContent("public/"+lang+"/css/main.css", `/css/pixel-NJRUOINY.png`)
346 b.AssertFileExists("public/"+lang+"/css/pixel-NJRUOINY.png", true)
347 }
348 }
349
350 func TestCSSBuildLoadersDefault(t *testing.T) {
351 t.Parallel()
352
353 files := `
354 -- hugo.toml --
355 -- assets/a/pixel.png --
356 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
357 -- static/b/issue14619.png --
358 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
359 -- assets/css/main.css --
360 body {
361 background-image: url("a/pixel.png");
362 }
363 div {
364 background-image: url("static/b/issue14619.png");
365 }
366 -- layouts/home.html --
367 {{ with resources.Get "css/main.css" }}
368 {{ with . | css.Build (dict "minify" true) }}
369 <link rel="stylesheet" href="{{ .RelPermalink }}" />
370 {{ end }}
371 {{ end }}
372 `
373
374 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
375 b.AssertFileContent("public/css/main.css", `/css/pixel-NJRUOINY.png`)
376 b.AssertFileExists("public/css/pixel-NJRUOINY.png", true)
377 b.AssertFileContent("public/css/main.css", `url("/css/issue14619-NJRUOINY.png")`)
378 b.AssertFileExists("public/css/issue14619-NJRUOINY.png", true)
379 }
380
381 // Issue #14849
382 func TestCSSBuildFileLoaderURLRelativeToWebContextRoot(t *testing.T) {
383 t.Parallel()
384
385 filesTemplate := `
386 -- hugo.toml --
387 BASEURL
388 disableKinds = ['rss','section','sitemap','taxonomy','term']
389 -- assets/css/main.css --
390 @import "components/header.css";
391 @import "/fonts/comic_neue/fonts.css";
392 body { color: #222; }
393 -- assets/css/components/header.css --
394 header { border-bottom: 1px solid #222; }
395 -- assets/fonts/comic_neue/fonts.css --
396 @font-face {
397 font-family: 'Comic Neue';
398 src: url(ComicNeue-Regular.ttf) format('truetype');
399 }
400 -- assets/fonts/comic_neue/ComicNeue-Regular.ttf --
401 fakefontdata
402 -- layouts/home.html --
403 {{ with resources.Get "css/main.css" }}
404 {{ with . | css.Build (dict "minify" true) }}
405 INLINE: <style>{{ .Content | safeCSS }}</style>
406 LINKED: <link rel="stylesheet" href="{{ .RelPermalink }}" />
407 {{ end }}
408 {{ end }}
409 `
410
411 // Default baseURL: URLs in inlined CSS must be resolvable from any page,
412 // not just relative to the (non-existent) /css/main.css location.
413 files := strings.ReplaceAll(filesTemplate, "BASEURL", "")
414 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
415 b.AssertFileContent("public/index.html", `url("/css/ComicNeue-Regular-`)
416 b.AssertFileContent("public/css/main.css", `url("/css/ComicNeue-Regular-`)
417
418 // baseURL with a subpath: URLs must include that subpath.
419 files = strings.ReplaceAll(filesTemplate, "BASEURL", `baseURL = "https://example.org/mysite/"`)
420 b = hugolib.Test(t, files, hugolib.TestOptOsFs())
421 b.AssertFileContent("public/index.html", `url("/mysite/css/ComicNeue-Regular-`)
422 b.AssertFileContent("public/css/main.css", `url("/mysite/css/ComicNeue-Regular-`)
423 }
424
425 // Issue #14849
426 func TestCSSBuildFileLoaderURLRelativeToWebContextRootMultihost(t *testing.T) {
427 t.Parallel()
428
429 // The CSS is built once for all hosts. The deepest base path among the
430 // hosts is baked into the file-loader URLs so the URL is reachable on every
431 // host, and the artifact is published at a per-host compensated path so the
432 // URL resolves correctly on each host. EN_WEIGHT/FR_WEIGHT swap which host
433 // renders first, to verify the result is independent of build order.
434 filesTemplate := `
435 -- hugo.toml --
436 defaultContentLanguage = "en"
437 defaultContentLanguageInSubdir = false
438 disableKinds = ['rss','section','sitemap','taxonomy','term']
439 [languages]
440 [languages.en]
441 baseURL = "https://example.com/docs/"
442 weight = EN_WEIGHT
443 [languages.fr]
444 baseURL = "https://example.fr/"
445 weight = FR_WEIGHT
446 -- assets/css/main.css --
447 @import "/fonts/comic_neue/fonts.css";
448 body { color: #222; }
449 -- assets/fonts/comic_neue/fonts.css --
450 @font-face {
451 font-family: 'Comic Neue';
452 src: url(ComicNeue-Regular.ttf) format('truetype');
453 }
454 -- assets/fonts/comic_neue/ComicNeue-Regular.ttf --
455 fakefontdata
456 -- layouts/home.html --
457 {{ with resources.Get "css/main.css" }}
458 {{ with . | css.Build (dict "minify" true) }}
459 INLINE: <style>{{ .Content | safeCSS }}</style>
460 LINKED: <link rel="stylesheet" href="{{ .RelPermalink }}" />
461 {{ range .Data.Artifacts }}ARTIFACT: {{ .RelPermalink }}|{{ .MediaType.Type }}
462 {{ end }}
463 {{ end }}
464 {{ end }}
465 `
466
467 assertResult := func(t *testing.T, b *hugolib.IntegrationTestBuilder) {
468 t.Helper()
469 b.Assert(b.H.Conf.IsMultihost(), qt.Equals, true)
470 b.AssertFileContent("public/en/index.html", `url("/docs/css/ComicNeue-Regular-`)
471 b.AssertFileContent("public/en/css/main.css", `url("/docs/css/ComicNeue-Regular-`)
472 b.AssertFileContent("public/fr/index.html", `url("/docs/css/ComicNeue-Regular-`)
473 b.AssertFileContent("public/fr/css/main.css", `url("/docs/css/ComicNeue-Regular-`)
474 b.AssertPublishDir("en/css/ComicNeue-Regular-UA4ODE7N.ttf")
475 b.AssertPublishDir("fr/docs/css/ComicNeue-Regular-UA4ODE7N.ttf")
476 // The artifact URL is the same on every host. See issue #15173.
477 b.AssertFileContent("public/en/index.html", "ARTIFACT: /docs/css/ComicNeue-Regular-UA4ODE7N.ttf|font/ttf")
478 b.AssertFileContent("public/fr/index.html", "ARTIFACT: /docs/css/ComicNeue-Regular-UA4ODE7N.ttf|font/ttf")
479 }
480
481 t.Run("en first", func(t *testing.T) {
482 t.Parallel()
483 files := strings.NewReplacer("EN_WEIGHT", "10", "FR_WEIGHT", "20").Replace(filesTemplate)
484 assertResult(t, hugolib.Test(t, files, hugolib.TestOptOsFs()))
485 })
486
487 t.Run("fr first", func(t *testing.T) {
488 t.Parallel()
489 files := strings.NewReplacer("EN_WEIGHT", "20", "FR_WEIGHT", "10").Replace(filesTemplate)
490 assertResult(t, hugolib.Test(t, files, hugolib.TestOptOsFs()))
491 })
492 }
493
494 // Issue #15173.
495 func TestCSSBuildDataArtifacts(t *testing.T) {
496 t.Parallel()
497
498 files := `
499 -- hugo.toml --
500 baseURL = "https://example.org/mysite/"
501 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
502 -- assets/css/main.css --
503 @import "/fonts/fonts.css";
504 body { color: #222; }
505 -- assets/fonts/fonts.css --
506 @font-face {
507 font-family: 'Comic Neue';
508 src: url(ComicNeue-Regular.woff2) format('woff2'), url(ComicNeue-Regular.ttf) format('truetype');
509 }
510 -- assets/fonts/ComicNeue-Regular.ttf --
511 fakefontdata
512 -- assets/fonts/ComicNeue-Regular.woff2 --
513 fakefontdata2
514 -- layouts/home.html --
515 {{ with resources.Get "css/main.css" }}
516 {{ with . | css.Build (dict "minify" true "sourcemap" "external") }}
517 {{ range .Data.Artifacts }}
518 ARTIFACT: {{ .RelPermalink }}|{{ .Permalink }}|{{ .MediaType.Type }}
519 {{ end }}
520 {{ range .Data.Artifacts }}{{ if eq .MediaType.MainType "font" }}
521 <link rel="preload" href="{{ .RelPermalink }}" as="font" type="{{ .MediaType.Type }}" crossorigin>
522 {{ end }}{{ end }}
523 <link rel="stylesheet" href="{{ .RelPermalink }}">
524 {{ end }}
525 {{ end }}
526 `
527
528 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
529 b.AssertFileContent("public/index.html",
530 "ARTIFACT: /mysite/css/main.css.map|https://example.org/mysite/css/main.css.map|application/source-map",
531 ".ttf|https://example.org/mysite/css/ComicNeue-Regular-",
532 ".woff2|https://example.org/mysite/css/ComicNeue-Regular-",
533 "|font/ttf",
534 "|font/woff2",
535 `as="font" type="font/woff2" crossorigin`,
536 )
537 b.AssertFileExists("public/css/main.css.map", true)
538 }
539
540 func TestCSSBuildDataArtifactsAndThenFingerprint(t *testing.T) {
541 t.Parallel()
542
543 files := `
544 -- hugo.toml --
545 baseURL = "https://example.org/mysite/"
546 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
547 -- assets/css/main.css --
548 @import "/fonts/fonts.css";
549 body { color: #222; }
550 -- assets/fonts/fonts.css --
551 @font-face {
552 font-family: 'Comic Neue';
553 src: url(ComicNeue-Regular.woff2) format('woff2'), url(ComicNeue-Regular.ttf) format('truetype');
554 }
555 -- assets/fonts/ComicNeue-Regular.ttf --
556 fakefontdata
557 -- assets/fonts/ComicNeue-Regular.woff2 --
558 fakefontdata2
559 -- layouts/home.html --
560 {{ with resources.Get "css/main.css" }}
561 ORIG CSS SHA256: {{ .Content | crypto.Hash "sha256" }}|
562 {{ with . | css.Build (dict "minify" true) }}
563 {{ range .Data.Artifacts }}
564 ARTIFACT: {{ .RelPermalink }}|{{ .Permalink }}|{{ .MediaType.Type }}
565 {{ end }}
566 BUILT CSS SHA256: {{ .Content | crypto.Hash "sha256" }}|
567 {{ with . | fingerprint }}
568 FINGERPRINTED: {{ .RelPermalink }}|
569 {{ end }}
570 {{ end }}
571 {{ end }}
572 `
573
574 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
575 b.AssertFileContent("public/index.html",
576 "ARTIFACT: /mysite/css/ComicNeue-Regular-UA4ODE7N.ttf|",
577 "ORIG CSS SHA256: a2b878c05df0f346c3854caebd21f00bb0965b46628440b8ccb31ce675af6bfe|",
578 "BUILT CSS SHA256: 5f8757d5579386b8755e0df759c7eecfd069c385294dfe7bc3695862d7f218b1|",
579 "FINGERPRINTED: /mysite/css/main.5f8757d5579386b8755e0df759c7eecfd069c385294dfe7bc3695862d7f218b1.css|",
580 )
581 }
582
583 // Issue #14623
584 func TestCSSBuildLoadersPartial(t *testing.T) {
585 t.Parallel()
586
587 files := `
588 -- hugo.toml --
589 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
590 -- layouts/home.html --
591 {{ $opts := dict "loaders" (dict ".svg" "dataurl") }}
592 {{ with resources.Get "css/main.css" | css.Build $opts }}
593 <link rel="stylesheet" href="{{ .RelPermalink }}">
594 {{ end }}
595 -- assets/css/main.css --
596 body { background-image: url('images/pixel.png'); }
597 div { background-image: url('images/foo.svg'); }
598 -- assets/images/pixel.png --
599 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
600 -- assets/images/foo.svg --
601 SVG file.
602 `
603 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
604 b.AssertFileExists("public/css/pixel-NJRUOINY.png", true)
605 b.AssertFileContent("public/css/main.css", `url(data:image/svg`) // svg should be inlined as dataurl.
606 b.AssertFileContent("public/css/main.css", `pixel-NJRUOINY.png`) // png should be referenced as a hashed file, not inlined.
607 }
608
609 func TestCSSBuildBootstrapFromNPM(t *testing.T) {
610 htesting.SkipSlowTestUnlessCI(t)
611 t.Parallel()
612
613 files := `
614 -- hugo.toml --
615 disableKinds = ["taxonomy", "term"]
616 -- assets/css/main.css --
617 @import "bootstrap";
618 -- package.json --
619 {
620 "devDependencies": {
621 "bootstrap": "5.3.8"
622 }
623 }
624 -- layouts/home.html --
625 {{ with resources.Get "css/main.css" }}
626 {{ with . | css.Build (dict "minify" true) }}
627 CSS size: {{ .Content | len }}|{{ .RelPermalink }}
628 {{ end }}
629 {{ end }}
630 `
631
632 b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall())
633 b.AssertFileContent("public/css/main.css", `--bs-indigo: #6610f2;`)
634 }
635
636 func TestCSSBuildVars(t *testing.T) {
637 t.Parallel()
638
639 files := `
640 -- hugo.toml --
641 -- assets/css/main.css --
642 @import "hugo:vars";
643
644 body {
645 background-color: var(--primary-color);
646 font-size: var(--font-size);
647 color: var(--text-color);
648 }
649 -- layouts/home.html --
650 {{ with resources.Get "css/main.css" }}
651 {{ $opts := dict
652 "vars" (dict "primary-color" "blue" "font-size" "24px" "text-color" "#333" "--already-prefixed" "red")
653 }}
654 {{ with . | css.Build $opts }}
655 <link rel="stylesheet" href="{{ .RelPermalink }}" />
656 {{ end }}
657 {{ end }}
658 `
659
660 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
661 b.AssertFileContent("public/css/main.css",
662 "--primary-color: blue;",
663 "--font-size: 24px;",
664 "--text-color: #333;",
665 "--already-prefixed: red;",
666 "background-color: var(--primary-color)",
667 )
668 }
669
670 func TestCSSBuildVarsNestedIssue14705(t *testing.T) {
671 t.Parallel()
672
673 files := `
674 -- hugo.toml --
675 -- assets/css/main.css --
676 @import "hugo:vars";
677 @import "hugo:vars/mobile" (max-width: 650px);
678
679 body {
680 background-color: var(--primary-color);
681 }
682 -- layouts/home.html --
683 {{ with resources.Get "css/main.css" }}
684 {{ $opts := dict
685 "vars" (dict "primary-color" "blue" "mobile" (dict "primary-color" "red" "font-size" "12px"))
686 }}
687 {{ with . | css.Build $opts }}
688 <link rel="stylesheet" href="{{ .RelPermalink }}" />
689 {{ end }}
690 {{ end }}
691 `
692
693 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
694 b.AssertFileContent("public/css/main.css",
695 "--primary-color: blue;",
696 "@media (max-width: 650px)",
697 "--primary-color: red;",
698 "! --mobile:",
699 "--font-size: 12px;",
700 "background-color: var(--primary-color)",
701 )
702 }
703
704 func TestCSSBuildVarsEmpty(t *testing.T) {
705 t.Parallel()
706
707 files := `
708 -- hugo.toml --
709 -- assets/css/main.css --
710 @import "hugo:vars";
711
712 body {
713 background-color: red;
714 }
715 -- layouts/home.html --
716 {{ with resources.Get "css/main.css" }}
717 {{ with . | css.Build }}
718 <link rel="stylesheet" href="{{ .RelPermalink }}" />
719 {{ end }}
720 {{ end }}
721 `
722
723 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
724 b.AssertFileContent("public/css/main.css", "background-color: red;")
725 }
726
727 func TestCSSBuildVarsNestedUpperCase(t *testing.T) {
728 t.Parallel()
729
730 filesTemplate := `
731 -- hugo.toml --
732 -- assets/css/main.css --
733 @import "hugo:vars";
734 @import "hugo:vars/MOBILE1" (max-width: 650px);
735
736 body {
737 background-color: var(--primary-color);
738 }
739 -- layouts/home.html --
740 {{ with resources.Get "css/main.css" }}
741 {{ $opts := dict
742 "vars" (dict "primary-color" "blue" "MOBILE2" (dict "primary-color" "red" "font-size" "12px" "MixedCaseKey" "value"))
743 }}
744 {{ with . | css.Build $opts }}
745 <link rel="stylesheet" href="{{ .RelPermalink }}" />
746 {{ end }}
747 {{ end }}
748 `
749
750 files := strings.ReplaceAll(filesTemplate, "MOBILE1", "Mobile")
751 files = strings.ReplaceAll(files, "MOBILE2", "mobile")
752 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
753 b.AssertFileContent("public/css/main.css", "primary-color: red;")
754
755 files = strings.ReplaceAll(filesTemplate, "MOBILE1", "mobile")
756 files = strings.ReplaceAll(files, "MOBILE2", "MobilE")
757 b = hugolib.Test(t, files, hugolib.TestOptOsFs())
758 b.AssertFileContent("public/css/main.css", "primary-color: red;")
759 }
760
761 func TestCSSBuildVarsQuoted(t *testing.T) {
762 t.Parallel()
763
764 files := `
765 -- hugo.toml --
766 -- assets/css/main.css --
767 @import "hugo:vars";
768
769 body {
770 font-family: var(--font-family);
771 color: var(--brand-color);
772 }
773 -- layouts/home.html --
774 {{ with resources.Get "css/main.css" }}
775 {{ $opts := dict
776 "vars" (dict "font-family" (css.Quoted "Arial, sans-serif") "brand-color" "hsl(0, 0%, 20%)")
777 }}
778 {{ with . | css.Build $opts }}
779 <link rel="stylesheet" href="{{ .RelPermalink }}" />
780 {{ end }}
781 {{ end }}
782 `
783
784 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
785 b.AssertFileContent("public/css/main.css",
786 `--font-family: "Arial, sans-serif";`,
787 "--brand-color: hsl(0, 0%, 20%);",
788 )
789 }
790
791 func TestCSSBuildVarsFromParams(t *testing.T) {
792 t.Parallel()
793
794 files := `
795 -- hugo.toml --
796 [params.styles]
797 primary-color = "blue"
798 font-size = "24px"
799 text-color = "#333"
800 -- assets/css/main.css --
801 @import "hugo:vars";
802 -- assets/css/main.css --
803 @import "hugo:vars";
804
805 body {
806 background-color: var(--primary-color);
807 font-size: var(--font-size);
808 color: var(--text-color);
809 }
810 -- layouts/home.html --
811 {{ with resources.Get "css/main.css" }}
812 {{ $opts := dict
813 "vars" site.Params.styles
814 }}
815 {{ with . | css.Build $opts }}
816 <link rel="stylesheet" href="{{ .RelPermalink }}" />
817 {{ end }}
818 {{ end }}
819 `
820
821 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
822 b.AssertFileContent("public/css/main.css",
823 "--primary-color: blue;",
824 "--font-size: 24px;",
825 "--text-color: #333;",
826 )
827 }
828
829 func TestCSSBuildImportContext(t *testing.T) {
830 t.Parallel()
831
832 files := `
833 -- hugo.toml --
834 -- assets/foo.css --
835 body { color: orange; }
836 -- assets/css/main.css --
837 @import "foo.css" screen;
838 @import "bar.css" print;
839 p {
840 font-color: red;
841 }
842 -- layouts/home.html --
843 {{ $foo := resources.FromString "foo.css" "body { color: blue; }" }}
844 {{ $bar := resources.FromString "bar.css" "body { color: green; }" }}
845 {{/* resource.Resources implements the resources.ResourceGetter interface (the type of importContext). */}}
846 {{ $resources := slice $foo $bar}}
847 {{ $opts := dict "minify" true "importContext" $resources }}
848 {{ $css := resources.Get "css/main.css" | css.Build $opts }}
849 CSS: {{ $css.RelPermalink }}|
850 `
851
852 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
853
854 b.AssertFileContent("public/css/main.css", `@media screen{body{color:#00f}}@media print{body{color:green}}p{font-color:red}`)
855 }
856
857 func TestCSSBuildImportContextChromaStyles(t *testing.T) {
858 t.Parallel()
859
860 files := `
861 -- hugo.toml --
862 disableKinds = ["taxonomy", "term", "rss", "sitemap", "section", "page"]
863 [markup.highlight]
864 style = "monokai"
865 -- assets/css/components/all.css --
866 @import "./chroma-light.css";
867 @import "./chroma-dark.css" (prefers-color-scheme: dark);
868 -- assets/css/main.css --
869 @import "./components/all.css";
870
871 :root {
872 color-scheme: light dark;
873 }
874
875 -- layouts/home.html --
876 {{ $light := css.ChromaStyles (dict "targetPath" "css/components/chroma-light.css" "style" "github" "mode" "light") }}
877 {{ $dark := css.ChromaStyles (dict "targetPath" "css/components/chroma-dark.css" "style" "github" "mode" "dark") }}
878 {{ $opts := dict "minify" true "importContext" (slice $light $dark) }}
879 {{ $css := resources.Get "css/main.css" | css.Build $opts }}
880 CSS: {{ $css.RelPermalink }}|
881 `
882
883 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
884
885 b.AssertFileContent("public/index.html", "CSS: /css/main.css|")
886 b.AssertFileContent("public/css/main.css", ".bg{background-color:#f7f7f7}.chroma", "@media(prefers-color-scheme:dark){.bg{color:#e6edf3")
887 }
888
889 func TestCSSBuildImportContextHashes(t *testing.T) {
890 t.Parallel()
891
892 files := `
893 -- hugo.toml --
894 -- assets/foo.css --
895 body { color: orange; }
896 -- assets/css/main.css --
897 @import "foo.css" screen;
898 @import "bar.css" print;
899 p {
900 font-color: red;
901 }
902 -- layouts/home.html --
903 {{ $foo := resources.FromString "foo.css" "body { color: blue; }" }}|
904 {{ $foo2 := resources.FromString "foo2.css" "body { color: blue; }" }}|
905 {{ $foo3 := resources.FromString "foo.css" "body { color: indigo; }" }}|
906 {{ $bar := resources.FromString "bar.css" "body { color: green; }" }}|
907 {{ $resources := slice $foo $bar}}
908 {{ $resourcesMount1 := $resources.Mount "a" "b"}}
909 {{ $resourcesMount2 := $resources.Mount "a" "c"}}
910 {{ $opts := dict "minify" true "importContext" $resources }}
911 foo: {{ $foo | testinginternal.HashString }}|
912 foo2: {{ $foo2 | testinginternal.HashString }}|
913 foo3: {{ $foo3 | testinginternal.HashString }}|
914 bar: {{ $bar | testinginternal.HashString }}|
915 resources: {{ $resources | testinginternal.HashString }}|
916 resourcesMount1: {{ $resourcesMount1 | testinginternal.HashString }}|
917 resourcesMount2: {{ $resourcesMount2 | testinginternal.HashString }}|
918 resources namespace: {{ resources | testinginternal.HashString }}|
919 opts: {{ $opts | testinginternal.HashString }}|
920 home: {{ . | testinginternal.HashString }}|{{ .Key }}|
921 slice with Page: {{ slice $foo $bar . | testinginternal.HashString }}|
922 map with Page: {{ dict "foo" $foo "bar" $bar "page" . | testinginternal.HashString }}|
923 {{ $css := resources.Get "css/main.css" | css.Build $opts -}}
924 css: {{ $css | testinginternal.HashString }}|
925 cached1: {{ testinginternal.NewCachedResourceGetter $resources resources | testinginternal.HashString}}
926 cached2: {{ testinginternal.NewCachedResourceGetter $resources | testinginternal.HashString}}
927 cached3: {{ testinginternal.NewCachedResourceGetter $resources resources $resourcesMount1 $resourcesMount2 | testinginternal.HashString}}
928
929
930 `
931
932 for range 2 {
933
934 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
935
936 b.AssertFileContent("public/index.html", `
937 foo: 17610550322594361312|
938 foo2: 12741631597870435822|
939 foo3: 4488319114298108946|
940 bar: 11981658863051989001|
941 resources: 10963320145636139629|
942 resourcesMount1: 7843365040860281860|
943 resourcesMount2: 10632811173795339581|
944 resources namespace: 12714111578321948564|
945 opts: 7538269917289793785|
946 css: 8075855322897706813|
947 home: 166103792234269065|/html|
948 slice with Page: 7769684332502419906|
949 map with Page: 10393165700333352395|
950 cached1: 7064840587060576200
951 cached2: 6730658186132701788
952 cached3: 13761938017162285970
953 `)
954
955 }
956 }
957
958 func TestCSSBuildImportContextEdit(t *testing.T) {
959 t.Parallel()
960
961 files := `
962 -- hugo.toml --
963 disableLiveReload = true
964 -- assets/css/common/baz.css --
965 body { color: purple; }
966 -- assets/css/main.css --
967 @import "foo.css" screen;
968 @import "bar.css" print;
969 @import "baz.css" screen and (min-width: 768px);
970 p {
971 font-color: red;
972 }
973 -- layouts/home.html --
974 {{ $foo := resources.FromString "foo.css" "body { color: blue; }" }}
975 {{ $bar := resources.FromString "bar.css" "body { color: green; }" }}
976 {{ $common := resources.Match "/css/common/*.css" }}
977 {{ $commonMount := ($common.Mount "/css/common" ".")}}
978 {{ $resources := slice $foo $bar }}
979 {{ $opts := dict "minify" true "importContext" (slice $resources $commonMount) }}
980 resources: {{ $resources | testinginternal.HashString }}|
981 {{ $css := resources.Get "css/main.css" | css.Build $opts }}
982 CSS: {{ $css.RelPermalink }}|
983 `
984
985 b := hugolib.TestRunning(t, files, hugolib.TestOptOsFs())
986
987 b.AssertFileContent("public/css/main.css", `
988 @media screen{body{color:#00f}}@media print{body{color:green}}
989 @media screen and (min-width:768px){body{color:purple}}
990 `)
991
992 // Edit resource built with resources.FromString.
993 b.EditFileReplaceAll("layouts/home.html", "color: green", "color: yellow").Build()
994 b.AssertFileContent("public/css/main.css", `
995 @media screen{body{color:#00f}}@media print{body{color:#ff0}}
996 @media screen and (min-width:768px){body{color:purple}}
997 `)
998
999 // Edit resource in the mounted slice.
1000 b.EditFileReplaceAll("assets/css/common/baz.css", "purple", "orange").Build()
1001 b.AssertFileContent("public/css/main.css", `
1002 @media screen{body{color:#00f}}@media print{body{color:#ff0}}
1003 @media screen and (min-width:768px){body{color:orange}}
1004 `)
1005 }