diff --git a/tpl/resources/init.go b/tpl/resources/init.go index 9ce9bee20..0cda68c4c 100644 --- a/tpl/resources/init.go +++ b/tpl/resources/init.go @@ -90,6 +90,11 @@ func init() { [][2]string{}, ) + ns.AddMethodMapping(ctx.Publish, + nil, + [][2]string{}, + ) + ns.AddMethodMapping(ctx.PostProcess, nil, [][2]string{}, diff --git a/tpl/resources/resources.go b/tpl/resources/resources.go index aae6ac1f3..88722fb07 100644 --- a/tpl/resources/resources.go +++ b/tpl/resources/resources.go @@ -296,6 +296,18 @@ func (ns *Namespace) Minify(r resources.ResourceTransformer) (resource.Resource, return ns.minifyClient.Minify(r) } +// Publish publishes r to the destination and returns it. +func (ns *Namespace) Publish(r resource.Resource) (resource.Resource, error) { + s, ok := r.(resource.Source) + if !ok { + return nil, fmt.Errorf("%T can not be published", r) + } + if err := s.Publish(); err != nil { + return nil, err + } + return r, nil +} + // PostProcess processes r after the build. // // Deprecated: Use templates.Defer instead. diff --git a/tpl/resources/resources_integration_test.go b/tpl/resources/resources_integration_test.go index b7f86111c..b5c611d52 100644 --- a/tpl/resources/resources_integration_test.go +++ b/tpl/resources/resources_integration_test.go @@ -278,6 +278,26 @@ disableKinds = ['page','section','rss','sitemap','taxonomy','term'] b.AssertLogContains("! WARN Dart Sass: hugo:vars") } +// See issue 15208. +func TestPublish(t *testing.T) { + t.Parallel() + + files := ` +-- hugo.toml -- +disableKinds = ["taxonomy", "term", "section", "RSS", "sitemap", "robotsTXT", "404"] +-- assets/js/main.js -- +let foo; +-- layouts/home.html -- +{{ $r := resources.Get "js/main.js" | minify | resources.Publish }} +Name: {{ $r.Name }}| +` + + b := hugolib.Test(t, files) + + b.AssertFileContent("public/index.html", "Name: /js/main.js|") + b.AssertFileExists("public/js/main.min.js", true) +} + // See issue 15086. func TestPostProcessDeprecated(t *testing.T) { t.Parallel()