From 908b7d2d446120af0ee8993ae5b5fe698f888bb4 Mon Sep 17 00:00:00 2001 From: davfsa Date: Wed, 4 Jun 2025 09:37:42 +0200 Subject: [PATCH] feat: add minify and split source map options to bundle Signed-off-by: davfsa --- cmd/bundle.go | 36 +++++++++++++++++++++++++++++++----- lib/esbuild.go | 9 ++++++++- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/cmd/bundle.go b/cmd/bundle.go index 383333be..c239866f 100644 --- a/cmd/bundle.go +++ b/cmd/bundle.go @@ -1,18 +1,29 @@ package cmd import ( - "ags/lib" + "fmt" "os" "path/filepath" + "strings" + + "ags/lib" + "github.com/evanw/esbuild/pkg/api" "github.com/spf13/cobra" ) var ( - defines []string - usePackage bool - gtkVersion int - workingDir string + defines []string + usePackage bool + minify bool + gtkVersion int + workingDir string + sourceMapOpt string + sourceMapOpts = map[string]api.SourceMap{ + "bundle": api.SourceMapInline, + "split": api.SourceMapLinked, + "none": api.SourceMapNone, + } ) var bundleCommand = &cobra.Command{ @@ -36,12 +47,25 @@ var bundleCommand = &cobra.Command{ lib.Err(err) } + sourceMap, ok := sourceMapOpts[sourceMapOpt] + if !ok { + var validKeys []string + for key := range sourceMapOpts { + validKeys = append(validKeys, fmt.Sprintf("'%s'", key)) + } + validOptsString := strings.Join(validKeys, ", ") + + lib.Err(fmt.Errorf("sourcemap option must be one of: %s ('%s' provided)", validOptsString, sourceMapOpt)) + } + opts := lib.BundleOpts{ Outfile: outfile, UsePackage: usePackage, Defines: defines, GtkVersion: gtkVersion, WorkingDirectory: workingDir, + Minify: minify, + SourceMap: sourceMap, } if info.IsDir() { @@ -61,4 +85,6 @@ func init() { f.BoolVarP(&usePackage, "package", "p", false, "use astal package as defined in package.json") f.IntVar(>kVersion, "gtk", 3, "gtk version") f.MarkHidden("gtk") + f.BoolVar(&minify, "minify", false, "minify the output bundle") + f.StringVar(&sourceMapOpt, "sourcemap", "bundle", "what to do with the source map (valid options are: 'bundle', 'split', 'none')") } diff --git a/lib/esbuild.go b/lib/esbuild.go index b59a2d74..4c0d0aed 100644 --- a/lib/esbuild.go +++ b/lib/esbuild.go @@ -137,6 +137,8 @@ type BundleOpts struct { Defines []string GtkVersion int WorkingDirectory string + SourceMap api.SourceMap + Minify bool } // TODO: bundle plugins @@ -161,7 +163,7 @@ func Bundle(opts BundleOpts) { Platform: api.PlatformNeutral, Define: defines, Target: api.ES2022, - Sourcemap: api.SourceMapInline, + Sourcemap: opts.SourceMap, Engines: []api.Engine{ {Name: api.EngineFirefox, Version: "115"}, }, @@ -185,6 +187,11 @@ func Bundle(opts BundleOpts) { }, } + if opts.Minify { + buildOpts.MinifySyntax = true + buildOpts.MinifyWhitespace = true + } + if opts.WorkingDirectory != "" { dir, err := filepath.Abs(opts.WorkingDirectory) if err != nil {