Debian Codegraph @

tpl/css/build_integration_test.go

raw ◀ ▶
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 t.Skip("This tests needs internet access")
611 htesting.SkipSlowTestUnlessCI(t)
612 t.Parallel()
613
614 files := `
615 -- hugo.toml --
616 disableKinds = ["taxonomy", "term"]
617 -- assets/css/main.css --
618 @import "bootstrap";
619 -- package.json --
620 {
621 "devDependencies": {
622 "bootstrap": "5.3.8"
623 }
624 }
625 -- layouts/home.html --
626 {{ with resources.Get "css/main.css" }}
627 {{ with . | css.Build (dict "minify" true) }}
628 CSS size: {{ .Content | len }}|{{ .RelPermalink }}
629 {{ end }}
630 {{ end }}
631 `
632
633 b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall())
634 b.AssertFileContent("public/css/main.css", `--bs-indigo: #6610f2;`)
635 }
636
637 func TestCSSBuildVars(t *testing.T) {
638 t.Parallel()
639
640 files := `
641 -- hugo.toml --
642 -- assets/css/main.css --
643 @import "hugo:vars";
644
645 body {
646 background-color: var(--primary-color);
647 font-size: var(--font-size);
648 color: var(--text-color);
649 }
650 -- layouts/home.html --
651 {{ with resources.Get "css/main.css" }}
652 {{ $opts := dict
653 "vars" (dict "primary-color" "blue" "font-size" "24px" "text-color" "#333" "--already-prefixed" "red")
654 }}
655 {{ with . | css.Build $opts }}
656 <link rel="stylesheet" href="{{ .RelPermalink }}" />
657 {{ end }}
658 {{ end }}
659 `
660
661 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
662 b.AssertFileContent("public/css/main.css",
663 "--primary-color: blue;",
664 "--font-size: 24px;",
665 "--text-color: #333;",
666 "--already-prefixed: red;",
667 "background-color: var(--primary-color)",
668 )
669 }
670
671 func TestCSSBuildVarsNestedIssue14705(t *testing.T) {
672 t.Parallel()
673
674 files := `
675 -- hugo.toml --
676 -- assets/css/main.css --
677 @import "hugo:vars";
678 @import "hugo:vars/mobile" (max-width: 650px);
679
680 body {
681 background-color: var(--primary-color);
682 }
683 -- layouts/home.html --
684 {{ with resources.Get "css/main.css" }}
685 {{ $opts := dict
686 "vars" (dict "primary-color" "blue" "mobile" (dict "primary-color" "red" "font-size" "12px"))
687 }}
688 {{ with . | css.Build $opts }}
689 <link rel="stylesheet" href="{{ .RelPermalink }}" />
690 {{ end }}
691 {{ end }}
692 `
693
694 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
695 b.AssertFileContent("public/css/main.css",
696 "--primary-color: blue;",
697 "@media (max-width: 650px)",
698 "--primary-color: red;",
699 "! --mobile:",
700 "--font-size: 12px;",
701 "background-color: var(--primary-color)",
702 )
703 }
704
705 func TestCSSBuildVarsEmpty(t *testing.T) {
706 t.Parallel()
707
708 files := `
709 -- hugo.toml --
710 -- assets/css/main.css --
711 @import "hugo:vars";
712
713 body {
714 background-color: red;
715 }
716 -- layouts/home.html --
717 {{ with resources.Get "css/main.css" }}
718 {{ with . | css.Build }}
719 <link rel="stylesheet" href="{{ .RelPermalink }}" />
720 {{ end }}
721 {{ end }}
722 `
723
724 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
725 b.AssertFileContent("public/css/main.css", "background-color: red;")
726 }
727
728 func TestCSSBuildVarsNestedUpperCase(t *testing.T) {
729 t.Parallel()
730
731 filesTemplate := `
732 -- hugo.toml --
733 -- assets/css/main.css --
734 @import "hugo:vars";
735 @import "hugo:vars/MOBILE1" (max-width: 650px);
736
737 body {
738 background-color: var(--primary-color);
739 }
740 -- layouts/home.html --
741 {{ with resources.Get "css/main.css" }}
742 {{ $opts := dict
743 "vars" (dict "primary-color" "blue" "MOBILE2" (dict "primary-color" "red" "font-size" "12px" "MixedCaseKey" "value"))
744 }}
745 {{ with . | css.Build $opts }}
746 <link rel="stylesheet" href="{{ .RelPermalink }}" />
747 {{ end }}
748 {{ end }}
749 `
750
751 files := strings.ReplaceAll(filesTemplate, "MOBILE1", "Mobile")
752 files = strings.ReplaceAll(files, "MOBILE2", "mobile")
753 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
754 b.AssertFileContent("public/css/main.css", "primary-color: red;")
755
756 files = strings.ReplaceAll(filesTemplate, "MOBILE1", "mobile")
757 files = strings.ReplaceAll(files, "MOBILE2", "MobilE")
758 b = hugolib.Test(t, files, hugolib.TestOptOsFs())
759 b.AssertFileContent("public/css/main.css", "primary-color: red;")
760 }
761
762 func TestCSSBuildVarsQuoted(t *testing.T) {
763 t.Parallel()
764
765 files := `
766 -- hugo.toml --
767 -- assets/css/main.css --
768 @import "hugo:vars";
769
770 body {
771 font-family: var(--font-family);
772 color: var(--brand-color);
773 }
774 -- layouts/home.html --
775 {{ with resources.Get "css/main.css" }}
776 {{ $opts := dict
777 "vars" (dict "font-family" (css.Quoted "Arial, sans-serif") "brand-color" "hsl(0, 0%, 20%)")
778 }}
779 {{ with . | css.Build $opts }}
780 <link rel="stylesheet" href="{{ .RelPermalink }}" />
781 {{ end }}
782 {{ end }}
783 `
784
785 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
786 b.AssertFileContent("public/css/main.css",
787 `--font-family: "Arial, sans-serif";`,
788 "--brand-color: hsl(0, 0%, 20%);",
789 )
790 }
791
792 func TestCSSBuildVarsFromParams(t *testing.T) {
793 t.Parallel()
794
795 files := `
796 -- hugo.toml --
797 [params.styles]
798 primary-color = "blue"
799 font-size = "24px"
800 text-color = "#333"
801 -- assets/css/main.css --
802 @import "hugo:vars";
803 -- assets/css/main.css --
804 @import "hugo:vars";
805
806 body {
807 background-color: var(--primary-color);
808 font-size: var(--font-size);
809 color: var(--text-color);
810 }
811 -- layouts/home.html --
812 {{ with resources.Get "css/main.css" }}
813 {{ $opts := dict
814 "vars" site.Params.styles
815 }}
816 {{ with . | css.Build $opts }}
817 <link rel="stylesheet" href="{{ .RelPermalink }}" />
818 {{ end }}
819 {{ end }}
820 `
821
822 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
823 b.AssertFileContent("public/css/main.css",
824 "--primary-color: blue;",
825 "--font-size: 24px;",
826 "--text-color: #333;",
827 )
828 }
829
830 func TestCSSBuildImportContext(t *testing.T) {
831 t.Parallel()
832
833 files := `
834 -- hugo.toml --
835 -- assets/foo.css --
836 body { color: orange; }
837 -- assets/css/main.css --
838 @import "foo.css" screen;
839 @import "bar.css" print;
840 p {
841 font-color: red;
842 }
843 -- layouts/home.html --
844 {{ $foo := resources.FromString "foo.css" "body { color: blue; }" }}
845 {{ $bar := resources.FromString "bar.css" "body { color: green; }" }}
846 {{/* resource.Resources implements the resources.ResourceGetter interface (the type of importContext). */}}
847 {{ $resources := slice $foo $bar}}
848 {{ $opts := dict "minify" true "importContext" $resources }}
849 {{ $css := resources.Get "css/main.css" | css.Build $opts }}
850 CSS: {{ $css.RelPermalink }}|
851 `
852
853 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
854
855 b.AssertFileContent("public/css/main.css", `@media screen{body{color:#00f}}@media print{body{color:green}}p{font-color:red}`)
856 }
857
858 func TestCSSBuildImportContextChromaStyles(t *testing.T) {
859 t.Parallel()
860
861 files := `
862 -- hugo.toml --
863 disableKinds = ["taxonomy", "term", "rss", "sitemap", "section", "page"]
864 [markup.highlight]
865 style = "monokai"
866 -- assets/css/components/all.css --
867 @import "./chroma-light.css";
868 @import "./chroma-dark.css" (prefers-color-scheme: dark);
869 -- assets/css/main.css --
870 @import "./components/all.css";
871
872 :root {
873 color-scheme: light dark;
874 }
875
876 -- layouts/home.html --
877 {{ $light := css.ChromaStyles (dict "targetPath" "css/components/chroma-light.css" "style" "github" "mode" "light") }}
878 {{ $dark := css.ChromaStyles (dict "targetPath" "css/components/chroma-dark.css" "style" "github" "mode" "dark") }}
879 {{ $opts := dict "minify" true "importContext" (slice $light $dark) }}
880 {{ $css := resources.Get "css/main.css" | css.Build $opts }}
881 CSS: {{ $css.RelPermalink }}|
882 `
883
884 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
885
886 b.AssertFileContent("public/index.html", "CSS: /css/main.css|")
887 b.AssertFileContent("public/css/main.css", ".bg{background-color:#f7f7f7}.chroma", "@media(prefers-color-scheme:dark){.bg{color:#e6edf3")
888 }
889
890 func TestCSSBuildImportContextHashes(t *testing.T) {
891 t.Parallel()
892
893 files := `
894 -- hugo.toml --
895 -- assets/foo.css --
896 body { color: orange; }
897 -- assets/css/main.css --
898 @import "foo.css" screen;
899 @import "bar.css" print;
900 p {
901 font-color: red;
902 }
903 -- layouts/home.html --
904 {{ $foo := resources.FromString "foo.css" "body { color: blue; }" }}|
905 {{ $foo2 := resources.FromString "foo2.css" "body { color: blue; }" }}|
906 {{ $foo3 := resources.FromString "foo.css" "body { color: indigo; }" }}|
907 {{ $bar := resources.FromString "bar.css" "body { color: green; }" }}|
908 {{ $resources := slice $foo $bar}}
909 {{ $resourcesMount1 := $resources.Mount "a" "b"}}
910 {{ $resourcesMount2 := $resources.Mount "a" "c"}}
911 {{ $opts := dict "minify" true "importContext" $resources }}
912 foo: {{ $foo | testinginternal.HashString }}|
913 foo2: {{ $foo2 | testinginternal.HashString }}|
914 foo3: {{ $foo3 | testinginternal.HashString }}|
915 bar: {{ $bar | testinginternal.HashString }}|
916 resources: {{ $resources | testinginternal.HashString }}|
917 resourcesMount1: {{ $resourcesMount1 | testinginternal.HashString }}|
918 resourcesMount2: {{ $resourcesMount2 | testinginternal.HashString }}|
919 resources namespace: {{ resources | testinginternal.HashString }}|
920 opts: {{ $opts | testinginternal.HashString }}|
921 home: {{ . | testinginternal.HashString }}|{{ .Key }}|
922 slice with Page: {{ slice $foo $bar . | testinginternal.HashString }}|
923 map with Page: {{ dict "foo" $foo "bar" $bar "page" . | testinginternal.HashString }}|
924 {{ $css := resources.Get "css/main.css" | css.Build $opts -}}
925 css: {{ $css | testinginternal.HashString }}|
926 cached1: {{ testinginternal.NewCachedResourceGetter $resources resources | testinginternal.HashString}}
927 cached2: {{ testinginternal.NewCachedResourceGetter $resources | testinginternal.HashString}}
928 cached3: {{ testinginternal.NewCachedResourceGetter $resources resources $resourcesMount1 $resourcesMount2 | testinginternal.HashString}}
929
930
931 `
932
933 for range 2 {
934
935 b := hugolib.Test(t, files, hugolib.TestOptOsFs())
936
937 b.AssertFileContent("public/index.html", `
938 foo: 17610550322594361312|
939 foo2: 12741631597870435822|
940 foo3: 4488319114298108946|
941 bar: 11981658863051989001|
942 resources: 10963320145636139629|
943 resourcesMount1: 7843365040860281860|
944 resourcesMount2: 10632811173795339581|
945 resources namespace: 12714111578321948564|
946 opts: 7538269917289793785|
947 css: 8075855322897706813|
948 home: 166103792234269065|/html|
949 slice with Page: 7769684332502419906|
950 map with Page: 10393165700333352395|
951 cached1: 7064840587060576200
952 cached2: 6730658186132701788
953 cached3: 13761938017162285970
954 `)
955
956 }
957 }
958
959 func TestCSSBuildImportContextEdit(t *testing.T) {
960 t.Parallel()
961
962 files := `
963 -- hugo.toml --
964 disableLiveReload = true
965 -- assets/css/common/baz.css --
966 body { color: purple; }
967 -- assets/css/main.css --
968 @import "foo.css" screen;
969 @import "bar.css" print;
970 @import "baz.css" screen and (min-width: 768px);
971 p {
972 font-color: red;
973 }
974 -- layouts/home.html --
975 {{ $foo := resources.FromString "foo.css" "body { color: blue; }" }}
976 {{ $bar := resources.FromString "bar.css" "body { color: green; }" }}
977 {{ $common := resources.Match "/css/common/*.css" }}
978 {{ $commonMount := ($common.Mount "/css/common" ".")}}
979 {{ $resources := slice $foo $bar }}
980 {{ $opts := dict "minify" true "importContext" (slice $resources $commonMount) }}
981 resources: {{ $resources | testinginternal.HashString }}|
982 {{ $css := resources.Get "css/main.css" | css.Build $opts }}
983 CSS: {{ $css.RelPermalink }}|
984 `
985
986 b := hugolib.TestRunning(t, files, hugolib.TestOptOsFs())
987
988 b.AssertFileContent("public/css/main.css", `
989 @media screen{body{color:#00f}}@media print{body{color:green}}
990 @media screen and (min-width:768px){body{color:purple}}
991 `)
992
993 // Edit resource built with resources.FromString.
994 b.EditFileReplaceAll("layouts/home.html", "color: green", "color: yellow").Build()
995 b.AssertFileContent("public/css/main.css", `
996 @media screen{body{color:#00f}}@media print{body{color:#ff0}}
997 @media screen and (min-width:768px){body{color:purple}}
998 `)
999
1000 // Edit resource in the mounted slice.
1001 b.EditFileReplaceAll("assets/css/common/baz.css", "purple", "orange").Build()
1002 b.AssertFileContent("public/css/main.css", `
1003 @media screen{body{color:#00f}}@media print{body{color:#ff0}}
1004 @media screen and (min-width:768px){body{color:orange}}
1005 `)
1006 }