Configure build
The build configuration section contains global build-related configuration options.
build:
buildStats:
disableClasses: false
disableIDs: false
disableTags: false
enable: false
cacheBusters:
- source: '(postcss|tailwind)\.config\.js'
target: (css|styles|scss|sass)
noJSConfigInAssets: false
useResourceCacheWhen: fallback
[build]
noJSConfigInAssets = false
useResourceCacheWhen = 'fallback'
[build.buildStats]
disableClasses = false
disableIDs = false
disableTags = false
enable = false
[[build.cacheBusters]]
source = '(postcss|tailwind)\.config\.js'
target = '(css|styles|scss|sass)'
{
"build": {
"buildStats": {
"disableClasses": false,
"disableIDs": false,
"disableTags": false,
"enable": false
},
"cacheBusters": [
{
"source": "(postcss|tailwind)\\.config\\.js",
"target": "(css|styles|scss|sass)"
}
],
"noJSConfigInAssets": false,
"useResourceCacheWhen": "fallback"
}
}
- buildStats
- See the build stats section below.
- cachebusters
- See the cache busters section below.
- noJSConfigInAssets
- (
bool) Whether to disable writing ajsconfig.jsonin yourassetsdirectory with mapping of imports from running js.Build. This file is intended to help with intellisense/navigation inside code editors such as VS Code. Note that if you do not usejs.Build, no file will be written. - useResourceCacheWhen
- (
string) When to use the resource file cache, one ofnever,fallback, oralways. Applicable when transpiling Sass to CSS. Default isfallback.
Cache busters
The build.cachebusters configuration option was added to support development using Tailwind 3.x’s JIT compiler where a build configuration may look like this:
build:
buildStats:
enable: true
cachebusters:
- source: 'assets/watching/hugo_stats\.json'
target: 'styles\.css'
- source: '(postcss|tailwind)\.config\.js'
target: css
- source: 'assets/.*\.(js|ts|jsx|tsx)'
target: js
- source: 'assets/.*\.(.*)$'
target: $1
[build]
[build.buildStats]
enable = true
[[build.cachebusters]]
source = 'assets/watching/hugo_stats\.json'
target = 'styles\.css'
[[build.cachebusters]]
source = '(postcss|tailwind)\.config\.js'
target = 'css'
[[build.cachebusters]]
source = 'assets/.*\.(js|ts|jsx|tsx)'
target = 'js'
[[build.cachebusters]]
source = 'assets/.*\.(.*)$'
target = '$1'
{
"build": {
"buildStats": {
"enable": true
},
"cachebusters": [
{
"source": "assets/watching/hugo_stats\\.json",
"target": "styles\\.css"
},
{
"source": "(postcss|tailwind)\\.config\\.js",
"target": "css"
},
{
"source": "assets/.*\\.(js|ts|jsx|tsx)",
"target": "js"
},
{
"source": "assets/.*\\.(.*)$",
"target": "$1"
}
]
}
}
When buildStats is enabled, Hugo writes a hugo_stats.json file on each build with HTML classes etc. that’s used in the rendered output. Changes to this file will trigger a rebuild of the styles.css file. You also need to add hugo_stats.json to Hugo’s server watcher. See Hugo Starter Tailwind Basic for a running example.
- source
- (
string) A regular expression matching file(s) relative to one of the virtual component directories in Hugo, typicallyassets/.... - target
- (
string) A regular expression matching the keys in the resource cache that should be expired whensourcechanges. You can use the matching regexp groups fromsourcein the expression, e.g.$1.
Build stats
build:
buildStats:
disableClasses: false
disableIDs: false
disableTags: false
enable: false
[build]
[build.buildStats]
disableClasses = false
disableIDs = false
disableTags = false
enable = false
{
"build": {
"buildStats": {
"disableClasses": false,
"disableIDs": false,
"disableTags": false,
"enable": false
}
}
}
- enable
- (
bool) Whether to create ahugo_stats.jsonfile in the root of your project. This file contains arrays of theclassattributes,idattributes, and tags of every HTML element within your published site. Use this file as data source when removing unused CSS from your site. This process is also known as pruning, purging, or tree shaking. Default isfalse.
- disableIDs
- (
bool) Whether to excludeidattributes. Default isfalse. - (
bool) Whether to exclude element tags. Default isfalse. - disableClasses
- (
bool) Whether to excludeclassattributes. Default isfalse.
Given that CSS purging is typically limited to production builds, place the buildStats object below config/production.
Built for speed, there may be “false positive” detections (e.g., HTML elements that are not HTML elements) while parsing the published site. These “false positives” are infrequent and inconsequential.
Due to the nature of partial server builds, new HTML entities are added while the server is running, but old values will not be removed until you restart the server or run a regular hugo build.
