]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
js/esbuild: Add drop option
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 13 Feb 2025 09:44:44 +0000 (10:44 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 13 Feb 2025 12:26:46 +0000 (13:26 +0100)
Fixes #13362

docs/content/en/functions/js/_common/options.md
internal/js/esbuild/options.go
internal/js/esbuild/options_test.go

index e2b6566adb65fdf0b8c3273e2c2d3bf8bfa2ff45..0d6138a7b5c90673bde44921874d6f01139eb656 100644 (file)
@@ -81,6 +81,14 @@ import * as ReactDOM from 'react-dom/client';
 {{ $defines := dict "process.env.NODE_ENV" `"development"` }}
 ```
 
+##### drop
+
+Edit your source code before building to drop certain constructs: One of `debugger` or `console`.
+
+{{< new-in 0.144.0 />}}
+
+See https://esbuild.github.io/api/#drop
+
 ###### sourceMap
 
 (`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.
index 5ea2bcea90f7d4e7c7c38baa48e724862136cf09..21f9e31cd9786f0617bf8aa01a0cd4957e6e192f 100644 (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,
index 7312eea2770d8fb1556f3d159b21b25a4c9ca698..e92c3bea681c18a2ea65dbddcfcc71e89cc1fae4 100644 (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) {