Improve Tailwind/PostCSS error messages

Fixes #7041
Fixes #7042
This commit is contained in:
Bjørn Erik Pedersen
2020-03-10 18:12:11 +01:00
parent b1106f8715
commit df298558a5
11 changed files with 362 additions and 118 deletions

View File

@@ -22,6 +22,8 @@ import (
"strings"
"testing"
"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/htesting"
"github.com/spf13/viper"
@@ -717,11 +719,10 @@ func TestResourceChainPostCSS(t *testing.T) {
packageJSON := `{
"scripts": {},
"dependencies": {
"tailwindcss": "^1.2"
},
"devDependencies": {
"postcss-cli": "^7.1.0"
"postcss-cli": "^7.1.0",
"tailwindcss": "^1.2"
}
}
`
@@ -826,7 +827,7 @@ Styles Content: Len: 770878|
assertCss(b)
build := func(s string, shouldFail bool) {
build := func(s string, shouldFail bool) error {
b.Assert(os.RemoveAll(filepath.Join(workDir, "public")), qt.IsNil)
v := viper.New()
@@ -837,19 +838,37 @@ Styles Content: Len: 770878|
b = newTestBuilder(v)
b.Assert(os.RemoveAll(filepath.Join(workDir, "public")), qt.IsNil)
b.Assert(os.RemoveAll(filepath.Join(workDir, "node_modules")), qt.IsNil)
err := b.BuildE(BuildCfg{})
if shouldFail {
b.BuildFail(BuildCfg{})
b.Assert(err, qt.Not(qt.IsNil))
} else {
b.Build(BuildCfg{})
b.Assert(err, qt.IsNil)
assertCss(b)
}
return err
}
build("always", false)
build("fallback", false)
// Introduce a syntax error in an import
b.WithSourceFile("assets/css/components/b.css", `@import "a.css";
class-in-b {
@apply asdf;
}
`)
err = build("newer", true)
err = herrors.UnwrapErrorWithFileContext(err)
fe, ok := err.(*herrors.ErrorWithFileContext)
b.Assert(ok, qt.Equals, true)
b.Assert(fe.Position().LineNumber, qt.Equals, 4)
b.Assert(fe.Error(), qt.Contains, filepath.Join(workDir, "assets/css/components/b.css:4:1"))
// Remove PostCSS
b.Assert(os.RemoveAll(filepath.Join(workDir, "node_modules")), qt.IsNil)