summaryrefslogtreecommitdiffhomepage
path: root/tpl
diff options
context:
space:
mode:
authorNiek de Wit <[email protected]>2019-03-22 17:07:37 +0100
committerBjørn Erik Pedersen <[email protected]>2020-04-29 10:51:33 +0200
commit2a171ff1c5d9b1603fe78c67d2d894bb2efccc8b (patch)
treed378f834de9efdabe7b214c423eea4ccd877a556 /tpl
parent67f920419a53c7ff11e01c4286dca23e92110a12 (diff)
downloadhugo-2a171ff1c5d9b1603fe78c67d2d894bb2efccc8b.tar.gz
hugo-2a171ff1c5d9b1603fe78c67d2d894bb2efccc8b.zip
resources: Add JavaScript transpiling solution
Add a new pipe called TranspileJS which uses the Babel cli. This makes it possible for users to write ES6 JavaScript code and transpile it to ES5 during website generation so that the code still works with older browser versions. Fixes #5764
Diffstat (limited to 'tpl')
-rw-r--r--tpl/resources/init.go5
-rw-r--r--tpl/resources/resources.go53
2 files changed, 43 insertions, 15 deletions
diff --git a/tpl/resources/init.go b/tpl/resources/init.go
index 3e750f325..10e8e5319 100644
--- a/tpl/resources/init.go
+++ b/tpl/resources/init.go
@@ -60,6 +60,11 @@ func init() {
[][2]string{},
)
+ ns.AddMethodMapping(ctx.TranspileJS,
+ []string{"transpileJS"},
+ [][2]string{},
+ )
+
return ns
}
diff --git a/tpl/resources/resources.go b/tpl/resources/resources.go
index 90fb58b4b..c256fa903 100644
--- a/tpl/resources/resources.go
+++ b/tpl/resources/resources.go
@@ -34,6 +34,7 @@ import (
"github.com/gohugoio/hugo/resources/resource_transformers/postcss"
"github.com/gohugoio/hugo/resources/resource_transformers/templates"
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/scss"
+ "github.com/gohugoio/hugo/resources/resource_transformers/transpilejs"
"github.com/spf13/cast"
)
@@ -54,14 +55,15 @@ func New(deps *deps.Deps) (*Namespace, error) {
}
return &Namespace{
- deps: deps,
- scssClient: scssClient,
- createClient: create.New(deps.ResourceSpec),
- bundlerClient: bundler.New(deps.ResourceSpec),
- integrityClient: integrity.New(deps.ResourceSpec),
- minifyClient: minifyClient,
- postcssClient: postcss.New(deps.ResourceSpec),
- templatesClient: templates.New(deps.ResourceSpec, deps),
+ deps: deps,
+ scssClient: scssClient,
+ createClient: create.New(deps.ResourceSpec),
+ bundlerClient: bundler.New(deps.ResourceSpec),
+ integrityClient: integrity.New(deps.ResourceSpec),
+ minifyClient: minifyClient,
+ postcssClient: postcss.New(deps.ResourceSpec),
+ templatesClient: templates.New(deps.ResourceSpec, deps),
+ transpileJSClient: transpilejs.New(deps.ResourceSpec),
}, nil
}
@@ -69,13 +71,14 @@ func New(deps *deps.Deps) (*Namespace, error) {
type Namespace struct {
deps *deps.Deps
- createClient *create.Client
- bundlerClient *bundler.Client
- scssClient *scss.Client
- integrityClient *integrity.Client
- minifyClient *minifier.Client
- postcssClient *postcss.Client
- templatesClient *templates.Client
+ createClient *create.Client
+ bundlerClient *bundler.Client
+ scssClient *scss.Client
+ integrityClient *integrity.Client
+ minifyClient *minifier.Client
+ postcssClient *postcss.Client
+ transpileJSClient *transpilejs.Client
+ templatesClient *templates.Client
}
// Get locates the filename given in Hugo's assets filesystem
@@ -277,6 +280,26 @@ func (ns *Namespace) PostCSS(args ...interface{}) (resource.Resource, error) {
func (ns *Namespace) PostProcess(r resource.Resource) (postpub.PostPublishedResource, error) {
return ns.deps.ResourceSpec.PostProcess(r)
+
+}
+
+// TranspileJS processes the given Resource with Babel.
+func (ns *Namespace) TranspileJS(args ...interface{}) (resource.Resource, error) {
+ r, m, err := ns.resolveArgs(args)
+ if err != nil {
+ return nil, err
+ }
+ var options transpilejs.Options
+ if m != nil {
+ options, err = transpilejs.DecodeOptions(m)
+
+ if err != nil {
+ return nil, err
+ }
+ }
+
+ return ns.transpileJSClient.Process(r, options)
+
}
// We allow string or a map as the first argument in some cases.