diff options
Diffstat (limited to 'resources/resource_transformers/tocss')
3 files changed, 56 insertions, 8 deletions
diff --git a/resources/resource_transformers/tocss/dartsass/client.go b/resources/resource_transformers/tocss/dartsass/client.go index b5d05b30b..647cf86aa 100644 --- a/resources/resource_transformers/tocss/dartsass/client.go +++ b/resources/resource_transformers/tocss/dartsass/client.go @@ -18,6 +18,7 @@ package dartsass import ( "fmt" "io" + "strings" "github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/hugolib/filesystems" @@ -41,7 +42,19 @@ func New(fs *filesystems.SourceFilesystem, rs *resources.Spec) (*Client, error) return nil, err } - transpiler, err := godartsass.Start(godartsass.Options{}) + transpiler, err := godartsass.Start(godartsass.Options{ + LogEventHandler: func(event godartsass.LogEvent) { + message := strings.ReplaceAll(event.Message, stdinPrefix, "") + switch event.Type { + case godartsass.LogEventTypeDebug: + // Log as Info for now, we may adjust this if it gets too chatty. + rs.Logger.Infof("Dart Sass: %s", message) + default: + // The rest are either deprecations or @warn statements. + rs.Logger.Warnf("Dart Sass: %s", message) + } + }, + }) if err != nil { return nil, err } diff --git a/resources/resource_transformers/tocss/dartsass/integration_test.go b/resources/resource_transformers/tocss/dartsass/integration_test.go index 9434b1d52..a1ac1d59f 100644 --- a/resources/resource_transformers/tocss/dartsass/integration_test.go +++ b/resources/resource_transformers/tocss/dartsass/integration_test.go @@ -18,6 +18,7 @@ import ( "github.com/gohugoio/hugo/hugolib" "github.com/gohugoio/hugo/resources/resource_transformers/tocss/dartsass" + jww "github.com/spf13/jwalterweatherman" ) func TestTransformIncludePaths(t *testing.T) { @@ -164,3 +165,34 @@ zoo { b.AssertFileContent("public/index.html", `T1: moo{color:#ccc}boo{color:green}zoo{color:pink}`) } + +func TestTransformLogging(t *testing.T) { + if !dartsass.Supports() { + t.Skip() + } + + files := ` +-- assets/scss/main.scss -- +@warn "foo"; +@debug "bar"; + +-- config.toml -- +disableKinds = ["term", "taxonomy", "section", "page"] +-- layouts/index.html -- +{{ $cssOpts := (dict "transpiler" "dartsass" ) }} +{{ $r := resources.Get "scss/main.scss" | toCSS $cssOpts }} +T1: {{ $r.Content }} + ` + + b := hugolib.NewIntegrationTestBuilder( + hugolib.IntegrationTestConfig{ + T: t, + TxtarString: files, + NeedsOsFS: true, + LogLevel: jww.LevelInfo, + }).Build() + + b.AssertLogMatches(`WARN.*Dart Sass: foo`) + b.AssertLogMatches(`INFO.*Dart Sass: .*assets.*main.scss:1:0: bar`) + +} diff --git a/resources/resource_transformers/tocss/dartsass/transform.go b/resources/resource_transformers/tocss/dartsass/transform.go index 57d9feadb..aaf9d4bf8 100644 --- a/resources/resource_transformers/tocss/dartsass/transform.go +++ b/resources/resource_transformers/tocss/dartsass/transform.go @@ -39,7 +39,8 @@ import ( const ( // See https://github.com/sass/dart-sass-embedded/issues/24 - stdinPlaceholder = "HUGOSTDIN" + // Note: This prefix must be all lower case. + stdinPrefix = "hugostdin:" dartSassEmbeddedBinaryName = "dart-sass-embedded" ) @@ -75,9 +76,14 @@ func (t *transform) Transform(ctx *resources.ResourceTransformationCtx) error { } baseDir := path.Dir(ctx.SourcePath) + filename := stdinPrefix + + if ctx.SourcePath != "" { + filename += t.c.sfs.RealFilename(ctx.SourcePath) + } args := godartsass.Args{ - URL: stdinPlaceholder, + URL: filename, IncludePaths: t.c.sfs.RealDirs(baseDir), ImportResolver: importResolver{ baseDir: baseDir, @@ -106,11 +112,8 @@ func (t *transform) Transform(ctx *resources.ResourceTransformationCtx) error { start := sassErr.Span.Start context := strings.TrimSpace(sassErr.Span.Context) filename, _ := urlToFilename(sassErr.Span.Url) - if filename == stdinPlaceholder { - if ctx.SourcePath == "" { - return sassErr - } - filename = t.c.sfs.RealFilename(ctx.SourcePath) + if strings.HasPrefix(filename, stdinPrefix) { + filename = filename[len(stdinPrefix):] } offsetMatcher := func(m herrors.LineMatcher) bool { |