transform.go - hugo - [fork] hugo port for 9front
(HTM) git clone https://git.drkhsh.at/hugo.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) Submodules
(DIR) README
(DIR) LICENSE
---
transform.go (1741B)
---
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
15
16 import (
17 "io"
18 "path"
19 "path/filepath"
20
21 "github.com/gohugoio/hugo/internal/js/esbuild"
22 "github.com/gohugoio/hugo/media"
23 "github.com/gohugoio/hugo/resources"
24 "github.com/gohugoio/hugo/resources/internal"
25 )
26
27 type buildTransformation struct {
28 optsm map[string]any
29 c *Client
30 }
31
32 func (t *buildTransformation) Key() internal.ResourceTransformationKey {
33 return internal.NewResourceTransformationKey("jsbuild", t.optsm)
34 }
35
36 func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx) error {
37 ctx.OutMediaType = media.Builtin.JavascriptType
38
39 var opts esbuild.Options
40
41 if t.optsm != nil {
42 optsExt, err := esbuild.DecodeExternalOptions(t.optsm)
43 if err != nil {
44 return err
45 }
46 opts.ExternalOptions = optsExt
47 }
48
49 if opts.TargetPath != "" {
50 ctx.OutPath = opts.TargetPath
51 } else {
52 ctx.ReplaceOutPathExtension(".js")
53 }
54
55 src, err := io.ReadAll(ctx.From)
56 if err != nil {
57 return err
58 }
59
60 opts.SourceDir = filepath.FromSlash(path.Dir(ctx.SourcePath))
61 opts.Contents = string(src)
62 opts.MediaType = ctx.InMediaType
63 opts.Stdin = true
64
65 _, err = t.c.transform(opts, ctx)
66
67 return err
68 }