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

@@ -170,7 +170,7 @@ func (t *babelTransformation) Transform(ctx *resources.ResourceTransformationCtx
stderr := io.MultiWriter(infoW, &errBuf)
cmdArgs = append(cmdArgs, hexec.WithStderr(stderr))
cmdArgs = append(cmdArgs, hexec.WithStdout(stderr))
cmdArgs = append(cmdArgs, hexec.WithEnviron(hugo.GetExecEnviron(t.rs.WorkingDir, t.rs.Cfg, t.rs.BaseFs.Assets.Fs)))
cmdArgs = append(cmdArgs, hexec.WithEnviron(hugo.GetExecEnviron(t.rs.Cfg.BaseConfig().WorkingDir, t.rs.Cfg, t.rs.BaseFs.Assets.Fs)))
defer os.Remove(compileOutput.Name())

View File

@@ -16,18 +16,16 @@ package htesting
import (
"path/filepath"
"github.com/gohugoio/hugo/cache/filecache"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/config/testconfig"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/media"
"github.com/gohugoio/hugo/output"
"github.com/gohugoio/hugo/resources"
"github.com/spf13/afero"
)
func NewTestResourceSpec() (*resources.Spec, error) {
cfg := config.NewWithTestDefaults()
cfg := config.New()
imagingCfg := map[string]any{
"resampleFilter": "linear",
@@ -36,20 +34,16 @@ func NewTestResourceSpec() (*resources.Spec, error) {
}
cfg.Set("imaging", imagingCfg)
afs := afero.NewMemMapFs()
fs := hugofs.NewFrom(hugofs.NewBaseFileDecorator(afero.NewMemMapFs()), cfg)
s, err := helpers.NewPathSpec(fs, cfg, nil)
conf := testconfig.GetTestConfig(afs, cfg)
fs := hugofs.NewFrom(hugofs.NewBaseFileDecorator(afs), conf.BaseConfig())
s, err := helpers.NewPathSpec(fs, conf, nil)
if err != nil {
return nil, err
}
filecaches, err := filecache.NewCaches(s)
if err != nil {
return nil, err
}
spec, err := resources.NewSpec(s, filecaches, nil, nil, nil, nil, output.DefaultFormats, media.DefaultTypes)
spec, err := resources.NewSpec(s, nil, nil, nil, nil, nil)
return spec, err
}

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)

View File

@@ -30,7 +30,7 @@ type Client struct {
// New creates a new Client given a specification. Note that it is the media types
// configured for the site that is used to match files to the correct minifier.
func New(rs *resources.Spec) (*Client, error) {
m, err := minifiers.New(rs.MediaTypes, rs.OutputFormats, rs.Cfg)
m, err := minifiers.New(rs.MediaTypes(), rs.OutputFormats(), rs.Cfg)
if err != nil {
return nil, err
}

View File

@@ -199,7 +199,7 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC
stderr := io.MultiWriter(infoW, &errBuf)
cmdArgs = append(cmdArgs, hexec.WithStderr(stderr))
cmdArgs = append(cmdArgs, hexec.WithStdout(ctx.To))
cmdArgs = append(cmdArgs, hexec.WithEnviron(hugo.GetExecEnviron(t.rs.WorkingDir, t.rs.Cfg, t.rs.BaseFs.Assets.Fs)))
cmdArgs = append(cmdArgs, hexec.WithEnviron(hugo.GetExecEnviron(t.rs.Cfg.BaseConfig().WorkingDir, t.rs.Cfg, t.rs.BaseFs.Assets.Fs)))
cmd, err := ex.Npx(binaryName, cmdArgs...)
if err != nil {
@@ -382,10 +382,13 @@ func (imp *importResolver) resolve() (io.Reader, error) {
// See https://www.w3schools.com/cssref/pr_import_rule.asp
// We currently only support simple file imports, no urls, no media queries.
// So this is OK:
// @import "navigation.css";
//
// @import "navigation.css";
//
// This is not:
// @import url("navigation.css");
// @import "mobstyle.css" screen and (max-width: 768px);
//
// @import url("navigation.css");
// @import "mobstyle.css" screen and (max-width: 768px);
func (imp *importResolver) shouldImport(s string) bool {
if !strings.HasPrefix(s, importIdentifier) {
return false

View File

@@ -59,7 +59,7 @@ func (t *transform) Key() internal.ResourceTransformationKey {
}
func (t *transform) Transform(ctx *resources.ResourceTransformationCtx) error {
ctx.OutMediaType = media.CSSType
ctx.OutMediaType = media.Builtin.CSSType
opts, err := decodeOptions(t.optsm)
if err != nil {
@@ -102,7 +102,7 @@ func (t *transform) Transform(ctx *resources.ResourceTransformationCtx) error {
}
}
if ctx.InMediaType.SubType == media.SASSType.SubType {
if ctx.InMediaType.SubType == media.Builtin.SASSType.SubType {
args.SourceSyntax = godartsass.SourceSyntaxSASS
}

View File

@@ -40,7 +40,7 @@ func Supports() bool {
}
func (t *toCSSTransformation) Transform(ctx *resources.ResourceTransformationCtx) error {
ctx.OutMediaType = media.CSSType
ctx.OutMediaType = media.Builtin.CSSType
var outName string
if t.options.from.TargetPath != "" {
@@ -124,14 +124,14 @@ func (t *toCSSTransformation) Transform(ctx *resources.ResourceTransformationCtx
return "", "", false
}
if ctx.InMediaType.SubType == media.SASSType.SubType {
if ctx.InMediaType.SubType == media.Builtin.SASSType.SubType {
options.to.SassSyntax = true
}
if options.from.EnableSourceMap {
options.to.SourceMapOptions.Filename = outName + ".map"
options.to.SourceMapOptions.Root = t.c.rs.WorkingDir
options.to.SourceMapOptions.Root = t.c.rs.Cfg.BaseConfig().WorkingDir
// Setting this to the relative input filename will get the source map
// more correct for the main entry path (main.scss typically), but
@@ -159,8 +159,8 @@ func (t *toCSSTransformation) Transform(ctx *resources.ResourceTransformationCtx
if options.from.EnableSourceMap && res.SourceMapContent != "" {
sourcePath := t.c.sfs.RealFilename(ctx.SourcePath)
if strings.HasPrefix(sourcePath, t.c.rs.WorkingDir) {
sourcePath = strings.TrimPrefix(sourcePath, t.c.rs.WorkingDir+helpers.FilePathSeparator)
if strings.HasPrefix(sourcePath, t.c.rs.Cfg.BaseConfig().WorkingDir) {
sourcePath = strings.TrimPrefix(sourcePath, t.c.rs.Cfg.BaseConfig().WorkingDir+helpers.FilePathSeparator)
}
// This needs to be Unix-style slashes, even on Windows.