1
- // Copyright 2020 The Hugo Authors. All rights reserved.
1
+ // Copyright 2024 The Hugo Authors. All rights reserved.
2
2
//
3
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
4
// you may not use this file except in compliance with the License.
@@ -16,25 +16,20 @@ package js
16
16
import (
17
17
"errors"
18
18
"fmt"
19
- "io"
20
19
"os"
21
20
"path"
22
21
"path/filepath"
23
22
"regexp"
24
23
"strings"
25
24
26
- "github.com/spf13/afero"
27
-
28
- "github.com/gohugoio/hugo/hugofs"
29
- "github.com/gohugoio/hugo/media"
30
-
25
+ "github.com/evanw/esbuild/pkg/api"
31
26
"github.com/gohugoio/hugo/common/herrors"
32
27
"github.com/gohugoio/hugo/common/text"
33
-
28
+ "github.com/gohugoio/hugo/hugofs"
34
29
"github.com/gohugoio/hugo/hugolib/filesystems"
35
- "github.com/gohugoio/hugo/resources/internal"
30
+ "github.com/gohugoio/hugo/identity"
31
+ "github.com/spf13/afero"
36
32
37
- "github.com/evanw/esbuild/pkg/api"
38
33
"github.com/gohugoio/hugo/resources"
39
34
"github.com/gohugoio/hugo/resources/resource"
40
35
)
@@ -53,46 +48,47 @@ func New(fs *filesystems.SourceFilesystem, rs *resources.Spec) *Client {
53
48
}
54
49
}
55
50
56
- type buildTransformation struct {
57
- optsm map [string ]any
58
- c * Client
51
+ // ProcessExernal processes a resource with the user provided options.
52
+ func (c * Client ) ProcessExernal (res resources.ResourceTransformer , opts map [string ]any ) (resource.Resource , error ) {
53
+ return res .Transform (
54
+ & buildTransformation {c : c , optsm : opts },
55
+ )
59
56
}
60
57
61
- func (t * buildTransformation ) Key () internal.ResourceTransformationKey {
62
- return internal .NewResourceTransformationKey ("jsbuild" , t .optsm )
58
+ // ProcessExernal processes a resource with the given options.
59
+ func (c * Client ) ProcessInternal (res resources.ResourceTransformer , opts Options ) (resource.Resource , error ) {
60
+ return res .Transform (
61
+ & buildTransformation {c : c , opts : opts },
62
+ )
63
63
}
64
64
65
- func (t * buildTransformation ) Transform (ctx * resources.ResourceTransformationCtx ) error {
66
- ctx .OutMediaType = media .Builtin .JavascriptType
65
+ func (c * Client ) BuildBundle (opts Options ) error {
66
+ return c .build (opts , nil )
67
+ }
67
68
68
- opts , err := decodeOptions (t .optsm )
69
- if err != nil {
70
- return err
69
+ // Note that transformCtx may be nil.
70
+ func (c * Client ) build (opts Options , transformCtx * resources.ResourceTransformationCtx ) error {
71
+ dependencyManager := opts .DependencyManager
72
+ if transformCtx != nil {
73
+ dependencyManager = transformCtx .DependencyManager // TODO1
71
74
}
72
-
73
- if opts .TargetPath != "" {
74
- ctx .OutPath = opts .TargetPath
75
- } else {
76
- ctx .ReplaceOutPathExtension (".js" )
75
+ if dependencyManager == nil {
76
+ dependencyManager = identity .NopManager
77
77
}
78
78
79
- src , err := io .ReadAll (ctx .From )
80
- if err != nil {
79
+ opts .ResolveDir = c .rs .Cfg .BaseConfig ().WorkingDir // where node_modules gets resolved
80
+ opts .TsConfig = c .rs .ResolveJSConfigFile ("tsconfig.json" )
81
+
82
+ if err := opts .validate (); err != nil {
81
83
return err
82
84
}
83
85
84
- opts .sourceDir = filepath .FromSlash (path .Dir (ctx .SourcePath ))
85
- opts .resolveDir = t .c .rs .Cfg .BaseConfig ().WorkingDir // where node_modules gets resolved
86
- opts .contents = string (src )
87
- opts .mediaType = ctx .InMediaType
88
- opts .tsConfig = t .c .rs .ResolveJSConfigFile ("tsconfig.json" )
89
-
90
86
buildOptions , err := toBuildOptions (opts )
91
87
if err != nil {
92
88
return err
93
89
}
94
90
95
- buildOptions .Plugins , err = createBuildPlugins (ctx . DependencyManager , t . c , opts )
91
+ buildOptions .Plugins , err = createBuildPlugins (c , dependencyManager , opts )
96
92
if err != nil {
97
93
return err
98
94
}
@@ -113,7 +109,7 @@ func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx
113
109
return fmt .Errorf ("inject: absolute paths not supported, must be relative to /assets" )
114
110
}
115
111
116
- m := resolveComponentInAssets (t . c .rs .Assets .Fs , impPath )
112
+ m := resolveComponentInAssets (c .rs .Assets .Fs , impPath )
117
113
118
114
if m == nil {
119
115
return fmt .Errorf ("inject: file %q not found" , ext )
@@ -138,7 +134,7 @@ func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx
138
134
}
139
135
path := loc .File
140
136
if path == stdinImporter {
141
- path = ctx .SourcePath
137
+ path = transformCtx .SourcePath
142
138
}
143
139
144
140
errorMessage := msg .Text
@@ -154,7 +150,7 @@ func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx
154
150
f , err = hugofs .Os .Open (path )
155
151
} else {
156
152
var fi os.FileInfo
157
- fi , err = t . c .sfs .Fs .Stat (path )
153
+ fi , err = c .sfs .Fs .Stat (path )
158
154
if err == nil {
159
155
m := fi .(hugofs.FileMetaInfo ).Meta ()
160
156
path = m .Filename
@@ -185,38 +181,37 @@ func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx
185
181
// Return 1, log the rest.
186
182
for i , err := range errors {
187
183
if i > 0 {
188
- t . c .rs .Logger .Errorf ("js.Build failed: %s" , err )
184
+ c .rs .Logger .Errorf ("js.Build failed: %s" , err )
189
185
}
190
186
}
191
187
192
188
return errors [0 ]
193
189
}
194
190
195
- if buildOptions .Sourcemap == api .SourceMapExternal {
196
- content := string (result .OutputFiles [1 ].Contents )
197
- symPath := path .Base (ctx .OutPath ) + ".map"
198
- re := regexp .MustCompile (`//# sourceMappingURL=.*\n?` )
199
- content = re .ReplaceAllString (content , "//# sourceMappingURL=" + symPath + "\n " )
191
+ if transformCtx != nil {
192
+ if buildOptions .Sourcemap == api .SourceMapExternal {
193
+ content := string (result .OutputFiles [1 ].Contents )
194
+ symPath := path .Base (transformCtx .OutPath ) + ".map"
195
+ re := regexp .MustCompile (`//# sourceMappingURL=.*\n?` )
196
+ content = re .ReplaceAllString (content , "//# sourceMappingURL=" + symPath + "\n " )
200
197
201
- if err = ctx .PublishSourceMap (string (result .OutputFiles [0 ].Contents )); err != nil {
202
- return err
203
- }
204
- _ , err := ctx .To .Write ([]byte (content ))
205
- if err != nil {
206
- return err
207
- }
208
- } else {
209
- _ , err := ctx .To .Write (result .OutputFiles [0 ].Contents )
210
- if err != nil {
211
- return err
198
+ if err = transformCtx .PublishSourceMap (string (result .OutputFiles [0 ].Contents )); err != nil {
199
+ return err
200
+ }
201
+ _ , err := transformCtx .To .Write ([]byte (content ))
202
+ if err != nil {
203
+ return err
204
+ }
205
+ } else {
206
+ _ , err := transformCtx .To .Write (result .OutputFiles [0 ].Contents )
207
+ if err != nil {
208
+ return err
209
+ }
212
210
}
211
+
212
+ return nil
213
213
}
214
- return nil
215
- }
216
214
217
- // Process process esbuild transform
218
- func (c * Client ) Process (res resources.ResourceTransformer , opts map [string ]any ) (resource.Resource , error ) {
219
- return res .Transform (
220
- & buildTransformation {c : c , optsm : opts },
221
- )
215
+ // TODO1
216
+ return nil
222
217
}
0 commit comments