Skip to content

Commit 889308d

Browse files
jmooringbep
authored andcommitted
resources: Address Dart Sass deprecation of global built-in functions
See https://github.com/sass/dart-sass/releases/tag/1.80.0 Fixes #12961
1 parent 72352f2 commit 889308d

File tree

6 files changed

+71
-19
lines changed

6 files changed

+71
-19
lines changed

resources/resource_transformers/tocss/dartsass/transform.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"github.com/gohugoio/hugo/resources"
3030

3131
"github.com/gohugoio/hugo/resources/internal"
32-
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/internal/sass"
32+
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/sass"
3333

3434
"github.com/spf13/afero"
3535

@@ -85,7 +85,7 @@ func (t *transform) Transform(ctx *resources.ResourceTransformationCtx) error {
8585
c: t.c,
8686
dependencyManager: ctx.DependencyManager,
8787

88-
varsStylesheet: godartsass.Import{Content: sass.CreateVarsStyleSheet(opts.Vars)},
88+
varsStylesheet: godartsass.Import{Content: sass.CreateVarsStyleSheet(sass.TranspilerDart, opts.Vars)},
8989
},
9090
OutputStyle: godartsass.ParseOutputStyle(opts.OutputStyle),
9191
EnableSourceMap: opts.EnableSourceMap,

resources/resource_transformers/tocss/internal/sass/helpers.go resources/resource_transformers/tocss/sass/helpers.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ import (
2424

2525
const (
2626
HugoVarsNamespace = "hugo:vars"
27+
// Transpiler implementation can be controlled from the client by
28+
// setting the 'transpiler' option.
29+
// Default is currently 'libsass', but that may change.
30+
TranspilerDart = "dartsass"
31+
TranspilerLibSass = "libsass"
2732
)
2833

29-
func CreateVarsStyleSheet(vars map[string]any) string {
34+
func CreateVarsStyleSheet(transpiler string, vars map[string]any) string {
3035
if vars == nil {
3136
return ""
3237
}
@@ -49,12 +54,22 @@ func CreateVarsStyleSheet(vars map[string]any) string {
4954
varsSlice = append(varsSlice, fmt.Sprintf("%s%s: %v;", prefix, k, v))
5055
} else {
5156
// unquote will preserve quotes around URLs etc. if needed.
52-
varsSlice = append(varsSlice, fmt.Sprintf("%s%s: unquote(%q);", prefix, k, v))
57+
if transpiler == TranspilerDart {
58+
varsSlice = append(varsSlice, fmt.Sprintf("%s%s: string.unquote(%q);", prefix, k, v))
59+
} else {
60+
varsSlice = append(varsSlice, fmt.Sprintf("%s%s: unquote(%q);", prefix, k, v))
61+
}
5362
}
5463
}
5564
}
5665
sort.Strings(varsSlice)
57-
varsStylesheet = strings.Join(varsSlice, "\n")
66+
67+
if transpiler == TranspilerDart {
68+
varsStylesheet = `@use "sass:string";` + "\n" + strings.Join(varsSlice, "\n")
69+
} else {
70+
varsStylesheet = strings.Join(varsSlice, "\n")
71+
}
72+
5873
return varsStylesheet
5974
}
6075

resources/resource_transformers/tocss/scss/tocss.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
"github.com/gohugoio/hugo/identity"
3232
"github.com/gohugoio/hugo/media"
3333
"github.com/gohugoio/hugo/resources"
34-
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/internal/sass"
34+
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/sass"
3535
)
3636

3737
// Used in tests. This feature requires Hugo to be built with the extended tag.
@@ -64,7 +64,7 @@ func (t *toCSSTransformation) Transform(ctx *resources.ResourceTransformationCtx
6464
}
6565
}
6666

67-
varsStylesheet := sass.CreateVarsStyleSheet(options.from.Vars)
67+
varsStylesheet := sass.CreateVarsStyleSheet(sass.TranspilerLibSass, options.from.Vars)
6868

6969
// To allow for overrides of SCSS files anywhere in the project/theme hierarchy, we need
7070
// to help libsass revolve the filename by looking in the composite filesystem first.

tpl/css/css.go

+5-12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/gohugoio/hugo/resources/resource_transformers/babel"
1616
"github.com/gohugoio/hugo/resources/resource_transformers/cssjs"
1717
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/dartsass"
18+
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/sass"
1819
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/scss"
1920
"github.com/gohugoio/hugo/tpl/internal"
2021
"github.com/gohugoio/hugo/tpl/internal/resourcehelpers"
@@ -84,21 +85,13 @@ func (ns *Namespace) Sass(args ...any) (resource.Resource, error) {
8485
return nil, errors.New("must not provide more arguments than resource object and options")
8586
}
8687

87-
const (
88-
// Transpiler implementation can be controlled from the client by
89-
// setting the 'transpiler' option.
90-
// Default is currently 'libsass', but that may change.
91-
transpilerDart = "dartsass"
92-
transpilerLibSass = "libsass"
93-
)
94-
9588
var (
9689
r resources.ResourceTransformer
9790
m map[string]any
9891
targetPath string
9992
err error
10093
ok bool
101-
transpiler = transpilerLibSass
94+
transpiler = sass.TranspilerLibSass
10295
)
10396

10497
r, targetPath, ok = resourcehelpers.ResolveIfFirstArgIsString(args)
@@ -113,15 +106,15 @@ func (ns *Namespace) Sass(args ...any) (resource.Resource, error) {
113106
if m != nil {
114107
if t, _, found := maps.LookupEqualFold(m, "transpiler"); found {
115108
switch t {
116-
case transpilerDart, transpilerLibSass:
109+
case sass.TranspilerDart, sass.TranspilerLibSass:
117110
transpiler = cast.ToString(t)
118111
default:
119-
return nil, fmt.Errorf("unsupported transpiler %q; valid values are %q or %q", t, transpilerLibSass, transpilerDart)
112+
return nil, fmt.Errorf("unsupported transpiler %q; valid values are %q or %q", t, sass.TranspilerLibSass, sass.TranspilerDart)
120113
}
121114
}
122115
}
123116

124-
if transpiler == transpilerLibSass {
117+
if transpiler == sass.TranspilerLibSass {
125118
var options scss.Options
126119
if targetPath != "" {
127120
options.TargetPath = paths.ToSlashTrimLeading(targetPath)

tpl/resources/resources_integration_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818

1919
qt "github.com/frankban/quicktest"
2020
"github.com/gohugoio/hugo/hugolib"
21+
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/dartsass"
22+
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/scss"
2123
)
2224

2325
func TestCopy(t *testing.T) {
@@ -238,3 +240,45 @@ match /files/C*: 2|
238240
b.AssertFileContent("public/files/b.txt", "I am b.txt")
239241
b.AssertFileContent("public/files/C.txt", "I am C.txt")
240242
}
243+
244+
// Issue #12961
245+
func TestDartSassVars(t *testing.T) {
246+
t.Parallel()
247+
248+
if !scss.Supports() || !dartsass.Supports() {
249+
t.Skip()
250+
}
251+
252+
files := `
253+
-- hugo.toml --
254+
disableKinds = ['page','section','rss','sitemap','taxonomy','term']
255+
-- layouts/index.html --
256+
{{ $opts := dict "transpiler" "dartsass" "outputStyle" "compressed" "vars" (dict "color" "red") }}
257+
{{ with resources.Get "dartsass.scss" | css.Sass $opts }}
258+
{{ .Content }}
259+
{{ end }}
260+
261+
{{ $opts := dict "transpiler" "libsass" "outputStyle" "compressed" "vars" (dict "color" "blue") }}
262+
{{ with resources.Get "libsass.scss" | css.Sass $opts }}
263+
{{ .Content }}
264+
{{ end }}
265+
-- assets/dartsass.scss --
266+
@use "hugo:vars" as v;
267+
.dartsass {
268+
color: v.$color;
269+
}
270+
-- assets/libsass.scss --
271+
@import "hugo:vars";
272+
.libsass {
273+
color: $color;
274+
}
275+
`
276+
277+
b := hugolib.Test(t, files, hugolib.TestOptWarn())
278+
279+
b.AssertFileContent("public/index.html",
280+
".dartsass{color:red}",
281+
".libsass{color:blue}",
282+
)
283+
b.AssertLogContains("! WARN Dart Sass: hugo:vars")
284+
}

0 commit comments

Comments
 (0)