js/esbuild: Add drop option

Fixes #13362
This commit is contained in:
Bjørn Erik Pedersen
2025-02-13 10:44:44 +01:00
parent 304a7e5e74
commit d25f7ec172
3 changed files with 46 additions and 0 deletions

View File

@@ -138,6 +138,10 @@ type ExternalOptions struct {
// User defined symbols.
Defines map[string]any
// This tells esbuild to edit your source code before building to drop certain constructs.
// See https://esbuild.github.io/api/#drop
Drop string
// Maps a component import to another.
Shims map[string]string
@@ -298,6 +302,17 @@ func (opts *Options) compile() (err error) {
defines = maps.ToStringMapString(opts.Defines)
}
var drop api.Drop
switch opts.Drop {
case "":
case "console":
drop = api.DropConsole
case "debugger":
drop = api.DropDebugger
default:
err = fmt.Errorf("unsupported drop type: %q", opts.Drop)
}
// By default we only need to specify outDir and no outFile
outDir := opts.OutDir
outFile := ""
@@ -344,6 +359,7 @@ func (opts *Options) compile() (err error) {
Define: defines,
External: opts.Externals,
Drop: drop,
JSXFactory: opts.JSXFactory,
JSXFragment: opts.JSXFragment,

View File

@@ -177,6 +177,28 @@ func TestToBuildOptions(t *testing.T) {
JSX: api.JSXAutomatic,
JSXImportSource: "preact",
})
opts = Options{
ExternalOptions: ExternalOptions{
Drop: "console",
},
}
c.Assert(opts.compile(), qt.IsNil)
c.Assert(opts.compiled.Drop, qt.Equals, api.DropConsole)
opts = Options{
ExternalOptions: ExternalOptions{
Drop: "debugger",
},
}
c.Assert(opts.compile(), qt.IsNil)
c.Assert(opts.compiled.Drop, qt.Equals, api.DropDebugger)
opts = Options{
ExternalOptions: ExternalOptions{
Drop: "adsfadsf",
},
}
c.Assert(opts.compile(), qt.ErrorMatches, `unsupported drop type: "adsfadsf"`)
}
func TestToBuildOptionsTarget(t *testing.T) {