diff options
author | Bjørn Erik Pedersen <[email protected]> | 2023-01-04 18:24:36 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2023-05-16 18:01:29 +0200 |
commit | 241b21b0fd34d91fccb2ce69874110dceae6f926 (patch) | |
tree | d4e0118eac7e9c42f065815447a70805f8d6ad3e /resources/resource_transformers | |
parent | 6aededf6b42011c3039f5f66487a89a8dd65e0e7 (diff) | |
download | hugo-241b21b0fd34d91fccb2ce69874110dceae6f926.tar.gz hugo-241b21b0fd34d91fccb2ce69874110dceae6f926.zip |
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
Diffstat (limited to 'resources/resource_transformers')
9 files changed, 37 insertions, 41 deletions
diff --git a/resources/resource_transformers/babel/babel.go b/resources/resource_transformers/babel/babel.go index 89d74d9ed..ff19d9dda 100644 --- a/resources/resource_transformers/babel/babel.go +++ b/resources/resource_transformers/babel/babel.go @@ -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()) diff --git a/resources/resource_transformers/htesting/testhelpers.go b/resources/resource_transformers/htesting/testhelpers.go index 3c91fc0dd..75ae4245e 100644 --- a/resources/resource_transformers/htesting/testhelpers.go +++ b/resources/resource_transformers/htesting/testhelpers.go @@ -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) - if err != nil { - return nil, err - } - - filecaches, err := filecache.NewCaches(s) + 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 } - 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 } diff --git a/resources/resource_transformers/js/build.go b/resources/resource_transformers/js/build.go index 34bc2cc12..949cd4fcb 100644 --- a/resources/resource_transformers/js/build.go +++ b/resources/resource_transformers/js/build.go @@ -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 diff --git a/resources/resource_transformers/js/options.go b/resources/resource_transformers/js/options.go index 8b40648e7..1f57709cd 100644 --- a/resources/resource_transformers/js/options.go +++ b/resources/resource_transformers/js/options.go @@ -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) diff --git a/resources/resource_transformers/js/options_test.go b/resources/resource_transformers/js/options_test.go index 135164d18..a76a24caa 100644 --- a/resources/resource_transformers/js/options_test.go +++ b/resources/resource_transformers/js/options_test.go @@ -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) diff --git a/resources/resource_transformers/minifier/minify.go b/resources/resource_transformers/minifier/minify.go index c00d478af..872d284c6 100644 --- a/resources/resource_transformers/minifier/minify.go +++ b/resources/resource_transformers/minifier/minify.go @@ -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 } diff --git a/resources/resource_transformers/postcss/postcss.go b/resources/resource_transformers/postcss/postcss.go index b4234bcf8..376d72182 100644 --- a/resources/resource_transformers/postcss/postcss.go +++ b/resources/resource_transformers/postcss/postcss.go @@ -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 diff --git a/resources/resource_transformers/tocss/dartsass/transform.go b/resources/resource_transformers/tocss/dartsass/transform.go index fdf4d8ef3..61ea54437 100644 --- a/resources/resource_transformers/tocss/dartsass/transform.go +++ b/resources/resource_transformers/tocss/dartsass/transform.go @@ -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 } diff --git a/resources/resource_transformers/tocss/scss/tocss.go b/resources/resource_transformers/tocss/scss/tocss.go index 7e44f327e..1018ea02e 100644 --- a/resources/resource_transformers/tocss/scss/tocss.go +++ b/resources/resource_transformers/tocss/scss/tocss.go @@ -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. |