Create a struct with all of Hugo's config options

Primary motivation is documentation, but it will also hopefully simplify the code.

Also,

* Lower case the default output format names; this is in line with the custom ones (map keys) and how
it's treated all the places. This avoids doing `stringds.EqualFold` everywhere.

Closes #10896
Closes #10620
This commit is contained in:
Bjørn Erik Pedersen
2023-01-04 18:24:36 +01:00
parent 6aededf6b4
commit 241b21b0fd
337 changed files with 13377 additions and 14898 deletions

View File

@@ -27,12 +27,12 @@ import (
"github.com/spf13/afero"
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/media"
"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/text"
"github.com/gohugoio/hugo/hugolib/filesystems"
"github.com/gohugoio/hugo/media"
"github.com/gohugoio/hugo/resources/internal"
"github.com/evanw/esbuild/pkg/api"
@@ -64,7 +64,7 @@ func (t *buildTransformation) Key() internal.ResourceTransformationKey {
}
func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx) error {
ctx.OutMediaType = media.JavascriptType
ctx.OutMediaType = media.Builtin.JavascriptType
opts, err := decodeOptions(t.optsm)
if err != nil {
@@ -83,7 +83,7 @@ func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx
}
opts.sourceDir = filepath.FromSlash(path.Dir(ctx.SourcePath))
opts.resolveDir = t.c.rs.WorkingDir // where node_modules gets resolved
opts.resolveDir = t.c.rs.Cfg.BaseConfig().WorkingDir // where node_modules gets resolved
opts.contents = string(src)
opts.mediaType = ctx.InMediaType

View File

@@ -337,20 +337,20 @@ func toBuildOptions(opts Options) (buildOptions api.BuildOptions, err error) {
mediaType := opts.mediaType
if mediaType.IsZero() {
mediaType = media.JavascriptType
mediaType = media.Builtin.JavascriptType
}
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.
case media.JavascriptType.SubType:
case media.Builtin.JavascriptType.SubType:
loader = api.LoaderJS
case media.TypeScriptType.SubType:
case media.Builtin.TypeScriptType.SubType:
loader = api.LoaderTS
case media.TSXType.SubType:
case media.Builtin.TSXType.SubType:
loader = api.LoaderTSX
case media.JSXType.SubType:
case media.Builtin.JSXType.SubType:
loader = api.LoaderJSX
default:
err = fmt.Errorf("unsupported Media Type: %q", opts.mediaType)

View File

@@ -18,11 +18,10 @@ import (
"testing"
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/media"
"github.com/spf13/afero"
"github.com/gohugoio/hugo/media"
"github.com/evanw/esbuild/pkg/api"
qt "github.com/frankban/quicktest"
@@ -46,7 +45,7 @@ func TestOptionKey(t *testing.T) {
func TestToBuildOptions(t *testing.T) {
c := qt.New(t)
opts, err := toBuildOptions(Options{mediaType: media.JavascriptType})
opts, err := toBuildOptions(Options{mediaType: media.Builtin.JavascriptType})
c.Assert(err, qt.IsNil)
c.Assert(opts, qt.DeepEquals, api.BuildOptions{
@@ -62,7 +61,7 @@ func TestToBuildOptions(t *testing.T) {
Target: "es2018",
Format: "cjs",
Minify: true,
mediaType: media.JavascriptType,
mediaType: media.Builtin.JavascriptType,
AvoidTDZ: true,
})
c.Assert(err, qt.IsNil)
@@ -79,7 +78,7 @@ func TestToBuildOptions(t *testing.T) {
})
opts, err = toBuildOptions(Options{
Target: "es2018", Format: "cjs", Minify: true, mediaType: media.JavascriptType,
Target: "es2018", Format: "cjs", Minify: true, mediaType: media.Builtin.JavascriptType,
SourceMap: "inline",
})
c.Assert(err, qt.IsNil)
@@ -97,7 +96,7 @@ func TestToBuildOptions(t *testing.T) {
})
opts, err = toBuildOptions(Options{
Target: "es2018", Format: "cjs", Minify: true, mediaType: media.JavascriptType,
Target: "es2018", Format: "cjs", Minify: true, mediaType: media.Builtin.JavascriptType,
SourceMap: "inline",
})
c.Assert(err, qt.IsNil)
@@ -115,7 +114,7 @@ func TestToBuildOptions(t *testing.T) {
})
opts, err = toBuildOptions(Options{
Target: "es2018", Format: "cjs", Minify: true, mediaType: media.JavascriptType,
Target: "es2018", Format: "cjs", Minify: true, mediaType: media.Builtin.JavascriptType,
SourceMap: "external",
})
c.Assert(err, qt.IsNil)