diff options
Diffstat (limited to 'resources/resource_transformers/tocss/scss')
3 files changed, 57 insertions, 0 deletions
diff --git a/resources/resource_transformers/tocss/scss/client.go b/resources/resource_transformers/tocss/scss/client.go index ecaceaa6c..0d027d888 100644 --- a/resources/resource_transformers/tocss/scss/client.go +++ b/resources/resource_transformers/tocss/scss/client.go @@ -60,6 +60,10 @@ type Options struct { // When enabled, Hugo will generate a source map. EnableSourceMap bool + + // Vars will be available in 'hugo:vars', e.g: + // @import "hugo:vars"; + Vars map[string]string } func DecodeOptions(m map[string]any) (opts Options, err error) { diff --git a/resources/resource_transformers/tocss/scss/integration_test.go b/resources/resource_transformers/tocss/scss/integration_test.go index 13b664cc7..799c70ee5 100644 --- a/resources/resource_transformers/tocss/scss/integration_test.go +++ b/resources/resource_transformers/tocss/scss/integration_test.go @@ -25,6 +25,7 @@ import ( ) func TestTransformIncludePaths(t *testing.T) { + t.Parallel() if !scss.Supports() { t.Skip() } @@ -57,6 +58,7 @@ T1: {{ $r.Content }} } func TestTransformImportRegularCSS(t *testing.T) { + t.Parallel() if !scss.Supports() { t.Skip() } @@ -113,6 +115,7 @@ moo { } func TestTransformThemeOverrides(t *testing.T) { + t.Parallel() if !scss.Supports() { t.Skip() } @@ -175,6 +178,7 @@ zoo { } func TestTransformErrors(t *testing.T) { + t.Parallel() if !scss.Supports() { t.Skip() } @@ -245,3 +249,45 @@ T1: {{ $r.Content }} }) } + +func TestOptionVars(t *testing.T) { + t.Parallel() + if !scss.Supports() { + t.Skip() + } + + files := ` +-- assets/scss/main.scss -- +@import "hugo:vars"; + +body { + body { + background: url($image) no-repeat center/cover; + } +} + +p { + color: $color1; + font-size: var$font_size; +} + +b { + color: $color2; +} +-- layouts/index.html -- +{{ $image := "images/hero.jpg" }} +{{ $vars := dict "$color1" "blue" "$color2" "green" "font_size" "24px" "image" $image }} +{{ $cssOpts := (dict "transpiler" "libsass" "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:var 24px}b{color:green}`) +} diff --git a/resources/resource_transformers/tocss/scss/tocss.go b/resources/resource_transformers/tocss/scss/tocss.go index 10bd1f6f8..7e44f327e 100644 --- a/resources/resource_transformers/tocss/scss/tocss.go +++ b/resources/resource_transformers/tocss/scss/tocss.go @@ -31,6 +31,7 @@ import ( "github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/media" "github.com/gohugoio/hugo/resources" + "github.com/gohugoio/hugo/resources/resource_transformers/tocss/internal/sass" ) // Used in tests. This feature requires Hugo to be built with the extended tag. @@ -63,11 +64,17 @@ func (t *toCSSTransformation) Transform(ctx *resources.ResourceTransformationCtx } } + varsStylesheet := sass.CreateVarsStyleSheet(options.from.Vars) + // To allow for overrides of SCSS files anywhere in the project/theme hierarchy, we need // to help libsass revolve the filename by looking in the composite filesystem first. // We add the entry directories for both project and themes to the include paths list, but // that only work for overrides on the top level. options.to.ImportResolver = func(url string, prev string) (newUrl string, body string, resolved bool) { + if url == sass.HugoVarsNamespace { + return url, varsStylesheet, true + } + // We get URL paths from LibSASS, but we need file paths. url = filepath.FromSlash(url) prev = filepath.FromSlash(prev) |