Skip to content

Commit 36afa8e

Browse files
committed
js/esbuild: Add drop option
Fixes #13362
1 parent 304a7e5 commit 36afa8e

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

docs/content/en/functions/js/_common/options.md

+8
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ import * as ReactDOM from 'react-dom/client';
8181
{{ $defines := dict "process.env.NODE_ENV" `"development"` }}
8282
```
8383

84+
##### drop
85+
86+
Edit your source code before building to drop certain constructs: One of `debugger` or `console`.
87+
88+
{{< new-in 0.144.0 />}}
89+
90+
See https://esbuild.github.io/api/#drop
91+
8492
###### sourceMap
8593

8694
(`string`) Whether to generate `inline`, `linked` or `external` source maps from esbuild. Linked and external source maps will be written to the target with the output file name + ".map". When `linked` a `sourceMappingURL` will also be written to the output file. By default, source maps are not created. Note that the `linked` option was added in Hugo 0.140.0.

internal/js/esbuild/options.go

+16
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ type ExternalOptions struct {
138138
// User defined symbols.
139139
Defines map[string]any
140140

141+
// This tells esbuild to edit your source code before building to drop certain constructs.
142+
// See https://esbuild.github.io/api/#drop
143+
Drop string
144+
141145
// Maps a component import to another.
142146
Shims map[string]string
143147

@@ -298,6 +302,17 @@ func (opts *Options) compile() (err error) {
298302
defines = maps.ToStringMapString(opts.Defines)
299303
}
300304

305+
var drop api.Drop
306+
switch opts.Drop {
307+
case "":
308+
case "console":
309+
drop = api.DropConsole
310+
case "debugger":
311+
drop = api.DropDebugger
312+
default:
313+
err = fmt.Errorf("unsupported drop type: %q", opts.Drop)
314+
}
315+
301316
// By default we only need to specify outDir and no outFile
302317
outDir := opts.OutDir
303318
outFile := ""
@@ -344,6 +359,7 @@ func (opts *Options) compile() (err error) {
344359

345360
Define: defines,
346361
External: opts.Externals,
362+
Drop: drop,
347363

348364
JSXFactory: opts.JSXFactory,
349365
JSXFragment: opts.JSXFragment,

internal/js/esbuild/options_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,28 @@ func TestToBuildOptions(t *testing.T) {
177177
JSX: api.JSXAutomatic,
178178
JSXImportSource: "preact",
179179
})
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"`)
180202
}
181203

182204
func TestToBuildOptionsTarget(t *testing.T) {

0 commit comments

Comments
 (0)