Debian Codegraph @

tpl/js/js.go

raw
1 // Copyright 2024 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 // Package js provides functions for building JavaScript resources
15 package js
16
17 import (
18 "errors"
19
20 "github.com/gohugoio/hugo/deps"
21 "github.com/gohugoio/hugo/internal/js"
22 "github.com/gohugoio/hugo/internal/js/esbuild"
23 "github.com/gohugoio/hugo/resources"
24 "github.com/gohugoio/hugo/resources/resource"
25 "github.com/gohugoio/hugo/resources/resource_factories/create"
26 "github.com/gohugoio/hugo/resources/resource_transformers/babel"
27 jstransform "github.com/gohugoio/hugo/resources/resource_transformers/js"
28 "github.com/gohugoio/hugo/tpl/internal/resourcehelpers"
29 )
30
31 // New returns a new instance of the js-namespaced template functions.
32 func New(d *deps.Deps) (*Namespace, error) {
33 if d.ResourceSpec == nil {
34 return &Namespace{}, nil
35 }
36
37 return &Namespace{
38 d: d,
39 jsTransformClient: jstransform.New(d.BaseFs.Assets, d.ResourceSpec, false),
40 createClient: create.New(d.ResourceSpec),
41 babelClient: babel.New(d.ResourceSpec),
42 }, nil
43 }
44
45 // Namespace provides template functions for the "js" namespace.
46 type Namespace struct {
47 d *deps.Deps
48
49 jsTransformClient *jstransform.Client
50 createClient *create.Client
51 babelClient *babel.Client
52 }
53
54 // Build processes the given JavaScript Resource with ESBuild.
55 func (ns *Namespace) Build(args ...any) (resource.Resource, error) {
56 var (
57 r resources.ResourceTransformer
58 m map[string]any
59 targetPath string
60 err error
61 ok bool
62 )
63
64 r, targetPath, ok = resourcehelpers.ResolveIfFirstArgIsString(args)
65
66 if !ok {
67 r, m, err = resourcehelpers.ResolveArgs(args)
68 if err != nil {
69 return nil, err
70 }
71 }
72
73 if targetPath != "" {
74 m = map[string]any{"targetPath": targetPath}
75 }
76
77 return ns.jsTransformClient.Process(r, m)
78 }
79
80 // Batch creates a new Batcher with the given ID.
81 // Repeated calls with the same ID will return the same Batcher.
82 // The ID will be used to name the root directory of the batch.
83 // Forward slashes in the ID is allowed.
84 func (ns *Namespace) Batch(id string) (js.Batcher, error) {
85 if err := esbuild.ValidateBatchID(id, true); err != nil {
86 return nil, err
87 }
88
89 b, err := ns.d.JSBatcherClient.Store().GetOrCreate(id, func() (js.Batcher, error) {
90 return ns.d.JSBatcherClient.New(id)
91 })
92 if err != nil {
93 return nil, err
94 }
95 return b, nil
96 }
97
98 // Babel processes the given Resource with Babel.
99 func (ns *Namespace) Babel(args ...any) (resource.Resource, error) {
100 if len(args) > 2 {
101 return nil, errors.New("must not provide more arguments than resource object and options")
102 }
103
104 r, m, err := resourcehelpers.ResolveArgs(args)
105 if err != nil {
106 return nil, err
107 }
108 var options babel.Options
109 if m != nil {
110 options, err = babel.DecodeOptions(m)
111 if err != nil {
112 return nil, err
113 }
114 }
115
116 return ns.babelClient.Process(r, options)
117 }