Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions cmd/bundle.go
Original file line number Diff line number Diff line change
@@ -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{
Expand All @@ -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() {
Expand All @@ -61,4 +85,6 @@ func init() {
f.BoolVarP(&usePackage, "package", "p", false, "use astal package as defined in package.json")
f.IntVar(&gtkVersion, "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')")
}
9 changes: 8 additions & 1 deletion lib/esbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ type BundleOpts struct {
Defines []string
GtkVersion int
WorkingDirectory string
SourceMap api.SourceMap
Minify bool
}

// TODO: bundle plugins
Expand All @@ -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"},
},
Expand All @@ -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 {
Expand Down