mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
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
This commit is contained in:
parent
f1a061e9ed
commit
7ae62f4aa3
4 changed files with 37 additions and 4 deletions
|
@ -31,6 +31,8 @@ const (
|
||||||
FilenamePackageHugoJSON = "package.hugo.json"
|
FilenamePackageHugoJSON = "package.hugo.json"
|
||||||
// The NPM package file.
|
// The NPM package file.
|
||||||
FilenamePackageJSON = "package.json"
|
FilenamePackageJSON = "package.json"
|
||||||
|
|
||||||
|
FilenameHugoStatsJSON = "hugo_stats.json"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/bep/logg"
|
"github.com/bep/logg"
|
||||||
|
"github.com/gohugoio/hugo/hugofs/files"
|
||||||
"github.com/gohugoio/hugo/langs"
|
"github.com/gohugoio/hugo/langs"
|
||||||
"github.com/gohugoio/hugo/publisher"
|
"github.com/gohugoio/hugo/publisher"
|
||||||
"github.com/gohugoio/hugo/tpl"
|
"github.com/gohugoio/hugo/tpl"
|
||||||
|
@ -491,14 +492,12 @@ func (h *HugoSites) writeBuildStats() error {
|
||||||
HTMLElements: *htmlElements,
|
HTMLElements: *htmlElements,
|
||||||
}
|
}
|
||||||
|
|
||||||
const hugoStatsName = "hugo_stats.json"
|
|
||||||
|
|
||||||
js, err := json.MarshalIndent(stats, "", " ")
|
js, err := json.MarshalIndent(stats, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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 {
|
if existingContent, err := afero.ReadFile(hugofs.Os, filename); err == nil {
|
||||||
// Check if the content has changed.
|
// Check if the content has changed.
|
||||||
|
|
|
@ -664,9 +664,23 @@ func (c *collector) normalizeMounts(owner *moduleAdapter, mounts []Mount) ([]Mou
|
||||||
// Verify that Source exists
|
// Verify that Source exists
|
||||||
_, err := c.fs.Stat(sourceDir)
|
_, err := c.fs.Stat(sourceDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Verify that target points to one of the predefined component dirs
|
// Verify that target points to one of the predefined component dirs
|
||||||
targetBase := mnt.Target
|
targetBase := mnt.Target
|
||||||
idxPathSep := strings.Index(mnt.Target, string(os.PathSeparator))
|
idxPathSep := strings.Index(mnt.Target, string(os.PathSeparator))
|
||||||
|
|
18
testscripts/unfinished/server__watch_hugo_stats.txt
Normal file
18
testscripts/unfinished/server__watch_hugo_stats.txt
Normal file
|
@ -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 --
|
||||||
|
<body>Home</body>
|
Loading…
Reference in a new issue