Hi everyone, I’m looking for a way to config the SASS/SCSS variables via configuration file with @use
rule.
Motivation
I used to use ExecuteAsTemplate
the variables’ template, and then append the generated resource that contains configured vars via resources.Concat
, so that the others files can access the global SASS/SCSS variables.
Now the @import
rule was marked deprecated two years ago, may be removed in the next few years. I would like to use the @use
instead, but I don’t know if it’s possible. I’m unable to access the generated vars resource.
I think it could be done by overriding the assets (Hugo look up order), but this way is a little complicated compared with the configurable way for end users.
Error: Error building site: TOCSS-DART: failed to transform "hbs/scss/main.scss" (text/x-scss): "<stream>:1:1": Can't find stylesheet to import.
Code Snippet
I’ve also committed the codes to GitHub repo, see GitHub - razonyang/hugo-lab at scss-configurable-vars.
$ tree assets
└── hbs
└── scss
├── main.scss
└── vars.tmpl.scss
I got two assets that content as following.
$ cat assets/hbs/scss/vars.tmpl.scss
$heading-color: '{{ default "skyblue" .Site.Params.heading_color }}';
vars.tmpl.scss
is the template for generating vars from config file.
$ cat assets/hbs/scss/main.scss
@use 'vars';
.heading {
color: vars.$heading_color;
}
{{ $varsTmpl := resources.Get "hbs/scss/vars.tmpl.scss" }}
{{ $vars := $varsTmpl | resources.ExecuteAsTemplate "hbs/scss/_vars.scss" . }}
{{ $styles := slice $vars (resources.Get "hbs/scss/main.scss") }}
{{ $style := $styles | resources.Concat "css/hbs.css" | toCSS (dict
"transpiler" "dartsass"
) }}
<link href="{{ $style.Permalink }}" rel="stylesheet">
The hard part is that I don’t know how to @use
the generated vars
stylesheet.
I’m wondering that will resources.ExecuteAsTemplate "hbs/scss/_vars.scss" .
create the file assets/hbs/scss/_vars.scss
, which @used
by assets/hbs/scss/main.scss
?