Skip to content

Commit ec1933f

Browse files
committedDec 12, 2024·
js/esbuild: Add platform option
Closes #13136
1 parent 75ad9cd commit ec1933f

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed
 

‎internal/js/esbuild/options.go

+19
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ type ExternalOptions struct {
121121
// Default is to esm.
122122
Format string
123123

124+
// One of browser, node, neutral.
125+
// Default is browser.
126+
// See https://esbuild.github.io/api/#platform
127+
Platform string
128+
124129
// External dependencies, e.g. "react".
125130
Externals []string
126131

@@ -274,6 +279,19 @@ func (opts *Options) compile() (err error) {
274279
return
275280
}
276281

282+
var platform api.Platform
283+
switch opts.Platform {
284+
case "", "browser":
285+
platform = api.PlatformBrowser
286+
case "node":
287+
platform = api.PlatformNode
288+
case "neutral":
289+
platform = api.PlatformNeutral
290+
default:
291+
err = fmt.Errorf("unsupported platform type: %q", opts.Platform)
292+
return
293+
}
294+
277295
var defines map[string]string
278296
if opts.Defines != nil {
279297
defines = maps.ToStringMapString(opts.Defines)
@@ -310,6 +328,7 @@ func (opts *Options) compile() (err error) {
310328

311329
Target: target,
312330
Format: format,
331+
Platform: platform,
313332
Sourcemap: sourceMap,
314333
SourcesContent: sourcesContent,
315334

‎internal/js/esbuild/options_test.go

+24-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func TestToBuildOptions(t *testing.T) {
3838
Bundle: true,
3939
Target: api.ESNext,
4040
Format: api.FormatIIFE,
41+
Platform: api.PlatformBrowser,
4142
SourcesContent: 1,
4243
Stdin: &api.StdinOptions{
4344
Loader: api.LoaderJS,
@@ -62,6 +63,7 @@ func TestToBuildOptions(t *testing.T) {
6263
Bundle: true,
6364
Target: api.ES2018,
6465
Format: api.FormatCommonJS,
66+
Platform: api.PlatformBrowser,
6567
SourcesContent: 1,
6668
MinifyIdentifiers: true,
6769
MinifySyntax: true,
@@ -87,6 +89,7 @@ func TestToBuildOptions(t *testing.T) {
8789
Bundle: true,
8890
Target: api.ES2018,
8991
Format: api.FormatCommonJS,
92+
Platform: api.PlatformBrowser,
9093
MinifyIdentifiers: true,
9194
MinifySyntax: true,
9295
MinifyWhitespace: true,
@@ -113,6 +116,7 @@ func TestToBuildOptions(t *testing.T) {
113116
Bundle: true,
114117
Target: api.ES2018,
115118
Format: api.FormatCommonJS,
119+
Platform: api.PlatformBrowser,
116120
MinifyIdentifiers: true,
117121
MinifySyntax: true,
118122
MinifyWhitespace: true,
@@ -139,6 +143,7 @@ func TestToBuildOptions(t *testing.T) {
139143
Bundle: true,
140144
Target: api.ES2018,
141145
Format: api.FormatCommonJS,
146+
Platform: api.PlatformBrowser,
142147
MinifyIdentifiers: true,
143148
MinifySyntax: true,
144149
MinifyWhitespace: true,
@@ -164,6 +169,7 @@ func TestToBuildOptions(t *testing.T) {
164169
Bundle: true,
165170
Target: api.ESNext,
166171
Format: api.FormatIIFE,
172+
Platform: api.PlatformBrowser,
167173
SourcesContent: 1,
168174
Stdin: &api.StdinOptions{
169175
Loader: api.LoaderJS,
@@ -210,10 +216,25 @@ func TestToBuildOptionsTarget(t *testing.T) {
210216

211217
func TestDecodeExternalOptions(t *testing.T) {
212218
c := qt.New(t)
213-
m := map[string]any{}
214-
opts, err := DecodeExternalOptions(m)
219+
m := map[string]any{
220+
"platform": "node",
221+
}
222+
ext, err := DecodeExternalOptions(m)
215223
c.Assert(err, qt.IsNil)
216-
c.Assert(opts, qt.DeepEquals, ExternalOptions{
224+
c.Assert(ext, qt.DeepEquals, ExternalOptions{
217225
SourcesContent: true,
226+
Platform: "node",
227+
})
228+
229+
opts := Options{
230+
ExternalOptions: ext,
231+
}
232+
c.Assert(opts.compile(), qt.IsNil)
233+
c.Assert(opts.compiled, qt.DeepEquals, api.BuildOptions{
234+
Bundle: true,
235+
Target: api.ESNext,
236+
Format: api.FormatIIFE,
237+
Platform: api.PlatformNode,
238+
SourcesContent: api.SourcesContentInclude,
218239
})
219240
}

0 commit comments

Comments
 (0)
Please sign in to comment.