| 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/evanw/esbuild/pkg/api" |
| 22 |
"github.com/gohugoio/hugo/common/hmaps" |
| 23 |
"github.com/gohugoio/hugo/identity" |
| 24 |
"github.com/gohugoio/hugo/internal/js/esbuild" |
| 25 |
"github.com/gohugoio/hugo/media" |
| 26 |
"github.com/gohugoio/hugo/resources" |
| 27 |
"github.com/gohugoio/hugo/resources/internal" |
| 28 |
"github.com/gohugoio/hugo/resources/resource" |
| 29 |
) |
| 30 |
|
| 31 |
type buildTransformation struct { |
| 32 |
optsm map[string]any |
| 33 |
c *Client |
| 34 |
} |
| 35 |
|
| 36 |
func (t *buildTransformation) Key() internal.ResourceTransformationKey { |
| 37 |
key := "jsbuild" |
| 38 |
if t.c.c.CssMode { |
| 39 |
key = "cssbuild" |
| 40 |
} |
| 41 |
return internal.NewResourceTransformationKey(key, t.optsm) |
| 42 |
} |
| 43 |
|
| 44 |
func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx) error { |
| 45 |
ctx.OutMediaType = media.Builtin.JavascriptType |
| 46 |
if ctx.InMediaType == media.Builtin.CSSType { |
| 47 |
ctx.OutMediaType = media.Builtin.CSSType |
| 48 |
} |
| 49 |
|
| 50 |
var opts esbuild.Options |
| 51 |
|
| 52 |
if t.optsm != nil { |
| 53 |
optsExt, err := esbuild.DecodeExternalOptions(t.optsm) |
| 54 |
if err != nil { |
| 55 |
return err |
| 56 |
} |
| 57 |
opts.ExternalOptions = optsExt |
| 58 |
} |
| 59 |
|
| 60 |
if opts.TargetPath != "" { |
| 61 |
ctx.OutPath = opts.TargetPath |
| 62 |
} else { |
| 63 |
ctx.ReplaceOutPathExtension(ctx.OutMediaType.FirstSuffix.FullSuffix) |
| 64 |
} |
| 65 |
|
| 66 |
src, err := io.ReadAll(ctx.From) |
| 67 |
if err != nil { |
| 68 |
return err |
| 69 |
} |
| 70 |
|
| 71 |
opts.SourceDir = filepath.FromSlash(path.Dir(ctx.SourcePath)) |
| 72 |
opts.Contents = string(src) |
| 73 |
opts.MediaType = ctx.InMediaType |
| 74 |
opts.Stdin = true |
| 75 |
opts.IsCSS = t.c.c.CssMode |
| 76 |
var ic resource.ResourceGetter |
| 77 |
if opts.ImportContext != nil { |
| 78 |
ic = resource.NewCachedResourceGetter(opts.ImportContext) |
| 79 |
} |
| 80 |
|
| 81 |
if ic != nil { |
| 82 |
resolved := hmaps.NewCache[string, resource.Resource]() |
| 83 |
opts.ImportOnResolveFunc = func(imp string, args api.OnResolveArgs) string { |
| 84 |
if r := esbuild.ResolveResource(imp, ic); r != nil { |
| 85 |
p := esbuild.PrefixHugoVirtual + resources.InternalResourceTargetPath(r) |
| 86 |
resolved.Set(p, r) |
| 87 |
ctx.DependencyManager.AddIdentity(identity.FirstIdentity(r)) |
| 88 |
return p |
| 89 |
} |
| 90 |
return "" |
| 91 |
} |
| 92 |
opts.ImportOnLoadFunc = func(args api.OnLoadArgs) (string, error) { |
| 93 |
if r, found := resolved.Get(args.Path); found { |
| 94 |
return resources.InternalResourceSourceContent(ctx.Ctx, r) |
| 95 |
} |
| 96 |
return "", nil |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
_, err = t.c.transform(opts, ctx) |
| 101 |
|
| 102 |
return err |
| 103 |
} |