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