mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-29 22:29:56 +02:00
tocss: Add vars option
This commit adds a new `vars` option to both the Sass transpilers (Dart Sass and Libsass). This means that you can pass a map with key/value pairs to the transpiler: ```handlebars {{ $vars := dict "$color1" "blue" "$color2" "green" "$font_size" "24px" }} {{ $cssOpts := (dict "transpiler" "dartsass" "outputStyle" "compressed" "vars" $vars ) }} {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts }} ``` And the the variables will be available in the `hugo:vars` namespace. Example usage for Dart Sass: ```scss @use "hugo:vars" as v; p { color: v.$color1; font-size: v.$font_size; } ``` Note that Libsass does not support the `use` keyword, so you need to `import` them as global variables: ```scss @import "hugo:vars"; p { color: $color1; font-size: $font_size; } ``` Hugo will: * Add a missing leading `$` for the variable names if needed. * Wrap the values in `unquote('VALUE')` (Sass built-in) to get proper handling of identifiers vs other strings. This means that you can pull variables directly from e.g. the site config: ```toml [params] [params.sassvars] color1 = "blue" color2 = "green" font_size = "24px" image = "images/hero.jpg" ``` ```handlebars {{ $vars := site.Params.sassvars}} {{ $cssOpts := (dict "transpiler" "dartsass" "outputStyle" "compressed" "vars" $vars ) }} {{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts }} ``` Fixes #10555
This commit is contained in:
@@ -93,6 +93,7 @@ func (c *Client) toCSS(args godartsass.Args, src io.Reader) (godartsass.Result,
|
||||
var res godartsass.Result
|
||||
|
||||
in := helpers.ReaderToString(src)
|
||||
|
||||
args.Source = in
|
||||
|
||||
res, err := c.transpiler.Execute(args)
|
||||
@@ -130,6 +131,11 @@ type Options struct {
|
||||
|
||||
// If enabled, sources will be embedded in the generated source map.
|
||||
SourceMapIncludeSources bool
|
||||
|
||||
// Vars will be available in 'hugo:vars', e.g:
|
||||
// @use "hugo:vars";
|
||||
// $color: vars.$color;
|
||||
Vars map[string]string
|
||||
}
|
||||
|
||||
func decodeOptions(m map[string]any) (opts Options, err error) {
|
||||
|
@@ -24,6 +24,7 @@ import (
|
||||
)
|
||||
|
||||
func TestTransformIncludePaths(t *testing.T) {
|
||||
t.Parallel()
|
||||
if !dartsass.Supports() {
|
||||
t.Skip()
|
||||
}
|
||||
@@ -55,6 +56,7 @@ T1: {{ $r.Content }}
|
||||
}
|
||||
|
||||
func TestTransformImportRegularCSS(t *testing.T) {
|
||||
t.Parallel()
|
||||
if !dartsass.Supports() {
|
||||
t.Skip()
|
||||
}
|
||||
@@ -108,6 +110,7 @@ T1: {{ $r.Content | safeHTML }}
|
||||
}
|
||||
|
||||
func TestTransformThemeOverrides(t *testing.T) {
|
||||
t.Parallel()
|
||||
if !dartsass.Supports() {
|
||||
t.Skip()
|
||||
}
|
||||
@@ -169,6 +172,7 @@ zoo {
|
||||
}
|
||||
|
||||
func TestTransformLogging(t *testing.T) {
|
||||
t.Parallel()
|
||||
if !dartsass.Supports() {
|
||||
t.Skip()
|
||||
}
|
||||
@@ -200,6 +204,7 @@ T1: {{ $r.Content }}
|
||||
}
|
||||
|
||||
func TestTransformErrors(t *testing.T) {
|
||||
t.Parallel()
|
||||
if !dartsass.Supports() {
|
||||
t.Skip()
|
||||
}
|
||||
@@ -271,3 +276,93 @@ T1: {{ $r.Content }}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestOptionVars(t *testing.T) {
|
||||
t.Parallel()
|
||||
if !dartsass.Supports() {
|
||||
t.Skip()
|
||||
}
|
||||
|
||||
files := `
|
||||
-- assets/scss/main.scss --
|
||||
@use "hugo:vars";
|
||||
|
||||
body {
|
||||
body {
|
||||
background: url(vars.$image) no-repeat center/cover;
|
||||
}
|
||||
}
|
||||
|
||||
p {
|
||||
color: vars.$color1;
|
||||
font-size: vars.$font_size;
|
||||
}
|
||||
|
||||
b {
|
||||
color: vars.$color2;
|
||||
}
|
||||
-- layouts/index.html --
|
||||
{{ $image := "images/hero.jpg" }}
|
||||
{{ $vars := dict "$color1" "blue" "$color2" "green" "font_size" "24px" "image" $image }}
|
||||
{{ $cssOpts := (dict "transpiler" "dartsass" "outputStyle" "compressed" "vars" $vars ) }}
|
||||
{{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts }}
|
||||
T1: {{ $r.Content }}
|
||||
`
|
||||
|
||||
b := hugolib.NewIntegrationTestBuilder(
|
||||
hugolib.IntegrationTestConfig{
|
||||
T: t,
|
||||
TxtarString: files,
|
||||
NeedsOsFS: true,
|
||||
}).Build()
|
||||
|
||||
b.AssertFileContent("public/index.html", `T1: body body{background:url(images/hero.jpg) no-repeat center/cover}p{color:blue;font-size:24px}b{color:green}`)
|
||||
}
|
||||
|
||||
func TestOptionVarsParams(t *testing.T) {
|
||||
t.Parallel()
|
||||
if !dartsass.Supports() {
|
||||
t.Skip()
|
||||
}
|
||||
|
||||
files := `
|
||||
-- config.toml --
|
||||
[params]
|
||||
[params.sassvars]
|
||||
color1 = "blue"
|
||||
color2 = "green"
|
||||
font_size = "24px"
|
||||
image = "images/hero.jpg"
|
||||
-- assets/scss/main.scss --
|
||||
@use "hugo:vars";
|
||||
|
||||
body {
|
||||
body {
|
||||
background: url(vars.$image) no-repeat center/cover;
|
||||
}
|
||||
}
|
||||
|
||||
p {
|
||||
color: vars.$color1;
|
||||
font-size: vars.$font_size;
|
||||
}
|
||||
|
||||
b {
|
||||
color: vars.$color2;
|
||||
}
|
||||
-- layouts/index.html --
|
||||
{{ $vars := site.Params.sassvars}}
|
||||
{{ $cssOpts := (dict "transpiler" "dartsass" "outputStyle" "compressed" "vars" $vars ) }}
|
||||
{{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts }}
|
||||
T1: {{ $r.Content }}
|
||||
`
|
||||
|
||||
b := hugolib.NewIntegrationTestBuilder(
|
||||
hugolib.IntegrationTestConfig{
|
||||
T: t,
|
||||
TxtarString: files,
|
||||
NeedsOsFS: true,
|
||||
}).Build()
|
||||
|
||||
b.AssertFileContent("public/index.html", `T1: body body{background:url(images/hero.jpg) no-repeat center/cover}p{color:blue;font-size:24px}b{color:green}`)
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
// Copyright 2020 The Hugo Authors. All rights reserved.
|
||||
// Copyright 2022 The Hugo Authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@@ -28,6 +28,7 @@ import (
|
||||
"github.com/gohugoio/hugo/resources"
|
||||
|
||||
"github.com/gohugoio/hugo/resources/internal"
|
||||
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/internal/sass"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
|
||||
@@ -84,6 +85,8 @@ func (t *transform) Transform(ctx *resources.ResourceTransformationCtx) error {
|
||||
ImportResolver: importResolver{
|
||||
baseDir: baseDir,
|
||||
c: t.c,
|
||||
|
||||
varsStylesheet: sass.CreateVarsStyleSheet(opts.Vars),
|
||||
},
|
||||
OutputStyle: godartsass.ParseOutputStyle(opts.OutputStyle),
|
||||
EnableSourceMap: opts.EnableSourceMap,
|
||||
@@ -128,9 +131,14 @@ func (t *transform) Transform(ctx *resources.ResourceTransformationCtx) error {
|
||||
type importResolver struct {
|
||||
baseDir string
|
||||
c *Client
|
||||
|
||||
varsStylesheet string
|
||||
}
|
||||
|
||||
func (t importResolver) CanonicalizeURL(url string) (string, error) {
|
||||
if url == sass.HugoVarsNamespace {
|
||||
return url, nil
|
||||
}
|
||||
filePath, isURL := paths.UrlToFilename(url)
|
||||
var prevDir string
|
||||
var pathDir string
|
||||
@@ -177,6 +185,9 @@ func (t importResolver) CanonicalizeURL(url string) (string, error) {
|
||||
}
|
||||
|
||||
func (t importResolver) Load(url string) (string, error) {
|
||||
if url == sass.HugoVarsNamespace {
|
||||
return t.varsStylesheet, nil
|
||||
}
|
||||
filename, _ := paths.UrlToFilename(url)
|
||||
b, err := afero.ReadFile(hugofs.Os, filename)
|
||||
return string(b), err
|
||||
|
Reference in New Issue
Block a user