Skip to content

Commit

Permalink
Improve Tailwind/PostCSS error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Mar 10, 2020
1 parent b1106f8 commit d323726
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 50 deletions.
14 changes: 10 additions & 4 deletions commands/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,9 +722,6 @@ func (c *commandeer) handleBuildErr(err error, msg string) {

func (c *commandeer) rebuildSites(events []fsnotify.Event) error {
defer c.timeTrack(time.Now(), "Total")
defer func() {
c.wasError = false
}()

c.buildErr = nil
visited := c.visitedURLs.PeekAllSet()
Expand Down Expand Up @@ -886,6 +883,10 @@ func (c *commandeer) handleEvents(watcher *watcher.Batcher,
evs []fsnotify.Event,
configSet map[string]bool) {

defer func() {
c.wasError = false
}()

var isHandled bool

for _, ev := range evs {
Expand Down Expand Up @@ -1080,10 +1081,11 @@ func (c *commandeer) handleEvents(watcher *watcher.Batcher,
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initialized

// force refresh when more than one file
if len(staticEvents) == 1 {
if !c.wasError && len(staticEvents) == 1 {
ev := staticEvents[0]
path := c.hugo().BaseFs.SourceFilesystems.MakeStaticPathRelative(ev.Name)
path = c.firstPathSpec().RelURL(helpers.ToSlashTrimLeading(path), false)

livereload.RefreshPath(path)
} else {
livereload.ForceRefresh()
Expand All @@ -1107,6 +1109,10 @@ func (c *commandeer) handleEvents(watcher *watcher.Batcher,

if doLiveReload {
if len(partitionedEvents.ContentEvents) == 0 && len(partitionedEvents.AssetEvents) > 0 {
if c.wasError {
livereload.ForceRefresh()
return
}
changed := c.changeDetector.changed()
if c.changeDetector != nil && len(changed) == 0 {
// Nothing has changed.
Expand Down
1 change: 1 addition & 0 deletions commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, string, erro
if err != nil {
f.c.logger.ERROR.Println(err)
}

port = 1313
if !f.c.paused {
port = f.c.Cfg.GetInt("liveReloadPort")
Expand Down
14 changes: 5 additions & 9 deletions commands/server_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ var buildErrorTemplate = `<!doctype html>
body {
font-family: "Muli",avenir, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
font-size: 16px;
background-color: black;
color: rgba(255, 255, 255, 0.9);
background-color: #2f1e2e;
}
main {
margin: auto;
Expand All @@ -43,7 +42,7 @@ var buildErrorTemplate = `<!doctype html>
padding: 1rem 0;
}
.stack {
margin-top: 6rem;
margin-top: 4rem;
}
pre {
white-space: pre-wrap;
Expand All @@ -54,10 +53,7 @@ var buildErrorTemplate = `<!doctype html>
}
.highlight {
overflow-x: auto;
padding: 0.75rem;
margin-bottom: 1rem;
background-color: #272822;
border: 1px solid black;
}
a {
color: #0594cb;
Expand All @@ -70,14 +66,14 @@ var buildErrorTemplate = `<!doctype html>
</head>
<body>
<main>
{{ highlight .Error "apl" "noclasses=true,style=monokai" }}
{{ highlight .Error "apl" "linenos=false,noclasses=true,style=paraiso-dark" }}
{{ with .File }}
{{ $params := printf "noclasses=true,style=monokai,linenos=table,hl_lines=%d,linenostart=%d" (add .LinesPos 1) (sub .Position.LineNumber .LinesPos) }}
{{ $params := printf "noclasses=true,style=paraiso-dark,linenos=table,hl_lines=%d,linenostart=%d" (add .LinesPos 1) (sub .Position.LineNumber .LinesPos) }}
{{ $lexer := .ChromaLexer | default "go-html-template" }}
{{ highlight (delimit .Lines "\n") $lexer $params }}
{{ end }}
{{ with .StackTrace }}
{{ highlight . "apl" "noclasses=true,style=monokai" }}
{{ highlight . "apl" "noclasses=true,style=paraiso-dark" }}
{{ end }}
<p class="version">{{ .Version }}</p>
<a href="">Reload Page</a>
Expand Down
8 changes: 5 additions & 3 deletions deps/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ func New(cfg DepsCfg) (*Deps, error) {
return nil, errors.WithMessage(err, "failed to create file caches from configuration")
}

resourceSpec, err := resources.NewSpec(ps, fileCaches, logger, cfg.OutputFormats, cfg.MediaTypes)
errorHandler := &globalErrHandler{}

resourceSpec, err := resources.NewSpec(ps, fileCaches, logger, errorHandler, cfg.OutputFormats, cfg.MediaTypes)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -275,7 +277,7 @@ func New(cfg DepsCfg) (*Deps, error) {
BuildStartListeners: &Listeners{},
BuildFlags: &BuildFlags{},
Timeout: time.Duration(timeoutms) * time.Millisecond,
globalErrHandler: &globalErrHandler{},
globalErrHandler: errorHandler,
}

if cfg.Cfg.GetBool("templateMetrics") {
Expand Down Expand Up @@ -306,7 +308,7 @@ func (d Deps) ForLanguage(cfg DepsCfg, onCreated func(d *Deps) error) (*Deps, er
// The resource cache is global so reuse.
// TODO(bep) clean up these inits.
resourceCache := d.ResourceSpec.ResourceCache
d.ResourceSpec, err = resources.NewSpec(d.PathSpec, d.ResourceSpec.FileCaches, d.Log, cfg.OutputFormats, cfg.MediaTypes)
d.ResourceSpec, err = resources.NewSpec(d.PathSpec, d.ResourceSpec.FileCaches, d.Log, d.globalErrHandler, cfg.OutputFormats, cfg.MediaTypes)
if err != nil {
return nil, err
}
Expand Down
31 changes: 23 additions & 8 deletions hugolib/resource_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package hugolib

import (
"fmt"
"io"
"os"
"os/exec"
Expand Down Expand Up @@ -717,11 +718,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"
}
}
`
Expand Down Expand Up @@ -826,7 +826,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()
Expand All @@ -837,19 +837,34 @@ 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)

fmt.Println(">>> ERR", err)

// Remove PostCSS
b.Assert(os.RemoveAll(filepath.Join(workDir, "node_modules")), qt.IsNil)

Expand Down
7 changes: 6 additions & 1 deletion resources/resource_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"path/filepath"
"strings"

"github.com/gohugoio/hugo/common/herrors"

"github.com/gohugoio/hugo/config"

"github.com/gohugoio/hugo/hugofs"
Expand All @@ -43,6 +45,7 @@ func NewSpec(
s *helpers.PathSpec,
fileCaches filecache.Caches,
logger *loggers.Logger,
errorHandler herrors.ErrorSender,
outputFormats output.Formats,
mimeTypes media.Types) (*Spec, error) {

Expand All @@ -67,6 +70,7 @@ func NewSpec(

rs := &Spec{PathSpec: s,
Logger: logger,
ErrorSender: errorHandler,
imaging: imaging,
MediaTypes: mimeTypes,
OutputFormats: outputFormats,
Expand All @@ -91,7 +95,8 @@ type Spec struct {
MediaTypes media.Types
OutputFormats output.Formats

Logger *loggers.Logger
Logger *loggers.Logger
ErrorSender herrors.ErrorSender

TextTemplates tpl.TemplateParseFinder

Expand Down
Loading

0 comments on commit d323726

Please sign in to comment.