| 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 esbuild provides functions for building JavaScript resources. |
| 15 |
package esbuild |
| 16 |
|
| 17 |
import ( |
| 18 |
"errors" |
| 19 |
"fmt" |
| 20 |
"os" |
| 21 |
"path/filepath" |
| 22 |
"strings" |
| 23 |
|
| 24 |
"github.com/evanw/esbuild/pkg/api" |
| 25 |
"github.com/gohugoio/hugo/common/herrors" |
| 26 |
"github.com/gohugoio/hugo/common/hugio" |
| 27 |
"github.com/gohugoio/hugo/common/text" |
| 28 |
"github.com/gohugoio/hugo/hugofs" |
| 29 |
"github.com/gohugoio/hugo/hugolib/filesystems" |
| 30 |
"github.com/gohugoio/hugo/identity" |
| 31 |
"github.com/gohugoio/hugo/resources" |
| 32 |
) |
| 33 |
|
| 34 |
// NewBuildClient creates a new BuildClient. |
| 35 |
func NewBuildClient(fs *filesystems.SourceFilesystem, rs *resources.Spec, cssMode bool) *BuildClient { |
| 36 |
return &BuildClient{ |
| 37 |
rs: rs, |
| 38 |
sfs: fs, |
| 39 |
CssMode: cssMode, |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// BuildClient is a client for building JavaScript resources using esbuild. |
| 44 |
type BuildClient struct { |
| 45 |
rs *resources.Spec |
| 46 |
sfs *filesystems.SourceFilesystem |
| 47 |
CssMode bool |
| 48 |
} |
| 49 |
|
| 50 |
// Spec returns the resources.Spec for this client. |
| 51 |
func (c *BuildClient) Spec() *resources.Spec { |
| 52 |
return c.rs |
| 53 |
} |
| 54 |
|
| 55 |
// Build builds the given JavaScript resources using esbuild with the given options. |
| 56 |
func (c *BuildClient) Build(opts Options) (api.BuildResult, error) { |
| 57 |
dependencyManager := opts.DependencyManager |
| 58 |
if dependencyManager == nil { |
| 59 |
dependencyManager = identity.NopManager |
| 60 |
} |
| 61 |
|
| 62 |
opts.OutDir = c.rs.AbsPublishDir |
| 63 |
opts.ResolveDir = c.rs.Cfg.BaseConfig().WorkingDir // where node_modules gets resolved |
| 64 |
opts.AbsWorkingDir = opts.ResolveDir |
| 65 |
opts.TsConfig = c.rs.ResolveJSConfigFile("tsconfig.json") |
| 66 |
assetsResolver := newFSResolver(c.rs.Assets.Fs) |
| 67 |
|
| 68 |
if err := opts.validate(); err != nil { |
| 69 |
return api.BuildResult{}, err |
| 70 |
} |
| 71 |
|
| 72 |
if err := opts.compile(); err != nil { |
| 73 |
return api.BuildResult{}, err |
| 74 |
} |
| 75 |
|
| 76 |
var err error |
| 77 |
opts.compiled.Plugins, err = createBuildPlugins(c.rs, assetsResolver, dependencyManager, opts) |
| 78 |
if err != nil { |
| 79 |
return api.BuildResult{}, err |
| 80 |
} |
| 81 |
|
| 82 |
if opts.Inject != nil { |
| 83 |
// Resolve the absolute filenames. |
| 84 |
for i, ext := range opts.Inject { |
| 85 |
impPath := filepath.FromSlash(ext) |
| 86 |
if filepath.IsAbs(impPath) { |
| 87 |
return api.BuildResult{}, fmt.Errorf("inject: absolute paths not supported, must be relative to /assets") |
| 88 |
} |
| 89 |
|
| 90 |
m := assetsResolver.resolveComponent(impPath, false) |
| 91 |
|
| 92 |
if m == nil { |
| 93 |
return api.BuildResult{}, fmt.Errorf("inject: file %q not found", ext) |
| 94 |
} |
| 95 |
|
| 96 |
opts.Inject[i] = m.Filename |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
opts.compiled.Inject = opts.Inject |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
result := api.Build(opts.compiled) |
| 105 |
|
| 106 |
if len(result.Errors) > 0 { |
| 107 |
createErr := func(msg api.Message) error { |
| 108 |
if msg.Location == nil { |
| 109 |
return errors.New(msg.Text) |
| 110 |
} |
| 111 |
var ( |
| 112 |
contentr hugio.ReadSeekCloser |
| 113 |
errorMessage string |
| 114 |
loc = msg.Location |
| 115 |
errorPath = loc.File |
| 116 |
err error |
| 117 |
) |
| 118 |
|
| 119 |
var resolvedError *ErrorMessageResolved |
| 120 |
|
| 121 |
if opts.ErrorMessageResolveFunc != nil { |
| 122 |
resolvedError = opts.ErrorMessageResolveFunc(msg) |
| 123 |
} |
| 124 |
|
| 125 |
if resolvedError == nil { |
| 126 |
if errorPath == stdinImporter { |
| 127 |
errorPath = opts.StdinSourcePath |
| 128 |
} |
| 129 |
|
| 130 |
errorMessage = msg.Text |
| 131 |
|
| 132 |
var namespace string |
| 133 |
for _, ns := range hugoNamespaces { |
| 134 |
if strings.HasPrefix(errorPath, ns) { |
| 135 |
namespace = ns |
| 136 |
break |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
if namespace != "" { |
| 141 |
namespace += ":" |
| 142 |
errorMessage = strings.ReplaceAll(errorMessage, namespace, "") |
| 143 |
errorPath = strings.TrimPrefix(errorPath, namespace) |
| 144 |
contentr, err = hugofs.Os.Open(errorPath) |
| 145 |
} else { |
| 146 |
var fi os.FileInfo |
| 147 |
fi, err = c.sfs.Fs.Stat(errorPath) |
| 148 |
if err == nil { |
| 149 |
m := fi.(hugofs.FileMetaInfo).Meta() |
| 150 |
errorPath = m.Filename |
| 151 |
contentr, err = m.Open() |
| 152 |
} |
| 153 |
} |
| 154 |
} else { |
| 155 |
contentr = resolvedError.Content |
| 156 |
errorPath = resolvedError.Path |
| 157 |
errorMessage = resolvedError.Message |
| 158 |
} |
| 159 |
|
| 160 |
if contentr != nil { |
| 161 |
defer contentr.Close() |
| 162 |
} |
| 163 |
|
| 164 |
if err == nil { |
| 165 |
fe := herrors. |
| 166 |
NewFileErrorFromName(errors.New(errorMessage), errorPath). |
| 167 |
UpdatePosition(text.Position{Offset: -1, LineNumber: loc.Line, ColumnNumber: loc.Column}). |
| 168 |
UpdateContent(contentr, nil) |
| 169 |
|
| 170 |
return fe |
| 171 |
} |
| 172 |
|
| 173 |
return fmt.Errorf("%s", errorMessage) |
| 174 |
} |
| 175 |
|
| 176 |
var errors []error |
| 177 |
|
| 178 |
for _, msg := range result.Errors { |
| 179 |
errors = append(errors, createErr(msg)) |
| 180 |
} |
| 181 |
|
| 182 |
// Return 1, log the rest. |
| 183 |
for i, err := range errors { |
| 184 |
if i > 0 { |
| 185 |
c.rs.Logger.Errorf("js.Build failed: %s", err) |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
return result, errors[0] |
| 190 |
} |
| 191 |
|
| 192 |
inOutputPathToAbsFilename := opts.ResolveSourceMapSource |
| 193 |
opts.ResolveSourceMapSource = func(s string) string { |
| 194 |
if inOutputPathToAbsFilename != nil { |
| 195 |
if filename := inOutputPathToAbsFilename(s); filename != "" { |
| 196 |
return filename |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
if m := assetsResolver.resolveComponent(s, false); m != nil { |
| 201 |
return m.Filename |
| 202 |
} |
| 203 |
|
| 204 |
return "" |
| 205 |
} |
| 206 |
|
| 207 |
for i, o := range result.OutputFiles { |
| 208 |
if err := fixOutputFile(&o, func(s string) string { |
| 209 |
if s == "<stdin>" { |
| 210 |
return opts.ResolveSourceMapSource(opts.StdinSourcePath) |
| 211 |
} |
| 212 |
var isNsHugo bool |
| 213 |
if strings.HasPrefix(s, "ns-hugo") { |
| 214 |
isNsHugo = true |
| 215 |
idxColon := strings.Index(s, ":") |
| 216 |
s = s[idxColon+1:] |
| 217 |
} |
| 218 |
|
| 219 |
if !strings.HasPrefix(s, PrefixHugoVirtual) { |
| 220 |
// A leading slash marks a synthetic import key rooted in Hugo's |
| 221 |
// virtual namespace (see paths.AddLeadingSlash in batch.go); it is |
| 222 |
// not an OutDir-relative path. filepath.IsAbs only reports true for |
| 223 |
// such paths on Unix, so check it explicitly to behave the same on |
| 224 |
// Windows. |
| 225 |
if !filepath.IsAbs(s) && !strings.HasPrefix(s, "/") { |
| 226 |
s = filepath.Join(opts.OutDir, s) |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
if isNsHugo { |
| 231 |
if ss := opts.ResolveSourceMapSource(s); ss != "" { |
| 232 |
if strings.HasPrefix(ss, PrefixHugoMemory) { |
| 233 |
// File not on disk, mark it for removal from the sources slice. |
| 234 |
return "" |
| 235 |
} |
| 236 |
return ss |
| 237 |
} |
| 238 |
if strings.HasPrefix(s, opts.OutDir) { |
| 239 |
// This is an output file, not a source file. |
| 240 |
return "" |
| 241 |
} |
| 242 |
// s is already the absolute filename set by the Hugo resolve plugin. |
| 243 |
return s |
| 244 |
} |
| 245 |
return s |
| 246 |
}); err != nil { |
| 247 |
return result, err |
| 248 |
} |
| 249 |
result.OutputFiles[i] = o |
| 250 |
} |
| 251 |
|
| 252 |
return result, nil |
| 253 |
} |