2020-10-05 07:34:14 -04:00
|
|
|
// Copyright 2020 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package js
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-02-18 17:43:26 -05:00
|
|
|
"os"
|
2020-10-05 07:34:14 -04:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2021-06-09 04:58:18 -04:00
|
|
|
"github.com/gohugoio/hugo/common/maps"
|
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/common/paths"
|
|
|
|
"github.com/gohugoio/hugo/identity"
|
2021-06-09 04:58:18 -04:00
|
|
|
"github.com/spf13/afero"
|
2020-10-05 07:34:14 -04:00
|
|
|
|
|
|
|
"github.com/evanw/esbuild/pkg/api"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
|
|
|
"github.com/gohugoio/hugo/media"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
2020-11-04 10:13:37 -05:00
|
|
|
const (
|
|
|
|
nsImportHugo = "ns-hugo"
|
|
|
|
nsParams = "ns-params"
|
|
|
|
|
|
|
|
stdinImporter = "<stdin>"
|
|
|
|
)
|
|
|
|
|
2020-10-05 07:34:14 -04:00
|
|
|
// Options esbuild configuration
|
|
|
|
type Options struct {
|
|
|
|
// If not set, the source path will be used as the base target path.
|
|
|
|
// Note that the target path's extension may change if the target MIME type
|
|
|
|
// is different, e.g. when the source is TypeScript.
|
|
|
|
TargetPath string
|
|
|
|
|
|
|
|
// Whether to minify to output.
|
|
|
|
Minify bool
|
|
|
|
|
|
|
|
// Whether to write mapfiles
|
|
|
|
SourceMap string
|
|
|
|
|
|
|
|
// The language target.
|
|
|
|
// One of: es2015, es2016, es2017, es2018, es2019, es2020 or esnext.
|
|
|
|
// Default is esnext.
|
|
|
|
Target string
|
|
|
|
|
|
|
|
// The output format.
|
|
|
|
// One of: iife, cjs, esm
|
|
|
|
// Default is to esm.
|
|
|
|
Format string
|
|
|
|
|
|
|
|
// External dependencies, e.g. "react".
|
2021-01-21 10:52:32 -05:00
|
|
|
Externals []string
|
2020-10-05 07:34:14 -04:00
|
|
|
|
2021-01-22 11:07:23 -05:00
|
|
|
// This option allows you to automatically replace a global variable with an import from another file.
|
|
|
|
// The filenames must be relative to /assets.
|
|
|
|
// See https://esbuild.github.io/api/#inject
|
|
|
|
Inject []string
|
|
|
|
|
2020-10-05 07:34:14 -04:00
|
|
|
// User defined symbols.
|
2022-03-17 17:03:27 -04:00
|
|
|
Defines map[string]any
|
2020-10-05 07:34:14 -04:00
|
|
|
|
2021-01-21 10:52:32 -05:00
|
|
|
// Maps a component import to another.
|
|
|
|
Shims map[string]string
|
|
|
|
|
2020-10-05 07:34:14 -04:00
|
|
|
// User defined params. Will be marshaled to JSON and available as "@params", e.g.
|
|
|
|
// import * as params from '@params';
|
2022-03-17 17:03:27 -04:00
|
|
|
Params any
|
2020-10-05 07:34:14 -04:00
|
|
|
|
|
|
|
// What to use instead of React.createElement.
|
|
|
|
JSXFactory string
|
|
|
|
|
|
|
|
// What to use instead of React.Fragment.
|
|
|
|
JSXFragment string
|
|
|
|
|
2024-02-22 11:58:22 -05:00
|
|
|
// What to do about JSX syntax.
|
|
|
|
// See https://esbuild.github.io/api/#jsx
|
|
|
|
JSX string
|
|
|
|
|
|
|
|
// Which library to use to automatically import JSX helper functions from. Only works if JSX is set to automatic.
|
|
|
|
// See https://esbuild.github.io/api/#jsx-import-source
|
|
|
|
JSXImportSource string
|
|
|
|
|
2020-11-03 03:05:09 -05:00
|
|
|
// There is/was a bug in WebKit with severe performance issue with the tracking
|
|
|
|
// of TDZ checks in JavaScriptCore.
|
|
|
|
//
|
|
|
|
// Enabling this flag removes the TDZ and `const` assignment checks and
|
|
|
|
// may improve performance of larger JS codebases until the WebKit fix
|
|
|
|
// is in widespread use.
|
|
|
|
//
|
|
|
|
// See https://bugs.webkit.org/show_bug.cgi?id=199866
|
2020-11-25 11:39:51 -05:00
|
|
|
// Deprecated: This no longer have any effect and will be removed.
|
|
|
|
// TODO(bep) remove. See https://github.com/evanw/esbuild/commit/869e8117b499ca1dbfc5b3021938a53ffe934dba
|
2020-11-03 03:05:09 -05:00
|
|
|
AvoidTDZ bool
|
|
|
|
|
2020-10-05 07:34:14 -04:00
|
|
|
mediaType media.Type
|
|
|
|
outDir string
|
|
|
|
contents string
|
2021-02-01 06:49:08 -05:00
|
|
|
sourceDir string
|
2020-10-05 07:34:14 -04:00
|
|
|
resolveDir string
|
|
|
|
tsConfig string
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func decodeOptions(m map[string]any) (Options, error) {
|
2020-10-05 07:34:14 -04:00
|
|
|
var opts Options
|
|
|
|
|
|
|
|
if err := mapstructure.WeakDecode(m, &opts); err != nil {
|
|
|
|
return opts, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.TargetPath != "" {
|
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
|
|
|
opts.TargetPath = paths.ToSlashTrimLeading(opts.TargetPath)
|
2020-10-05 07:34:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
opts.Target = strings.ToLower(opts.Target)
|
|
|
|
opts.Format = strings.ToLower(opts.Format)
|
|
|
|
|
|
|
|
return opts, nil
|
|
|
|
}
|
|
|
|
|
2020-11-04 10:13:37 -05:00
|
|
|
var extensionToLoaderMap = map[string]api.Loader{
|
|
|
|
".js": api.LoaderJS,
|
|
|
|
".mjs": api.LoaderJS,
|
|
|
|
".cjs": api.LoaderJS,
|
|
|
|
".jsx": api.LoaderJSX,
|
|
|
|
".ts": api.LoaderTS,
|
|
|
|
".tsx": api.LoaderTSX,
|
|
|
|
".css": api.LoaderCSS,
|
|
|
|
".json": api.LoaderJSON,
|
|
|
|
".txt": api.LoaderText,
|
|
|
|
}
|
|
|
|
|
|
|
|
func loaderFromFilename(filename string) api.Loader {
|
|
|
|
l, found := extensionToLoaderMap[filepath.Ext(filename)]
|
|
|
|
if found {
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
return api.LoaderJS
|
|
|
|
}
|
|
|
|
|
2021-07-13 05:41:02 -04:00
|
|
|
func resolveComponentInAssets(fs afero.Fs, impPath string) *hugofs.FileMeta {
|
|
|
|
findFirst := func(base string) *hugofs.FileMeta {
|
2021-01-22 11:07:23 -05:00
|
|
|
// This is the most common sub-set of ESBuild's default extensions.
|
|
|
|
// We assume that imports of JSON, CSS etc. will be using their full
|
|
|
|
// name with extension.
|
|
|
|
for _, ext := range []string{".js", ".ts", ".tsx", ".jsx"} {
|
2021-09-01 03:07:29 -04:00
|
|
|
if strings.HasSuffix(impPath, ext) {
|
|
|
|
// Import of foo.js.js need the full name.
|
2022-12-13 08:58:55 -05:00
|
|
|
continue
|
2021-09-01 03:07:29 -04:00
|
|
|
}
|
2021-01-22 11:07:23 -05:00
|
|
|
if fi, err := fs.Stat(base + ext); err == nil {
|
|
|
|
return fi.(hugofs.FileMetaInfo).Meta()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not found.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-13 05:41:02 -04:00
|
|
|
var m *hugofs.FileMeta
|
2021-01-22 11:07:23 -05:00
|
|
|
|
2021-09-01 03:07:29 -04:00
|
|
|
// We need to check if this is a regular file imported without an extension.
|
2023-02-18 15:47:35 -05:00
|
|
|
// There may be ambiguous situations where both foo.js and foo/index.js exists.
|
2021-09-01 03:07:29 -04:00
|
|
|
// This import order is in line with both how Node and ESBuild's native
|
|
|
|
// import resolver works.
|
|
|
|
|
|
|
|
// It may be a regular file imported without an extension, e.g.
|
|
|
|
// foo or foo/index.
|
|
|
|
m = findFirst(impPath)
|
|
|
|
if m != nil {
|
|
|
|
return m
|
|
|
|
}
|
2022-12-13 08:58:55 -05:00
|
|
|
|
|
|
|
base := filepath.Base(impPath)
|
|
|
|
if base == "index" {
|
|
|
|
// try index.esm.js etc.
|
2022-06-12 12:42:53 -04:00
|
|
|
m = findFirst(impPath + ".esm")
|
|
|
|
if m != nil {
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
}
|
2021-09-01 03:07:29 -04:00
|
|
|
|
2022-12-13 08:58:55 -05:00
|
|
|
// Check the path as is.
|
2021-01-22 11:07:23 -05:00
|
|
|
fi, err := fs.Stat(impPath)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if fi.IsDir() {
|
|
|
|
m = findFirst(filepath.Join(impPath, "index"))
|
2022-06-12 12:42:53 -04:00
|
|
|
if m == nil {
|
|
|
|
m = findFirst(filepath.Join(impPath, "index.esm"))
|
|
|
|
}
|
2021-01-22 11:07:23 -05:00
|
|
|
} else {
|
|
|
|
m = fi.(hugofs.FileMetaInfo).Meta()
|
|
|
|
}
|
2022-12-13 08:58:55 -05:00
|
|
|
} else if strings.HasSuffix(base, ".js") {
|
|
|
|
m = findFirst(strings.TrimSuffix(impPath, ".js"))
|
2021-01-22 11:07:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func createBuildPlugins(depsManager identity.Manager, c *Client, opts Options) ([]api.Plugin, error) {
|
2020-10-05 07:34:14 -04:00
|
|
|
fs := c.rs.Assets
|
|
|
|
|
|
|
|
resolveImport := func(args api.OnResolveArgs) (api.OnResolveResult, error) {
|
2021-01-21 10:52:32 -05:00
|
|
|
impPath := args.Path
|
|
|
|
if opts.Shims != nil {
|
|
|
|
override, found := opts.Shims[impPath]
|
|
|
|
if found {
|
|
|
|
impPath = override
|
|
|
|
}
|
|
|
|
}
|
2020-11-04 10:13:37 -05:00
|
|
|
isStdin := args.Importer == stdinImporter
|
|
|
|
var relDir string
|
|
|
|
if !isStdin {
|
2024-02-20 08:46:03 -05:00
|
|
|
rel, found := fs.MakePathRelative(args.Importer, true)
|
2020-11-13 02:54:29 -05:00
|
|
|
if !found {
|
|
|
|
// Not in any of the /assets folders.
|
|
|
|
// This is an import from a node_modules, let
|
|
|
|
// ESBuild resolve this.
|
|
|
|
return api.OnResolveResult{}, nil
|
|
|
|
}
|
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
|
|
|
|
2020-11-13 02:54:29 -05:00
|
|
|
relDir = filepath.Dir(rel)
|
2020-11-04 10:13:37 -05:00
|
|
|
} else {
|
2021-02-01 06:49:08 -05:00
|
|
|
relDir = opts.sourceDir
|
2020-10-05 07:34:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Imports not starting with a "." is assumed to live relative to /assets.
|
|
|
|
// Hugo makes no assumptions about the directory structure below /assets.
|
2020-11-04 10:13:37 -05:00
|
|
|
if relDir != "" && strings.HasPrefix(impPath, ".") {
|
|
|
|
impPath = filepath.Join(relDir, impPath)
|
2020-10-05 07:34:14 -04:00
|
|
|
}
|
|
|
|
|
2021-01-22 11:07:23 -05:00
|
|
|
m := resolveComponentInAssets(fs.Fs, impPath)
|
2020-10-05 07:34:14 -04:00
|
|
|
|
|
|
|
if m != nil {
|
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
|
|
|
depsManager.AddIdentity(m.PathInfo)
|
|
|
|
|
2020-10-05 07:34:14 -04:00
|
|
|
// Store the source root so we can create a jsconfig.json
|
2023-05-22 13:11:12 -04:00
|
|
|
// to help IntelliSense when the build is done.
|
2020-10-05 07:34:14 -04:00
|
|
|
// This should be a small number of elements, and when
|
|
|
|
// in server mode, we may get stale entries on renames etc.,
|
|
|
|
// but that shouldn't matter too much.
|
2021-07-13 05:41:02 -04:00
|
|
|
c.rs.JSConfigBuilder.AddSourceRoot(m.SourceRoot)
|
|
|
|
return api.OnResolveResult{Path: m.Filename, Namespace: nsImportHugo}, nil
|
2020-10-05 07:34:14 -04:00
|
|
|
}
|
|
|
|
|
2020-11-13 02:54:29 -05:00
|
|
|
// Fall back to ESBuild's resolve.
|
2020-10-05 07:34:14 -04:00
|
|
|
return api.OnResolveResult{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
importResolver := api.Plugin{
|
|
|
|
Name: "hugo-import-resolver",
|
|
|
|
Setup: func(build api.PluginBuild) {
|
|
|
|
build.OnResolve(api.OnResolveOptions{Filter: `.*`},
|
|
|
|
func(args api.OnResolveArgs) (api.OnResolveResult, error) {
|
2020-11-13 02:54:29 -05:00
|
|
|
return resolveImport(args)
|
2020-10-05 07:34:14 -04:00
|
|
|
})
|
2020-11-04 10:13:37 -05:00
|
|
|
build.OnLoad(api.OnLoadOptions{Filter: `.*`, Namespace: nsImportHugo},
|
|
|
|
func(args api.OnLoadArgs) (api.OnLoadResult, error) {
|
2023-02-18 17:43:26 -05:00
|
|
|
b, err := os.ReadFile(args.Path)
|
2020-11-04 10:13:37 -05:00
|
|
|
if err != nil {
|
2022-05-02 10:07:52 -04:00
|
|
|
return api.OnLoadResult{}, fmt.Errorf("failed to read %q: %w", args.Path, err)
|
2020-11-04 10:13:37 -05:00
|
|
|
}
|
|
|
|
c := string(b)
|
|
|
|
return api.OnLoadResult{
|
|
|
|
// See https://github.com/evanw/esbuild/issues/502
|
|
|
|
// This allows all modules to resolve dependencies
|
|
|
|
// in the main project's node_modules.
|
|
|
|
ResolveDir: opts.resolveDir,
|
|
|
|
Contents: &c,
|
|
|
|
Loader: loaderFromFilename(args.Path),
|
|
|
|
}, nil
|
|
|
|
})
|
2020-10-05 07:34:14 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
params := opts.Params
|
|
|
|
if params == nil {
|
|
|
|
// This way @params will always resolve to something.
|
2022-03-17 17:03:27 -04:00
|
|
|
params = make(map[string]any)
|
2020-10-05 07:34:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(params)
|
|
|
|
if err != nil {
|
2022-05-02 10:07:52 -04:00
|
|
|
return nil, fmt.Errorf("failed to marshal params: %w", err)
|
2020-10-05 07:34:14 -04:00
|
|
|
}
|
|
|
|
bs := string(b)
|
|
|
|
paramsPlugin := api.Plugin{
|
|
|
|
Name: "hugo-params-plugin",
|
|
|
|
Setup: func(build api.PluginBuild) {
|
|
|
|
build.OnResolve(api.OnResolveOptions{Filter: `^@params$`},
|
|
|
|
func(args api.OnResolveArgs) (api.OnResolveResult, error) {
|
|
|
|
return api.OnResolveResult{
|
|
|
|
Path: args.Path,
|
2020-11-04 10:13:37 -05:00
|
|
|
Namespace: nsParams,
|
2020-10-05 07:34:14 -04:00
|
|
|
}, nil
|
|
|
|
})
|
2020-11-04 10:13:37 -05:00
|
|
|
build.OnLoad(api.OnLoadOptions{Filter: `.*`, Namespace: nsParams},
|
2020-10-05 07:34:14 -04:00
|
|
|
func(args api.OnLoadArgs) (api.OnLoadResult, error) {
|
|
|
|
return api.OnLoadResult{
|
|
|
|
Contents: &bs,
|
|
|
|
Loader: api.LoaderJSON,
|
|
|
|
}, nil
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return []api.Plugin{importResolver, paramsPlugin}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toBuildOptions(opts Options) (buildOptions api.BuildOptions, err error) {
|
|
|
|
var target api.Target
|
|
|
|
switch opts.Target {
|
|
|
|
case "", "esnext":
|
|
|
|
target = api.ESNext
|
|
|
|
case "es5":
|
|
|
|
target = api.ES5
|
|
|
|
case "es6", "es2015":
|
|
|
|
target = api.ES2015
|
|
|
|
case "es2016":
|
|
|
|
target = api.ES2016
|
|
|
|
case "es2017":
|
|
|
|
target = api.ES2017
|
|
|
|
case "es2018":
|
|
|
|
target = api.ES2018
|
|
|
|
case "es2019":
|
|
|
|
target = api.ES2019
|
|
|
|
case "es2020":
|
|
|
|
target = api.ES2020
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("invalid target: %q", opts.Target)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mediaType := opts.mediaType
|
|
|
|
if mediaType.IsZero() {
|
2023-01-04 12:24:36 -05:00
|
|
|
mediaType = media.Builtin.JavascriptType
|
2020-10-05 07:34:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var loader api.Loader
|
|
|
|
switch mediaType.SubType {
|
|
|
|
// TODO(bep) ESBuild support a set of other loaders, but I currently fail
|
|
|
|
// to see the relevance. That may change as we start using this.
|
2023-01-04 12:24:36 -05:00
|
|
|
case media.Builtin.JavascriptType.SubType:
|
2020-10-05 07:34:14 -04:00
|
|
|
loader = api.LoaderJS
|
2023-01-04 12:24:36 -05:00
|
|
|
case media.Builtin.TypeScriptType.SubType:
|
2020-10-05 07:34:14 -04:00
|
|
|
loader = api.LoaderTS
|
2023-01-04 12:24:36 -05:00
|
|
|
case media.Builtin.TSXType.SubType:
|
2020-10-05 07:34:14 -04:00
|
|
|
loader = api.LoaderTSX
|
2023-01-04 12:24:36 -05:00
|
|
|
case media.Builtin.JSXType.SubType:
|
2020-10-05 07:34:14 -04:00
|
|
|
loader = api.LoaderJSX
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unsupported Media Type: %q", opts.mediaType)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var format api.Format
|
|
|
|
// One of: iife, cjs, esm
|
|
|
|
switch opts.Format {
|
|
|
|
case "", "iife":
|
|
|
|
format = api.FormatIIFE
|
|
|
|
case "esm":
|
|
|
|
format = api.FormatESModule
|
|
|
|
case "cjs":
|
|
|
|
format = api.FormatCommonJS
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unsupported script output format: %q", opts.Format)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-22 11:58:22 -05:00
|
|
|
var jsx api.JSX
|
|
|
|
switch opts.JSX {
|
|
|
|
case "", "transform":
|
|
|
|
jsx = api.JSXTransform
|
|
|
|
case "preserve":
|
|
|
|
jsx = api.JSXPreserve
|
|
|
|
case "automatic":
|
|
|
|
jsx = api.JSXAutomatic
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unsupported jsx type: %q", opts.JSX)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-05 07:34:14 -04:00
|
|
|
var defines map[string]string
|
|
|
|
if opts.Defines != nil {
|
2021-06-09 04:58:18 -04:00
|
|
|
defines = maps.ToStringMapString(opts.Defines)
|
2020-10-05 07:34:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// By default we only need to specify outDir and no outFile
|
2020-12-02 07:23:25 -05:00
|
|
|
outDir := opts.outDir
|
|
|
|
outFile := ""
|
2020-10-05 07:34:14 -04:00
|
|
|
var sourceMap api.SourceMap
|
|
|
|
switch opts.SourceMap {
|
|
|
|
case "inline":
|
|
|
|
sourceMap = api.SourceMapInline
|
2021-01-18 04:38:09 -05:00
|
|
|
case "external":
|
|
|
|
sourceMap = api.SourceMapExternal
|
2020-10-05 07:34:14 -04:00
|
|
|
case "":
|
|
|
|
sourceMap = api.SourceMapNone
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unsupported sourcemap type: %q", opts.SourceMap)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buildOptions = api.BuildOptions{
|
|
|
|
Outfile: outFile,
|
|
|
|
Bundle: true,
|
|
|
|
|
|
|
|
Target: target,
|
|
|
|
Format: format,
|
|
|
|
Sourcemap: sourceMap,
|
|
|
|
|
|
|
|
MinifyWhitespace: opts.Minify,
|
|
|
|
MinifyIdentifiers: opts.Minify,
|
|
|
|
MinifySyntax: opts.Minify,
|
|
|
|
|
|
|
|
Outdir: outDir,
|
|
|
|
Define: defines,
|
|
|
|
|
|
|
|
External: opts.Externals,
|
|
|
|
|
|
|
|
JSXFactory: opts.JSXFactory,
|
|
|
|
JSXFragment: opts.JSXFragment,
|
|
|
|
|
2024-02-22 11:58:22 -05:00
|
|
|
JSX: jsx,
|
|
|
|
JSXImportSource: opts.JSXImportSource,
|
|
|
|
|
2020-10-05 07:34:14 -04:00
|
|
|
Tsconfig: opts.tsConfig,
|
|
|
|
|
2021-02-01 04:57:38 -05:00
|
|
|
// Note: We're not passing Sourcefile to ESBuild.
|
|
|
|
// This makes ESBuild pass `stdin` as the Importer to the import
|
|
|
|
// resolver, which is what we need/expect.
|
2020-10-05 07:34:14 -04:00
|
|
|
Stdin: &api.StdinOptions{
|
|
|
|
Contents: opts.contents,
|
|
|
|
ResolveDir: opts.resolveDir,
|
|
|
|
Loader: loader,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|