2017-01-10 04:55:03 -05:00
|
|
|
package deps
|
|
|
|
|
|
|
|
import (
|
2023-03-04 08:43:23 -05:00
|
|
|
"context"
|
2022-05-02 10:07:52 -04:00
|
|
|
"fmt"
|
2023-06-16 02:17:42 -04:00
|
|
|
"io"
|
2022-09-26 11:34:20 -04:00
|
|
|
"path/filepath"
|
2023-01-04 12:24:36 -05:00
|
|
|
"sort"
|
2022-09-26 11:34:20 -04:00
|
|
|
"strings"
|
2018-07-11 13:23:22 -04:00
|
|
|
"sync"
|
2020-02-25 15:40:02 -05:00
|
|
|
"sync/atomic"
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2023-06-16 02:17:42 -04:00
|
|
|
"github.com/bep/logg"
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
"github.com/gohugoio/hugo/cache/dynacache"
|
|
|
|
"github.com/gohugoio/hugo/cache/filecache"
|
2021-12-12 06:11:11 -05:00
|
|
|
"github.com/gohugoio/hugo/common/hexec"
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
"github.com/gohugoio/hugo/common/loggers"
|
2024-06-08 05:52:22 -04:00
|
|
|
"github.com/gohugoio/hugo/common/maps"
|
2024-05-17 11:06:47 -04:00
|
|
|
"github.com/gohugoio/hugo/common/types"
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/config"
|
2023-01-04 12:24:36 -05:00
|
|
|
"github.com/gohugoio/hugo/config/allconfig"
|
2021-12-12 06:11:11 -05:00
|
|
|
"github.com/gohugoio/hugo/config/security"
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/helpers"
|
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
2024-05-17 11:06:47 -04:00
|
|
|
"github.com/gohugoio/hugo/identity"
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
"github.com/gohugoio/hugo/media"
|
2019-01-02 06:33:26 -05:00
|
|
|
"github.com/gohugoio/hugo/resources/page"
|
2022-09-14 05:58:45 -04:00
|
|
|
"github.com/gohugoio/hugo/resources/postpub"
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2017-09-26 14:03:04 -04:00
|
|
|
"github.com/gohugoio/hugo/metrics"
|
2019-01-02 05:58:32 -05:00
|
|
|
"github.com/gohugoio/hugo/resources"
|
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
|
|
|
"github.com/gohugoio/hugo/source"
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/tpl"
|
2023-01-04 12:24:36 -05:00
|
|
|
"github.com/spf13/afero"
|
2017-01-10 04:55:03 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Deps holds dependencies used by many.
|
2017-08-02 08:25:05 -04:00
|
|
|
// There will be normally only one instance of deps in play
|
2017-01-10 04:55:03 -05:00
|
|
|
// at a given time, i.e. one per Site built.
|
|
|
|
type Deps struct {
|
|
|
|
// The logger to use.
|
2020-10-21 05:17:48 -04:00
|
|
|
Log loggers.Logger `json:"-"`
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2021-12-12 06:11:11 -05:00
|
|
|
ExecHelper *hexec.Exec
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
// The templates to use. This will usually implement the full tpl.TemplateManager.
|
2023-01-04 12:24:36 -05:00
|
|
|
tmplHandlers *tpl.TemplateHandlers
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
// The file systems to use.
|
|
|
|
Fs *hugofs.Fs `json:"-"`
|
|
|
|
|
|
|
|
// The PathSpec to use
|
|
|
|
*helpers.PathSpec `json:"-"`
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
// The ContentSpec to use
|
|
|
|
*helpers.ContentSpec `json:"-"`
|
|
|
|
|
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
|
|
|
// The SourceSpec to use
|
|
|
|
SourceSpec *source.SourceSpec `json:"-"`
|
|
|
|
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
// The Resource Spec to use
|
2019-01-02 05:58:32 -05:00
|
|
|
ResourceSpec *resources.Spec
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
// The configuration to use
|
2023-01-04 12:24:36 -05:00
|
|
|
Conf config.AllProvider `json:"-"`
|
2018-11-08 04:24:13 -05:00
|
|
|
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
// The memory cache to use.
|
|
|
|
MemCache *dynacache.Cache
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
// The translation func to use
|
2023-03-04 08:43:23 -05:00
|
|
|
Translate func(ctx context.Context, translationID string, templateData any) string `json:"-"`
|
2017-02-04 22:20:06 -05:00
|
|
|
|
2018-11-26 04:11:22 -05:00
|
|
|
// The site building.
|
2019-01-02 06:33:26 -05:00
|
|
|
Site page.Site
|
2018-11-26 04:11:22 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
TemplateProvider ResourceProvider
|
2019-12-10 13:56:44 -05:00
|
|
|
// Used in tests
|
2022-03-17 17:03:27 -04:00
|
|
|
OverloadedTemplateFuncs map[string]any
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
TranslationProvider ResourceProvider
|
2017-09-26 14:03:04 -04:00
|
|
|
|
|
|
|
Metrics metrics.Provider
|
2018-04-19 12:06:40 -04:00
|
|
|
|
2018-07-11 13:23:22 -04:00
|
|
|
// BuildStartListeners will be notified before a build starts.
|
|
|
|
BuildStartListeners *Listeners
|
2018-10-21 06:20:21 -04:00
|
|
|
|
2023-10-19 04:53:27 -04:00
|
|
|
// BuildEndListeners will be notified after a build finishes.
|
|
|
|
BuildEndListeners *Listeners
|
|
|
|
|
2020-12-23 03:26:23 -05:00
|
|
|
// Resources that gets closed when the build is done or the server shuts down.
|
2024-05-17 11:06:47 -04:00
|
|
|
BuildClosers *types.Closers
|
2020-12-23 03:26:23 -05:00
|
|
|
|
2020-02-25 15:40:02 -05:00
|
|
|
// This is common/global for all sites.
|
|
|
|
BuildState *BuildState
|
2020-01-15 09:59:56 -05:00
|
|
|
|
2018-10-21 06:20:21 -04:00
|
|
|
*globalErrHandler
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (d Deps) Clone(s page.Site, conf config.AllProvider) (*Deps, error) {
|
|
|
|
d.Conf = conf
|
|
|
|
d.Site = s
|
|
|
|
d.ExecHelper = nil
|
|
|
|
d.ContentSpec = nil
|
|
|
|
|
|
|
|
if err := d.Init(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Deps) SetTempl(t *tpl.TemplateHandlers) {
|
|
|
|
d.tmplHandlers = t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Deps) Init() error {
|
|
|
|
if d.Conf == nil {
|
|
|
|
panic("conf is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.Fs == nil {
|
|
|
|
// For tests.
|
|
|
|
d.Fs = hugofs.NewFrom(afero.NewMemMapFs(), d.Conf.BaseConfig())
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.Log == nil {
|
2023-06-16 02:17:42 -04:00
|
|
|
d.Log = loggers.NewDefault()
|
2023-01-04 12:24:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.globalErrHandler == nil {
|
2023-06-16 02:17:42 -04:00
|
|
|
d.globalErrHandler = &globalErrHandler{
|
|
|
|
logger: d.Log,
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.BuildState == nil {
|
|
|
|
d.BuildState = &BuildState{}
|
|
|
|
}
|
2024-06-08 05:52:22 -04:00
|
|
|
if d.BuildState.DeferredExecutions == nil {
|
|
|
|
if d.BuildState.DeferredExecutionsGroupedByRenderingContext == nil {
|
|
|
|
d.BuildState.DeferredExecutionsGroupedByRenderingContext = make(map[tpl.RenderingContext]*DeferredExecutions)
|
|
|
|
}
|
|
|
|
d.BuildState.DeferredExecutions = &DeferredExecutions{
|
|
|
|
Executions: maps.NewCache[string, *tpl.DeferredExecution](),
|
|
|
|
FilenamesWithPostPrefix: maps.NewCache[string, bool](),
|
|
|
|
}
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
|
|
|
|
if d.BuildStartListeners == nil {
|
|
|
|
d.BuildStartListeners = &Listeners{}
|
|
|
|
}
|
|
|
|
|
2023-10-19 04:53:27 -04:00
|
|
|
if d.BuildEndListeners == nil {
|
|
|
|
d.BuildEndListeners = &Listeners{}
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
if d.BuildClosers == nil {
|
2024-05-17 11:06:47 -04:00
|
|
|
d.BuildClosers = &types.Closers{}
|
2023-01-04 12:24:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.Metrics == nil && d.Conf.TemplateMetrics() {
|
|
|
|
d.Metrics = metrics.NewProvider(d.Conf.TemplateMetricsHints())
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.ExecHelper == nil {
|
2024-06-23 06:49:10 -04:00
|
|
|
d.ExecHelper = hexec.New(d.Conf.GetConfigSection("security").(security.Config), d.Conf.WorkingDir())
|
2023-01-04 12:24:36 -05:00
|
|
|
}
|
|
|
|
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
if d.MemCache == nil {
|
2024-04-25 06:30:16 -04:00
|
|
|
d.MemCache = dynacache.New(dynacache.Options{Watching: d.Conf.Watching(), Log: d.Log})
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
if d.PathSpec == nil {
|
2024-06-08 05:52:22 -04:00
|
|
|
hashBytesReceiverFunc := func(name string, match []byte) {
|
|
|
|
s := string(match)
|
|
|
|
switch s {
|
|
|
|
case postpub.PostProcessPrefix:
|
|
|
|
d.BuildState.AddFilenameWithPostPrefix(name)
|
|
|
|
case tpl.HugoDeferredTemplatePrefix:
|
|
|
|
d.BuildState.DeferredExecutions.FilenamesWithPostPrefix.Set(name, true)
|
2023-01-04 12:24:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip binary files.
|
|
|
|
mediaTypes := d.Conf.GetConfigSection("mediaTypes").(media.Types)
|
2024-06-08 05:52:22 -04:00
|
|
|
hashBytesShouldCheck := func(name string) bool {
|
2023-01-04 12:24:36 -05:00
|
|
|
ext := strings.TrimPrefix(filepath.Ext(name), ".")
|
|
|
|
return mediaTypes.IsTextSuffix(ext)
|
|
|
|
}
|
2024-06-08 05:52:22 -04:00
|
|
|
d.Fs.PublishDir = hugofs.NewHasBytesReceiver(
|
|
|
|
d.Fs.PublishDir,
|
|
|
|
hashBytesShouldCheck,
|
|
|
|
hashBytesReceiverFunc,
|
|
|
|
[]byte(tpl.HugoDeferredTemplatePrefix),
|
|
|
|
[]byte(postpub.PostProcessPrefix))
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
pathSpec, err := helpers.NewPathSpec(d.Fs, d.Conf, d.Log)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.PathSpec = pathSpec
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
d.PathSpec, err = helpers.NewPathSpecWithBaseBaseFsProvided(d.Fs, d.Conf, d.Log, d.PathSpec.BaseFs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.ContentSpec == nil {
|
|
|
|
contentSpec, err := helpers.NewContentSpec(d.Conf, d.Log, d.Content.Fs, d.ExecHelper)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.ContentSpec = contentSpec
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.SourceSpec == nil {
|
|
|
|
d.SourceSpec = source.NewSourceSpec(d.PathSpec, nil, d.Fs.Source)
|
|
|
|
}
|
|
|
|
|
|
|
|
var common *resources.SpecCommon
|
|
|
|
if d.ResourceSpec != nil {
|
|
|
|
common = d.ResourceSpec.SpecCommon
|
|
|
|
}
|
2023-05-28 05:35:00 -04:00
|
|
|
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
fileCaches, err := filecache.NewCaches(d.PathSpec)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create file caches from configuration: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
resourceSpec, err := resources.NewSpec(d.PathSpec, common, fileCaches, d.MemCache, d.BuildState, d.Log, d, d.ExecHelper, d.BuildClosers, d.BuildState)
|
2023-01-04 12:24:36 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create resource spec: %w", err)
|
|
|
|
}
|
|
|
|
d.ResourceSpec = resourceSpec
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Deps) Compile(prototype *Deps) error {
|
|
|
|
var err error
|
|
|
|
if prototype == nil {
|
|
|
|
if err = d.TemplateProvider.NewResource(d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = d.TranslationProvider.NewResource(d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = d.TemplateProvider.CloneResource(d, prototype); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = d.TranslationProvider.CloneResource(d, prototype); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-21 06:20:21 -04:00
|
|
|
type globalErrHandler struct {
|
2023-06-16 02:17:42 -04:00
|
|
|
logger loggers.Logger
|
|
|
|
|
2018-10-21 06:20:21 -04:00
|
|
|
// Channel for some "hard to get to" build errors
|
|
|
|
buildErrors chan error
|
2023-04-13 05:44:22 -04:00
|
|
|
// Used to signal that the build is done.
|
|
|
|
quit chan struct{}
|
2018-10-21 06:20:21 -04:00
|
|
|
}
|
|
|
|
|
2023-05-18 05:05:56 -04:00
|
|
|
// SendError sends the error on a channel to be handled later.
|
2018-10-21 06:20:21 -04:00
|
|
|
// This can be used in situations where returning and aborting the current
|
|
|
|
// operation isn't practical.
|
|
|
|
func (e *globalErrHandler) SendError(err error) {
|
|
|
|
if e.buildErrors != nil {
|
|
|
|
select {
|
2023-04-13 05:44:22 -04:00
|
|
|
case <-e.quit:
|
2018-10-21 06:20:21 -04:00
|
|
|
case e.buildErrors <- err:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2023-06-16 02:17:42 -04:00
|
|
|
e.logger.Errorln(err)
|
2018-10-21 06:20:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *globalErrHandler) StartErrorCollector() chan error {
|
2023-04-13 05:44:22 -04:00
|
|
|
e.quit = make(chan struct{})
|
2018-10-21 06:20:21 -04:00
|
|
|
e.buildErrors = make(chan error, 10)
|
|
|
|
return e.buildErrors
|
2018-07-11 13:23:22 -04:00
|
|
|
}
|
|
|
|
|
2023-04-13 05:44:22 -04:00
|
|
|
func (e *globalErrHandler) StopErrorCollector() {
|
|
|
|
if e.buildErrors != nil {
|
|
|
|
close(e.quit)
|
|
|
|
close(e.buildErrors)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 14:23:56 -04:00
|
|
|
// Listeners represents an event listener.
|
2018-07-11 13:23:22 -04:00
|
|
|
type Listeners struct {
|
|
|
|
sync.Mutex
|
|
|
|
|
|
|
|
// A list of funcs to be notified about an event.
|
|
|
|
listeners []func()
|
|
|
|
}
|
|
|
|
|
2018-09-06 14:23:56 -04:00
|
|
|
// Add adds a function to a Listeners instance.
|
2018-07-11 13:23:22 -04:00
|
|
|
func (b *Listeners) Add(f func()) {
|
2018-12-21 10:21:13 -05:00
|
|
|
if b == nil {
|
|
|
|
return
|
|
|
|
}
|
2018-07-11 13:23:22 -04:00
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
b.listeners = append(b.listeners, f)
|
|
|
|
}
|
|
|
|
|
2018-09-06 14:23:56 -04:00
|
|
|
// Notify executes all listener functions.
|
2018-07-11 13:23:22 -04:00
|
|
|
func (b *Listeners) Notify() {
|
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
for _, notify := range b.listeners {
|
|
|
|
notify()
|
|
|
|
}
|
2017-01-10 04:55:03 -05:00
|
|
|
}
|
|
|
|
|
2017-03-09 08:18:12 -05:00
|
|
|
// ResourceProvider is used to create and refresh, and clone resources needed.
|
2017-02-04 22:20:06 -05:00
|
|
|
type ResourceProvider interface {
|
2023-01-04 12:24:36 -05:00
|
|
|
NewResource(dst *Deps) error
|
|
|
|
CloneResource(dst, src *Deps) error
|
2017-01-10 04:55:03 -05:00
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (d *Deps) Tmpl() tpl.TemplateHandler {
|
2023-01-04 12:24:36 -05:00
|
|
|
return d.tmplHandlers.Tmpl
|
2020-01-15 09:59:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Deps) TextTmpl() tpl.TemplateParseFinder {
|
2023-01-04 12:24:36 -05:00
|
|
|
return d.tmplHandlers.TxtTmpl
|
2017-01-10 04:55:03 -05:00
|
|
|
}
|
|
|
|
|
2020-12-23 03:26:23 -05:00
|
|
|
func (d *Deps) Close() error {
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
if d.MemCache != nil {
|
|
|
|
d.MemCache.Stop()
|
|
|
|
}
|
2020-12-23 03:26:23 -05:00
|
|
|
return d.BuildClosers.Close()
|
|
|
|
}
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
// DepsCfg contains configuration options that can be used to configure Hugo
|
|
|
|
// on a global level, i.e. logging etc.
|
|
|
|
// Nil values will be given default values.
|
|
|
|
type DepsCfg struct {
|
2023-06-16 02:17:42 -04:00
|
|
|
// The logger to use. Only set in some tests.
|
|
|
|
// TODO(bep) get rid of this.
|
|
|
|
TestLogger loggers.Logger
|
|
|
|
|
|
|
|
// The logging level to use.
|
|
|
|
LogLevel logg.Level
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2023-06-16 02:17:42 -04:00
|
|
|
// Where to write the logs.
|
|
|
|
// Currently we typically write everything to stdout.
|
|
|
|
LogOut io.Writer
|
2017-01-10 04:55:03 -05:00
|
|
|
|
|
|
|
// The file systems to use
|
|
|
|
Fs *hugofs.Fs
|
|
|
|
|
2018-11-26 04:11:22 -05:00
|
|
|
// The Site in use
|
2019-01-02 06:33:26 -05:00
|
|
|
Site page.Site
|
2018-11-26 04:11:22 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
Configs *allconfig.Configs
|
2018-08-05 05:13:49 -04:00
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
// Template handling.
|
2017-02-04 22:20:06 -05:00
|
|
|
TemplateProvider ResourceProvider
|
|
|
|
|
|
|
|
// i18n handling.
|
|
|
|
TranslationProvider ResourceProvider
|
2024-05-17 11:06:47 -04:00
|
|
|
|
|
|
|
// ChangesFromBuild for changes passed back to the server/watch process.
|
|
|
|
ChangesFromBuild chan []identity.Identity
|
2017-01-10 04:55:03 -05:00
|
|
|
}
|
2020-01-15 09:59:56 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
// BuildState are state used during a build.
|
2020-02-25 15:40:02 -05:00
|
|
|
type BuildState struct {
|
|
|
|
counter uint64
|
2023-01-04 12:24:36 -05:00
|
|
|
|
|
|
|
mu sync.Mutex // protects state below.
|
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
OnSignalRebuild func(ids ...identity.Identity)
|
|
|
|
|
2024-03-15 12:25:52 -04:00
|
|
|
// A set of filenames in /public that
|
2023-01-04 12:24:36 -05:00
|
|
|
// contains a post-processing prefix.
|
|
|
|
filenamesWithPostPrefix map[string]bool
|
2024-06-08 05:52:22 -04:00
|
|
|
|
|
|
|
DeferredExecutions *DeferredExecutions
|
|
|
|
|
|
|
|
// Deferred executions grouped by rendering context.
|
|
|
|
DeferredExecutionsGroupedByRenderingContext map[tpl.RenderingContext]*DeferredExecutions
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeferredExecutions struct {
|
|
|
|
// A set of filenames in /public that
|
|
|
|
// contains a post-processing prefix.
|
|
|
|
FilenamesWithPostPrefix *maps.Cache[string, bool]
|
|
|
|
|
|
|
|
// Maps a placeholder to a deferred execution.
|
|
|
|
Executions *maps.Cache[string, *tpl.DeferredExecution]
|
2020-01-15 09:59:56 -05:00
|
|
|
}
|
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
var _ identity.SignalRebuilder = (*BuildState)(nil)
|
|
|
|
|
2024-06-08 05:52:22 -04:00
|
|
|
// StartStageRender will be called before a stage is rendered.
|
|
|
|
func (b *BuildState) StartStageRender(stage tpl.RenderingContext) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopStageRender will be called after a stage is rendered.
|
|
|
|
func (b *BuildState) StopStageRender(stage tpl.RenderingContext) {
|
|
|
|
b.DeferredExecutionsGroupedByRenderingContext[stage] = b.DeferredExecutions
|
|
|
|
b.DeferredExecutions = &DeferredExecutions{
|
|
|
|
Executions: maps.NewCache[string, *tpl.DeferredExecution](),
|
|
|
|
FilenamesWithPostPrefix: maps.NewCache[string, bool](),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-17 11:06:47 -04:00
|
|
|
func (b *BuildState) SignalRebuild(ids ...identity.Identity) {
|
|
|
|
b.OnSignalRebuild(ids...)
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (b *BuildState) AddFilenameWithPostPrefix(filename string) {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
if b.filenamesWithPostPrefix == nil {
|
|
|
|
b.filenamesWithPostPrefix = make(map[string]bool)
|
|
|
|
}
|
|
|
|
b.filenamesWithPostPrefix[filename] = true
|
2020-02-25 15:40:02 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (b *BuildState) GetFilenamesWithPostPrefix() []string {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
var filenames []string
|
|
|
|
for filename := range b.filenamesWithPostPrefix {
|
|
|
|
filenames = append(filenames, filename)
|
|
|
|
}
|
|
|
|
sort.Strings(filenames)
|
|
|
|
return filenames
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BuildState) Incr() int {
|
|
|
|
return int(atomic.AddUint64(&b.counter, uint64(1)))
|
2020-01-15 09:59:56 -05:00
|
|
|
}
|