Debian Codegraph @

internal/js/esbuild/options_test.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 esbuild
15
16 import (
17 "testing"
18
19 "github.com/gohugoio/hugo/media"
20
21 "github.com/evanw/esbuild/pkg/api"
22
23 qt "github.com/frankban/quicktest"
24 )
25
26 func TestToBuildOptions(t *testing.T) {
27 c := qt.New(t)
28
29 opts := Options{
30 InternalOptions: InternalOptions{
31 MediaType: media.Builtin.JavascriptType,
32 Stdin: true,
33 },
34 }
35
36 c.Assert(opts.compile(), qt.IsNil)
37 c.Assert(opts.compiled, qt.DeepEquals, api.BuildOptions{
38 Bundle: true,
39 Target: api.ESNext,
40 Format: api.FormatIIFE,
41 Platform: api.PlatformBrowser,
42 SourcesContent: 1,
43 Stdin: &api.StdinOptions{
44 Loader: api.LoaderJS,
45 },
46 })
47
48 opts = Options{
49 ExternalOptions: ExternalOptions{
50 Target: []string{"es2018"},
51 Format: "cjs",
52 Minify: true,
53 AvoidTDZ: true,
54 },
55 InternalOptions: InternalOptions{
56 MediaType: media.Builtin.JavascriptType,
57 Stdin: true,
58 },
59 }
60
61 c.Assert(opts.compile(), qt.IsNil)
62 c.Assert(opts.compiled, qt.DeepEquals, api.BuildOptions{
63 Bundle: true,
64 Target: api.ES2018,
65 Format: api.FormatCommonJS,
66 Platform: api.PlatformBrowser,
67 SourcesContent: 1,
68 MinifyIdentifiers: true,
69 MinifySyntax: true,
70 MinifyWhitespace: true,
71 Stdin: &api.StdinOptions{
72 Loader: api.LoaderJS,
73 },
74 })
75
76 opts = Options{
77 ExternalOptions: ExternalOptions{
78 Target: []string{"es2018"}, Format: "cjs", Minify: true,
79 SourceMap: "inline",
80 },
81 InternalOptions: InternalOptions{
82 MediaType: media.Builtin.JavascriptType,
83 Stdin: true,
84 },
85 }
86
87 c.Assert(opts.compile(), qt.IsNil)
88 c.Assert(opts.compiled, qt.DeepEquals, api.BuildOptions{
89 Bundle: true,
90 Target: api.ES2018,
91 Format: api.FormatCommonJS,
92 Platform: api.PlatformBrowser,
93 MinifyIdentifiers: true,
94 MinifySyntax: true,
95 MinifyWhitespace: true,
96 SourcesContent: 1,
97 Sourcemap: api.SourceMapInline,
98 Stdin: &api.StdinOptions{
99 Loader: api.LoaderJS,
100 },
101 })
102
103 opts = Options{
104 ExternalOptions: ExternalOptions{
105 Target: []string{"es2018"}, Format: "cjs", Minify: true,
106 SourceMap: "inline",
107 },
108 InternalOptions: InternalOptions{
109 MediaType: media.Builtin.JavascriptType,
110 Stdin: true,
111 },
112 }
113
114 c.Assert(opts.compile(), qt.IsNil)
115 c.Assert(opts.compiled, qt.DeepEquals, api.BuildOptions{
116 Bundle: true,
117 Target: api.ES2018,
118 Format: api.FormatCommonJS,
119 Platform: api.PlatformBrowser,
120 MinifyIdentifiers: true,
121 MinifySyntax: true,
122 MinifyWhitespace: true,
123 Sourcemap: api.SourceMapInline,
124 SourcesContent: 1,
125 Stdin: &api.StdinOptions{
126 Loader: api.LoaderJS,
127 },
128 })
129
130 opts = Options{
131 ExternalOptions: ExternalOptions{
132 Target: []string{"es2018"}, Format: "cjs", Minify: true,
133 SourceMap: "external",
134 },
135 InternalOptions: InternalOptions{
136 MediaType: media.Builtin.JavascriptType,
137 Stdin: true,
138 },
139 }
140
141 c.Assert(opts.compile(), qt.IsNil)
142 c.Assert(opts.compiled, qt.DeepEquals, api.BuildOptions{
143 Bundle: true,
144 Target: api.ES2018,
145 Format: api.FormatCommonJS,
146 Platform: api.PlatformBrowser,
147 MinifyIdentifiers: true,
148 MinifySyntax: true,
149 MinifyWhitespace: true,
150 Sourcemap: api.SourceMapExternal,
151 SourcesContent: 1,
152 Stdin: &api.StdinOptions{
153 Loader: api.LoaderJS,
154 },
155 })
156
157 opts = Options{
158 ExternalOptions: ExternalOptions{
159 JSX: "automatic", JSXImportSource: "preact",
160 },
161 InternalOptions: InternalOptions{
162 MediaType: media.Builtin.JavascriptType,
163 Stdin: true,
164 },
165 }
166
167 c.Assert(opts.compile(), qt.IsNil)
168 c.Assert(opts.compiled, qt.DeepEquals, api.BuildOptions{
169 Bundle: true,
170 Target: api.ESNext,
171 Format: api.FormatIIFE,
172 Platform: api.PlatformBrowser,
173 SourcesContent: 1,
174 Stdin: &api.StdinOptions{
175 Loader: api.LoaderJS,
176 },
177 JSX: api.JSXAutomatic,
178 JSXImportSource: "preact",
179 })
180
181 opts = Options{
182 ExternalOptions: ExternalOptions{
183 Drop: "console",
184 },
185 }
186 c.Assert(opts.compile(), qt.IsNil)
187 c.Assert(opts.compiled.Drop, qt.Equals, api.DropConsole)
188 opts = Options{
189 ExternalOptions: ExternalOptions{
190 Drop: "debugger",
191 },
192 }
193 c.Assert(opts.compile(), qt.IsNil)
194 c.Assert(opts.compiled.Drop, qt.Equals, api.DropDebugger)
195
196 opts = Options{
197 ExternalOptions: ExternalOptions{
198 Drop: "adsfadsf",
199 },
200 }
201 c.Assert(opts.compile(), qt.ErrorMatches, `unsupported drop type: "adsfadsf"`)
202 }
203
204 func TestToBuildOptionsTarget(t *testing.T) {
205 c := qt.New(t)
206
207 for _, test := range []struct {
208 target string
209 expect api.Target
210 }{
211 {"es2015", api.ES2015},
212 {"es2016", api.ES2016},
213 {"es2017", api.ES2017},
214 {"es2018", api.ES2018},
215 {"es2019", api.ES2019},
216 {"es2020", api.ES2020},
217 {"es2021", api.ES2021},
218 {"es2022", api.ES2022},
219 {"es2023", api.ES2023},
220 {"", api.ESNext},
221 {"esnext", api.ESNext},
222 } {
223 c.Run(test.target, func(c *qt.C) {
224 opts := Options{
225 ExternalOptions: ExternalOptions{
226 Target: []string{test.target},
227 },
228 InternalOptions: InternalOptions{
229 MediaType: media.Builtin.JavascriptType,
230 },
231 }
232
233 c.Assert(opts.compile(), qt.IsNil)
234 c.Assert(opts.compiled.Target, qt.Equals, test.expect)
235 })
236 }
237 }
238
239 func TestDecodeExternalOptions(t *testing.T) {
240 c := qt.New(t)
241 m := map[string]any{
242 "platform": "node",
243 }
244 ext, err := DecodeExternalOptions(m)
245 c.Assert(err, qt.IsNil)
246 c.Assert(ext, qt.DeepEquals, ExternalOptions{
247 SourcesContent: true,
248 Platform: "node",
249 })
250
251 opts := Options{
252 ExternalOptions: ext,
253 }
254 c.Assert(opts.compile(), qt.IsNil)
255 c.Assert(opts.compiled, qt.DeepEquals, api.BuildOptions{
256 Bundle: true,
257 Target: api.ESNext,
258 Format: api.FormatIIFE,
259 Platform: api.PlatformNode,
260 SourcesContent: api.SourcesContentInclude,
261 })
262 }
263
264 func TestDecodeExternalOptionsLegacyTargetString(t *testing.T) {
265 c := qt.New(t)
266 m := map[string]any{
267 "target": "es2018",
268 }
269 ext, err := DecodeExternalOptions(m)
270 c.Assert(err, qt.IsNil)
271 c.Assert(ext.Target, qt.DeepEquals, []string{"es2018"})
272 }