Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/tpl
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-07-12 13:47:14 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-07-13 11:56:23 +0300
commit9df98ec49ca9fa326125ccfee626b6e46c6ab14b (patch)
treecdfcfcc9522366d69b7b543638b689852a5e1252 /tpl
parent2fc33807077cd25bf91f2298bf1a8ace126881a7 (diff)
Add proper Media Type handling in js.Build
See #732
Diffstat (limited to 'tpl')
-rw-r--r--tpl/internal/resourcehelpers/helpers.go71
-rw-r--r--tpl/js/js.go62
-rw-r--r--tpl/resources/resources.go60
3 files changed, 100 insertions, 93 deletions
diff --git a/tpl/internal/resourcehelpers/helpers.go b/tpl/internal/resourcehelpers/helpers.go
new file mode 100644
index 000000000..4f8b7539a
--- /dev/null
+++ b/tpl/internal/resourcehelpers/helpers.go
@@ -0,0 +1,71 @@
+// Copyright 2020 The Hugo Authors. All rights reserved.
+//
+// Portions Copyright The Go Authors.
+
+// 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 resourcehelpers
+
+import (
+ "errors"
+ "fmt"
+
+ _errors "github.com/pkg/errors"
+
+ "github.com/gohugoio/hugo/common/maps"
+ "github.com/gohugoio/hugo/resources"
+)
+
+// We allow string or a map as the first argument in some cases.
+func ResolveIfFirstArgIsString(args []interface{}) (resources.ResourceTransformer, string, bool) {
+ if len(args) != 2 {
+ return nil, "", false
+ }
+
+ v1, ok1 := args[0].(string)
+ if !ok1 {
+ return nil, "", false
+ }
+ v2, ok2 := args[1].(resources.ResourceTransformer)
+
+ return v2, v1, ok2
+}
+
+// This roundabout way of doing it is needed to get both pipeline behaviour and options as arguments.
+func ResolveArgs(args []interface{}) (resources.ResourceTransformer, map[string]interface{}, error) {
+ if len(args) == 0 {
+ return nil, nil, errors.New("no Resource provided in transformation")
+ }
+
+ if len(args) == 1 {
+ r, ok := args[0].(resources.ResourceTransformer)
+ if !ok {
+ return nil, nil, fmt.Errorf("type %T not supported in Resource transformations", args[0])
+ }
+ return r, nil, nil
+ }
+
+ r, ok := args[1].(resources.ResourceTransformer)
+ if !ok {
+ if _, ok := args[1].(map[string]interface{}); !ok {
+ return nil, nil, fmt.Errorf("no Resource provided in transformation")
+ }
+ return nil, nil, fmt.Errorf("type %T not supported in Resource transformations", args[0])
+ }
+
+ m, err := maps.ToStringMapE(args[0])
+ if err != nil {
+ return nil, nil, _errors.Wrap(err, "invalid options type")
+ }
+
+ return r, m, nil
+}
diff --git a/tpl/js/js.go b/tpl/js/js.go
index d8ba35a76..4dc97a707 100644
--- a/tpl/js/js.go
+++ b/tpl/js/js.go
@@ -15,15 +15,12 @@
package js
import (
- "errors"
- "fmt"
-
- "github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/deps"
+ "github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/resources"
"github.com/gohugoio/hugo/resources/resource"
"github.com/gohugoio/hugo/resources/resource_transformers/js"
- _errors "github.com/pkg/errors"
+ "github.com/gohugoio/hugo/tpl/internal/resourcehelpers"
)
// New returns a new instance of the js-namespaced template functions.
@@ -41,50 +38,33 @@ type Namespace struct {
// Build processes the given Resource with ESBuild.
func (ns *Namespace) Build(args ...interface{}) (resource.Resource, error) {
- r, m, err := ns.resolveArgs(args)
- if err != nil {
- return nil, err
- }
- var options js.Options
- if m != nil {
- options, err = js.DecodeOptions(m)
+ var (
+ r resources.ResourceTransformer
+ m map[string]interface{}
+ targetPath string
+ err error
+ ok bool
+ )
+
+ r, targetPath, ok = resourcehelpers.ResolveIfFirstArgIsString(args)
+ if !ok {
+ r, m, err = resourcehelpers.ResolveArgs(args)
if err != nil {
return nil, err
}
}
- return ns.client.Process(r, options)
-
-}
-
-// This roundabout way of doing it is needed to get both pipeline behaviour and options as arguments.
-// This is a copy of tpl/resources/resolveArgs
-func (ns *Namespace) resolveArgs(args []interface{}) (resources.ResourceTransformer, map[string]interface{}, error) {
- if len(args) == 0 {
- return nil, nil, errors.New("no Resource provided in transformation")
- }
-
- if len(args) == 1 {
- r, ok := args[0].(resources.ResourceTransformer)
- if !ok {
- return nil, nil, fmt.Errorf("type %T not supported in Resource transformations", args[0])
- }
- return r, nil, nil
- }
-
- r, ok := args[1].(resources.ResourceTransformer)
- if !ok {
- if _, ok := args[1].(map[string]interface{}); !ok {
- return nil, nil, fmt.Errorf("no Resource provided in transformation")
+ var options js.Options
+ if targetPath != "" {
+ options.TargetPath = helpers.ToSlashTrimLeading(targetPath)
+ } else if m != nil {
+ options, err = js.DecodeOptions(m)
+ if err != nil {
+ return nil, err
}
- return nil, nil, fmt.Errorf("type %T not supported in Resource transformations", args[0])
}
- m, err := maps.ToStringMapE(args[0])
- if err != nil {
- return nil, nil, _errors.Wrap(err, "invalid options type")
- }
+ return ns.client.Process(r, options)
- return r, m, nil
}
diff --git a/tpl/resources/resources.go b/tpl/resources/resources.go
index 6625702ab..cdde6bd5d 100644
--- a/tpl/resources/resources.go
+++ b/tpl/resources/resources.go
@@ -19,13 +19,14 @@ import (
"fmt"
"path/filepath"
+ "github.com/gohugoio/hugo/tpl/internal/resourcehelpers"
+
+ "github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/resources/postpub"
- "github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/resources"
"github.com/gohugoio/hugo/resources/resource"
- _errors "github.com/pkg/errors"
"github.com/gohugoio/hugo/resources/resource_factories/bundler"
"github.com/gohugoio/hugo/resources/resource_factories/create"
@@ -239,10 +240,10 @@ func (ns *Namespace) ToCSS(args ...interface{}) (resource.Resource, error) {
ok bool
)
- r, targetPath, ok = ns.resolveIfFirstArgIsString(args)
+ r, targetPath, ok = resourcehelpers.ResolveIfFirstArgIsString(args)
if !ok {
- r, m, err = ns.resolveArgs(args)
+ r, m, err = resourcehelpers.ResolveArgs(args)
if err != nil {
return nil, err
}
@@ -250,7 +251,7 @@ func (ns *Namespace) ToCSS(args ...interface{}) (resource.Resource, error) {
var options scss.Options
if targetPath != "" {
- options.TargetPath = targetPath
+ options.TargetPath = helpers.ToSlashTrimLeading(targetPath)
} else if m != nil {
options, err = scss.DecodeOptions(m)
if err != nil {
@@ -263,7 +264,7 @@ func (ns *Namespace) ToCSS(args ...interface{}) (resource.Resource, error) {
// PostCSS processes the given Resource with PostCSS
func (ns *Namespace) PostCSS(args ...interface{}) (resource.Resource, error) {
- r, m, err := ns.resolveArgs(args)
+ r, m, err := resourcehelpers.ResolveArgs(args)
if err != nil {
return nil, err
}
@@ -285,7 +286,7 @@ func (ns *Namespace) PostProcess(r resource.Resource) (postpub.PostPublishedReso
// Babel processes the given Resource with Babel.
func (ns *Namespace) Babel(args ...interface{}) (resource.Resource, error) {
- r, m, err := ns.resolveArgs(args)
+ r, m, err := resourcehelpers.ResolveArgs(args)
if err != nil {
return nil, err
}
@@ -301,48 +302,3 @@ func (ns *Namespace) Babel(args ...interface{}) (resource.Resource, error) {
return ns.babelClient.Process(r, options)
}
-
-// We allow string or a map as the first argument in some cases.
-func (ns *Namespace) resolveIfFirstArgIsString(args []interface{}) (resources.ResourceTransformer, string, bool) {
- if len(args) != 2 {
- return nil, "", false
- }
-
- v1, ok1 := args[0].(string)
- if !ok1 {
- return nil, "", false
- }
- v2, ok2 := args[1].(resources.ResourceTransformer)
-
- return v2, v1, ok2
-}
-
-// This roundabout way of doing it is needed to get both pipeline behaviour and options as arguments.
-func (ns *Namespace) resolveArgs(args []interface{}) (resources.ResourceTransformer, map[string]interface{}, error) {
- if len(args) == 0 {
- return nil, nil, errors.New("no Resource provided in transformation")
- }
-
- if len(args) == 1 {
- r, ok := args[0].(resources.ResourceTransformer)
- if !ok {
- return nil, nil, fmt.Errorf("type %T not supported in Resource transformations", args[0])
- }
- return r, nil, nil
- }
-
- r, ok := args[1].(resources.ResourceTransformer)
- if !ok {
- if _, ok := args[1].(map[string]interface{}); !ok {
- return nil, nil, fmt.Errorf("no Resource provided in transformation")
- }
- return nil, nil, fmt.Errorf("type %T not supported in Resource transformations", args[0])
- }
-
- m, err := maps.ToStringMapE(args[0])
- if err != nil {
- return nil, nil, _errors.Wrap(err, "invalid options type")
- }
-
- return r, m, nil
-}