Configure build
This is the default configuration:
build:
buildStats:
disableClasses: false
disableIDs: false
disableTags: false
enable: false
cacheBusters:
- source: '(postcss|tailwind)\.config\.(js|mjs|cjs)'
target: (css|styles|scss|sass)
cleanDestinationDir:
enable: false
keepDirs:
- '{**/,}.*'
keepFiles:
- '{**/,}.{git,gitignore,gitattributes}'
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|mjs|cjs)'
target = '(css|styles|scss|sass)'
[build.cleanDestinationDir]
enable = false
keepDirs = ['{**/,}.*']
keepFiles = ['{**/,}.{git,gitignore,gitattributes}']
{
"build": {
"buildStats": {
"disableClasses": false,
"disableIDs": false,
"disableTags": false,
"enable": false
},
"cacheBusters": [
{
"source": "(postcss|tailwind)\\.config\\.(js|mjs|cjs)",
"target": "(css|styles|scss|sass)"
}
],
"cleanDestinationDir": {
"enable": false,
"keepDirs": [
"{**/,}.*"
],
"keepFiles": [
"{**/,}.{git,gitignore,gitattributes}"
]
},
"noJSConfigInAssets": false,
"useResourceCacheWhen": "fallback"
}
}
buildStats- See the build stats section below.
cachebusters- See the cache busters section below.
cleanDestinationDir- See the clean destination directory section below.
noJSConfigInAssets- (
bool) Whether to disable writing ajsconfig.jsonin yourassetsdirectory with mapping of imports from runningjs.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.
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, such as 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 hugo build.
Cache busters
Use build.cachebusters to expire specific keys in the resource cache when a watched source file changes, triggering a rebuild of dependent resources such as CSS. For example, use this configuration when using the css.TailwindCSS function:
build:
buildStats:
enable: true
cachebusters:
- source: 'assets/notwatching/hugo_stats\.json'
target: css
- source: '(postcss|tailwind)\.config\.js'
target: css
module:
mounts:
- source: assets
target: assets
- disableWatch: true
source: hugo_stats.json
target: assets/notwatching/hugo_stats.json
security:
exec:
allow:
- ^(dart-)?sass$
- ^go$
- ^git$
- ^node$
- ^postcss$
- ^tailwindcss$
[build]
[build.buildStats]
enable = true
[[build.cachebusters]]
source = 'assets/notwatching/hugo_stats\.json'
target = 'css'
[[build.cachebusters]]
source = '(postcss|tailwind)\.config\.js'
target = 'css'
[module]
[[module.mounts]]
source = 'assets'
target = 'assets'
[[module.mounts]]
disableWatch = true
source = 'hugo_stats.json'
target = 'assets/notwatching/hugo_stats.json'
[security]
[security.exec]
allow = ['^(dart-)?sass$', '^go$', '^git$', '^node$', '^postcss$', '^tailwindcss$']
{
"build": {
"buildStats": {
"enable": true
},
"cachebusters": [
{
"source": "assets/notwatching/hugo_stats\\.json",
"target": "css"
},
{
"source": "(postcss|tailwind)\\.config\\.js",
"target": "css"
}
]
},
"module": {
"mounts": [
{
"source": "assets",
"target": "assets"
},
{
"disableWatch": true,
"source": "hugo_stats.json",
"target": "assets/notwatching/hugo_stats.json"
}
]
},
"security": {
"exec": {
"allow": [
"^(dart-)?sass$",
"^go$",
"^git$",
"^node$",
"^postcss$",
"^tailwindcss$"
]
}
}
}
When buildStats is enabled, Hugo writes a hugo_stats.json file on each build, containing the classes, IDs, and tags used in the rendered output. Changes to this file trigger a rebuild of the CSS. See the css.TailwindCSS function 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, such as$1.
Clean destination directory
Hugo does not clear the public directory before building your project. Existing files are overwritten, but not deleted. This behavior is intentional, preventing the inadvertent removal of files that you may have added to the public directory after the build.
As a result, the public directory can accumulate stale files over time. For example, a rendered page may remain after you delete or rename its content file, or draft, expired, and future content may remain after it no longer meets the criteria for publication. Enable cleanDestinationDir to have Hugo remove these stale files automatically on every build.
This is the default configuration:
build:
cleanDestinationDir:
enable: false
keepDirs:
- '{**/,}.*'
keepFiles:
- '{**/,}.{git,gitignore,gitattributes}'
[build]
[build.cleanDestinationDir]
enable = false
keepDirs = ['{**/,}.*']
keepFiles = ['{**/,}.{git,gitignore,gitattributes}']
{
"build": {
"cleanDestinationDir": {
"enable": false,
"keepDirs": [
"{**/,}.*"
],
"keepFiles": [
"{**/,}.{git,gitignore,gitattributes}"
]
}
}
}
enable- New in v0.167.0
- (
bool) Whether to clean thepublicdirectory before rendering the site. Hugo removes every file and directory in thepublicdirectory that does not have a corresponding static file, whether from the project’sstaticdirectory, a module mount, or a theme. This removes stale files, such as old rendered pages and deleted static assets, as well as files you added to thepublicdirectory yourself, such as aCNAMEor_redirectsfile. Use thekeepDirsandkeepFilessettings to preserve specific directories and files. This cleanup runs even if the project has no static files. Default isfalse. Override this setting for a single build with the--cleanDestinationDircommand line flag. keepDirs- New in v0.167.0
- (
[]string) A glob slice matching directories, relative to thepublicdirectory, to preserve when cleaning the destination directory. In a multilingual multihost project, patterns are relative to each language’s subdirectory of thepublicdirectory. A matching directory is kept along with everything beneath it, including subdirectories and their contents. The default value, shown above, matches directories whose names begin with a dot, wherever they occur in the directory tree. A value you set replaces the default rather than adding to it, so include the default pattern to continue preserving these directories. keepFiles- New in v0.167.0
- (
[]string) A glob slice matching files, relative to thepublicdirectory, to preserve when cleaning the destination directory. In a multilingual multihost project, patterns are relative to each language’s subdirectory of thepublicdirectory. The default value, shown above, matches.git,.gitignore, and.gitattributesfiles, wherever they occur in the directory tree. A value you set replaces the default rather than adding to it, so include the default pattern to continue preserving these files.
