From 7ae62f4aa384a734faafe2a0918d7a6bc029f0b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Wed, 19 Jul 2023 10:28:56 +0200 Subject: [PATCH] Create hugo_stats.json if it's mounted but does not exists A common pattern for Tailwind 3 is to mount that file to get it on the server watch list. A common pattern is also to add hugo_stats.json to .gitignore. This has meant that the first time you start the server (no hugo_stats.json), it just doesn't work as expected. Fixes #11264 --- hugofs/files/classifier.go | 2 ++ hugolib/hugo_sites_build.go | 5 ++--- modules/collect.go | 16 +++++++++++++++- .../unfinished/server__watch_hugo_stats.txt | 18 ++++++++++++++++++ 4 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 testscripts/unfinished/server__watch_hugo_stats.txt diff --git a/hugofs/files/classifier.go b/hugofs/files/classifier.go index 09b239c21..bdac2d686 100644 --- a/hugofs/files/classifier.go +++ b/hugofs/files/classifier.go @@ -31,6 +31,8 @@ const ( FilenamePackageHugoJSON = "package.hugo.json" // The NPM package file. FilenamePackageJSON = "package.json" + + FilenameHugoStatsJSON = "hugo_stats.json" ) var ( diff --git a/hugolib/hugo_sites_build.go b/hugolib/hugo_sites_build.go index 0a861d32e..0c1ec050b 100644 --- a/hugolib/hugo_sites_build.go +++ b/hugolib/hugo_sites_build.go @@ -23,6 +23,7 @@ import ( "time" "github.com/bep/logg" + "github.com/gohugoio/hugo/hugofs/files" "github.com/gohugoio/hugo/langs" "github.com/gohugoio/hugo/publisher" "github.com/gohugoio/hugo/tpl" @@ -491,14 +492,12 @@ func (h *HugoSites) writeBuildStats() error { HTMLElements: *htmlElements, } - const hugoStatsName = "hugo_stats.json" - js, err := json.MarshalIndent(stats, "", " ") if err != nil { return err } - filename := filepath.Join(h.Configs.LoadingInfo.BaseConfig.WorkingDir, hugoStatsName) + filename := filepath.Join(h.Configs.LoadingInfo.BaseConfig.WorkingDir, files.FilenameHugoStatsJSON) if existingContent, err := afero.ReadFile(hugofs.Os, filename); err == nil { // Check if the content has changed. diff --git a/modules/collect.go b/modules/collect.go index 0c578b5db..e47563ab7 100644 --- a/modules/collect.go +++ b/modules/collect.go @@ -664,7 +664,21 @@ func (c *collector) normalizeMounts(owner *moduleAdapter, mounts []Mount) ([]Mou // Verify that Source exists _, err := c.fs.Stat(sourceDir) if err != nil { - continue + if strings.HasSuffix(sourceDir, files.FilenameHugoStatsJSON) { + // A common pattern for Tailwind 3 is to mount that file to get it on the server watch list. + + // A common pattern is also to add hugo_stats.json to .gitignore. + + // Create an empty file. + f, err := c.fs.Create(sourceDir) + if err != nil { + return nil, fmt.Errorf("%s: %q", errMsg, err) + } + f.Close() + } else { + continue + } + } // Verify that target points to one of the predefined component dirs diff --git a/testscripts/unfinished/server__watch_hugo_stats.txt b/testscripts/unfinished/server__watch_hugo_stats.txt new file mode 100644 index 000000000..179e59f8d --- /dev/null +++ b/testscripts/unfinished/server__watch_hugo_stats.txt @@ -0,0 +1,18 @@ +hugo server & + +waitServer +stopServer +! stderr . + +exists hugo_stats.json + +-- hugo.toml -- +title = "Hugo Server Test" +baseURL = "https://example.org/" +disableKinds = ["taxonomy", "term", "sitemap"] +[module] +[[module.mounts]] +source = "hugo_stats.json" +target = "assets/watching/hugo_stats.json" +-- layouts/index.html -- +Home