Mostly to make it easier to toggle on/off this feature from the env.
See #11191
var defaultBuild = BuildConfig{
UseResourceCacheWhen: "fallback",
- WriteStats: WriteStats{},
+ BuildStats: BuildStats{},
CacheBusters: []CacheBuster{
{
// When enabled, will collect and write a hugo_stats.json with some build
// related aggregated data (e.g. CSS class names).
// Note that this was a bool <= v0.115.0.
- WriteStats WriteStats
+ BuildStats BuildStats
// Can be used to toggle off writing of the IntelliSense /assets/jsconfig.js
// file.
CacheBusters []CacheBuster
}
-// WriteStats configures what to write to the hugo_stats.json file.
-type WriteStats struct {
- Tags bool
- Classes bool
- IDs bool
+// BuildStats configures if and what to write to the hugo_stats.json file.
+type BuildStats struct {
+ Enable bool
+ DisableTags bool
+ DisableClasses bool
+ DisableIDs bool
}
-func (w WriteStats) Enabled() bool {
- return w.Tags || w.Classes || w.IDs
+func (w BuildStats) Enabled() bool {
+ if !w.Enable {
+ return false
+ }
+ return !w.DisableTags || !w.DisableClasses || !w.DisableIDs
}
func (b BuildConfig) clone() BuildConfig {
// writeStats was a bool <= v0.115.0.
if writeStats, ok := m["writestats"]; ok {
if bb, ok := writeStats.(bool); ok {
- m["writestats"] = WriteStats{
- Tags: bb,
- Classes: bb,
- IDs: bb,
- }
+ m["buildstats"] = BuildStats{Enable: bb}
}
}
if h.ResourceSpec == nil {
panic("h.ResourceSpec is nil")
}
- if !h.ResourceSpec.BuildConfig().WriteStats.Enabled() {
+ if !h.ResourceSpec.BuildConfig().BuildStats.Enabled() {
return nil
}
b.AssertDestinationExists("hugo_stats.json", false)
b = r(`
-[build.writeStats]
-tags = true
-classes = true
-ids = true
- `)
+[build.buildStats]
+enable = true
+`)
b.AssertFileContent("hugo_stats.json", "myclass", "div", "myid")
b = r(`
-[build.writeStats]
-tags = true
-classes = true
-ids = false
+[build.buildStats]
+enable = true
+disableids = true
`)
b.AssertFileContent("hugo_stats.json", "myclass", "div", "! myid")
b = r(`
-[build.writeStats]
-tags = true
-classes = false
-ids = true
+[build.buildStats]
+enable = true
+disableclasses = true
`)
b.AssertFileContent("hugo_stats.json", "! myclass", "div", "myid")
b = r(`
-[build.writeStats]
-tags = false
-classes = true
-ids = true
+[build.buildStats]
+enable = true
+disabletags = true
`)
b.AssertFileContent("hugo_stats.json", "myclass", "! div", "myid")
b = r(`
-[build.writeStats]
-tags = false
-classes = false
-ids = false
+[build.buildStats]
+enable = false
`)
b.AssertDestinationExists("hugo_stats.json", false)
}
)
-func newHTMLElementsCollector(conf config.WriteStats) *htmlElementsCollector {
+func newHTMLElementsCollector(conf config.BuildStats) *htmlElementsCollector {
return &htmlElementsCollector{
conf: conf,
elementSet: make(map[string]bool),
}
type htmlElementsCollector struct {
- conf config.WriteStats
+ conf config.BuildStats
// Contains the raw HTML string. We will get the same element
// several times, and want to avoid costly reparsing when this
for _, el := range c.elements {
classes = append(classes, el.Classes...)
ids = append(ids, el.IDs...)
- if c.conf.Tags {
+ if !c.conf.DisableTags {
tags = append(tags, el.Tag)
}
}
func (w *htmlElementsCollectorWriter) parseHTMLElement(elStr string) (el htmlElement, err error) {
conf := w.collector.conf
- if !conf.IDs && !conf.Classes {
+ if conf.DisableTags && conf.DisableClasses {
// Nothing to do.
return
}
switch {
case strings.EqualFold(a.Key, "id"):
// There should be only one, but one never knows...
- if conf.IDs {
+ if !conf.DisableIDs {
el.IDs = append(el.IDs, a.Val)
}
default:
- if !conf.Classes {
+ if conf.DisableClasses {
continue
}
if classAttrRe.MatchString(a.Key) {
c.Run(fmt.Sprintf("%s--minify-%t", test.name, variant.minify), func(c *qt.C) {
w := newHTMLElementsCollectorWriter(newHTMLElementsCollector(
- config.WriteStats{
- Tags: true,
- Classes: true,
- IDs: true,
- },
+ config.BuildStats{Enable: true},
))
if variant.minify {
if skipMinifyTest[test.name] {
`
for i := 0; i < b.N; i++ {
w := newHTMLElementsCollectorWriter(newHTMLElementsCollector(
- config.WriteStats{
- Tags: true,
- Classes: true,
- IDs: true,
- },
+ config.BuildStats{Enable: true},
))
fmt.Fprint(w, benchHTML)
`
w := newHTMLElementsCollectorWriter(newHTMLElementsCollector(
- config.WriteStats{
- Tags: true,
- Classes: true,
- IDs: true,
- },
+ config.BuildStats{Enable: true},
))
for i := 0; i < b.N; i++ {
fmt.Fprint(w, benchHTML)
fs := rs.BaseFs.PublishFs
cfg := rs.Cfg
var classCollector *htmlElementsCollector
- if rs.BuildConfig().WriteStats.Enabled() {
- classCollector = newHTMLElementsCollector(rs.BuildConfig().WriteStats)
+ if rs.BuildConfig().BuildStats.Enabled() {
+ classCollector = newHTMLElementsCollector(rs.BuildConfig().BuildStats)
}
pub = DestinationPublisher{fs: fs, htmlElementsCollector: classCollector}
pub.min, err = minifiers.New(mediaTypes, outputFormats, cfg)