shithub: hugo

Download patch

ref: 3acde9ae04fbf4a8c635d404608cb87218a8b803
parent: 473b6610d51d4a33ba35917f95b0d97ea78dad2b
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Sun Sep 20 09:34:45 EDT 2020

Make sure CSS is rebuilt when postcss.config.js or tailwind.config.js changes

Fixes #7715

--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -23,6 +23,7 @@
 	"os"
 	"path"
 	"path/filepath"
+	"regexp"
 	"sort"
 	"strconv"
 	"strings"
@@ -1027,11 +1028,20 @@
 		logger = helpers.NewDistinctFeedbackLogger()
 	)
 
+	var isCSSConfigRe = regexp.MustCompile(`(postcss|tailwind)\.config\.js`)
+	var isCSSFileRe = regexp.MustCompile(`\.(css|scss|sass)`)
+
 	var cachePartitions []string
+	// Special case
+	// TODO(bep) I have a ongoing branch where I have redone the cache. Consider this there.
+	var isCSSChange bool
 
 	for _, ev := range events {
 		if assetsFilename := s.BaseFs.Assets.MakePathRelative(ev.Name); assetsFilename != "" {
 			cachePartitions = append(cachePartitions, resources.ResourceKeyPartitions(assetsFilename)...)
+			if !isCSSChange {
+				isCSSChange = isCSSFileRe.MatchString(assetsFilename) || isCSSConfigRe.MatchString(assetsFilename)
+			}
 		}
 
 		id, found := s.eventToIdentity(ev)
@@ -1078,6 +1088,9 @@
 	// These in memory resource caches will be rebuilt on demand.
 	for _, s := range s.h.Sites {
 		s.ResourceSpec.ResourceCache.DeletePartitions(cachePartitions...)
+		if isCSSChange {
+			s.ResourceSpec.ResourceCache.DeleteContains("css", "scss", "sass")
+		}
 	}
 
 	if tmplChanged || i18nChanged {
--- a/resources/resource_cache.go
+++ b/resources/resource_cache.go
@@ -295,3 +295,22 @@
 	}
 
 }
+
+func (c *ResourceCache) DeleteContains(parts ...string) {
+	c.Lock()
+	defer c.Unlock()
+
+	for k := range c.cache {
+		clear := false
+		for _, part := range parts {
+			if strings.Contains(k, part) {
+				clear = true
+				break
+			}
+		}
+		if clear {
+			delete(c.cache, k)
+		}
+	}
+
+}