From c782ebd89c4e40f9c7ce363359efc64e8355ad17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 8 Jun 2023 16:17:12 +0200 Subject: [PATCH] Fix indented SASS imports for Dart Sass Fixes #11074 --- go.mod | 2 +- go.sum | 4 +++ .../tocss/dartsass/integration_test.go | 32 +++++++++++++++++++ .../tocss/dartsass/transform.go | 17 +++++++--- 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index d19c1b0ba14..0144c0959f4 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/bep/debounce v1.2.0 github.com/bep/gitmap v1.1.2 github.com/bep/goat v0.5.0 - github.com/bep/godartsass v0.16.0 + github.com/bep/godartsass v1.1.0 github.com/bep/golibsass v1.1.1 github.com/bep/gowebp v0.2.0 github.com/bep/helpers v0.4.0 diff --git a/go.sum b/go.sum index ec96735a685..2a672cc1915 100644 --- a/go.sum +++ b/go.sum @@ -170,6 +170,10 @@ github.com/bep/goat v0.5.0 h1:S8jLXHCVy/EHIoCY+btKkmcxcXFd34a0Q63/0D4TKeA= github.com/bep/goat v0.5.0/go.mod h1:Md9x7gRxiWKs85yHlVTvHQw9rg86Bm+Y4SuYE8CTH7c= github.com/bep/godartsass v0.16.0 h1:nTpenrZBQjVSjLkCw3AgnYmBB2czauTJa4BLLv448qg= github.com/bep/godartsass v0.16.0/go.mod h1:6LvK9RftsXMxGfsA0LDV12AGc4Jylnu6NgHL+Q5/pE8= +github.com/bep/godartsass v1.0.0 h1:vL5TTtPkpEAZowsXydfJ3M1BatR9fH513FP3but9TEM= +github.com/bep/godartsass v1.0.0/go.mod h1:6LvK9RftsXMxGfsA0LDV12AGc4Jylnu6NgHL+Q5/pE8= +github.com/bep/godartsass v1.1.0 h1:MYNXVQMFoohxue9sCbHi+bWp4AeykvH40gQj1fd9q1c= +github.com/bep/godartsass v1.1.0/go.mod h1:6LvK9RftsXMxGfsA0LDV12AGc4Jylnu6NgHL+Q5/pE8= github.com/bep/golibsass v1.1.1 h1:xkaet75ygImMYjM+FnHIT3xJn7H0xBA9UxSOJjk8Khw= github.com/bep/golibsass v1.1.1/go.mod h1:DL87K8Un/+pWUS75ggYv41bliGiolxzDKWJAq3eJ1MA= github.com/bep/gowebp v0.2.0 h1:ZVfK8i9PpZqKHEmthQSt3qCnnHycbLzBPEsVtk2ch2Q= diff --git a/resources/resource_transformers/tocss/dartsass/integration_test.go b/resources/resource_transformers/tocss/dartsass/integration_test.go index aa71101a04e..d765923360c 100644 --- a/resources/resource_transformers/tocss/dartsass/integration_test.go +++ b/resources/resource_transformers/tocss/dartsass/integration_test.go @@ -109,6 +109,38 @@ T1: {{ $r.Content | safeHTML }} /* foo */`) } +func TestTransformImportIndentedSASS(t *testing.T) { + t.Parallel() + if !dartsass.Supports() { + t.Skip() + } + + files := ` +-- assets/scss/_moo.sass -- +#main + color: blue +-- assets/scss/main.scss -- +@import "moo"; + +/* foo */ +-- config.toml -- +-- layouts/index.html -- +{{ $r := resources.Get "scss/main.scss" | toCSS (dict "transpiler" "dartsass") }} +T1: {{ $r.Content | safeHTML }} + + ` + + b := hugolib.NewIntegrationTestBuilder( + hugolib.IntegrationTestConfig{ + T: t, + TxtarString: files, + NeedsOsFS: true, + }, + ).Build() + + b.AssertFileContent("public/index.html", "T1: #main {\n color: blue;\n}\n\n/* foo */") +} + // Issue 10592 func TestTransformImportMountedCSS(t *testing.T) { t.Parallel() diff --git a/resources/resource_transformers/tocss/dartsass/transform.go b/resources/resource_transformers/tocss/dartsass/transform.go index 61ea54437ad..95dfd594492 100644 --- a/resources/resource_transformers/tocss/dartsass/transform.go +++ b/resources/resource_transformers/tocss/dartsass/transform.go @@ -86,7 +86,7 @@ func (t *transform) Transform(ctx *resources.ResourceTransformationCtx) error { baseDir: baseDir, c: t.c, - varsStylesheet: sass.CreateVarsStyleSheet(opts.Vars), + varsStylesheet: godartsass.Import{Content: sass.CreateVarsStyleSheet(opts.Vars)}, }, OutputStyle: godartsass.ParseOutputStyle(opts.OutputStyle), EnableSourceMap: opts.EnableSourceMap, @@ -132,7 +132,7 @@ type importResolver struct { baseDir string c *Client - varsStylesheet string + varsStylesheet godartsass.Import } func (t importResolver) CanonicalizeURL(url string) (string, error) { @@ -184,11 +184,20 @@ func (t importResolver) CanonicalizeURL(url string) (string, error) { return "", nil } -func (t importResolver) Load(url string) (string, error) { +func (t importResolver) Load(url string) (godartsass.Import, error) { if url == sass.HugoVarsNamespace { return t.varsStylesheet, nil } filename, _ := paths.UrlToFilename(url) b, err := afero.ReadFile(hugofs.Os, filename) - return string(b), err + + sourceSyntax := godartsass.SourceSyntaxSCSS + if strings.HasSuffix(filename, ".sass") { + sourceSyntax = godartsass.SourceSyntaxSASS + } else if strings.HasSuffix(filename, ".css") { + sourceSyntax = godartsass.SourceSyntaxCSS + } + + return godartsass.Import{Content: string(b), SourceSyntax: sourceSyntax}, err + }