package frontend import ( "context" "errors" "fmt" "strings" "github.com/evanw/esbuild/pkg/api" "github.com/urfave/cli/v3" ) var Command = &cli.Command{ Name: "frontend", Commands: []*cli.Command{ { Name: "build", Action: func(ctx context.Context, c *cli.Command) error { result := api.Build(api.BuildOptions{ EntryPoints: []string{"./frontend/index.jsx"}, Outdir: "static/assets", Bundle: true, Write: true, LogLevel: api.LogLevelInfo, ChunkNames: "chunks/[name]-[hash]", MinifyWhitespace: true, MinifyIdentifiers: true, MinifySyntax: true, Splitting: false, Sourcemap: api.SourceMapInline, Format: api.FormatDefault, Color: api.ColorAlways, Define: map[string]string{ "process.env.NODE_ENV": `"dev"`, }, AssetNames: "assets/[name]-[hash]", Loader: map[string]api.Loader{ ".png": api.LoaderFile, ".css": api.LoaderCSS, }, }) if len(result.Errors) > 0 { errs := make([]string, 0, len(result.Errors)) for _, e := range result.Errors { errs = append(errs, fmt.Sprintf("%s: %s", e.PluginName, e.Text)) } return errors.New(strings.Join(errs, ", ")) } return nil }, }, { Name: "watch", Action: func(ctx context.Context, c *cli.Command) error { bctx, result := api.Context(api.BuildOptions{ EntryPoints: []string{"./frontend/index.jsx"}, Outdir: "static/assets", Bundle: true, Write: true, LogLevel: api.LogLevelInfo, ChunkNames: "chunks/[name]-[hash]", MinifyWhitespace: false, MinifyIdentifiers: false, MinifySyntax: false, Splitting: false, Sourcemap: api.SourceMapInline, Format: api.FormatESModule, Color: api.ColorAlways, Define: map[string]string{ "process.env.NODE_ENV": `"dev"`, }, AssetNames: "assets/[name]-[hash]", Loader: map[string]api.Loader{ ".png": api.LoaderFile, ".css": api.LoaderCSS, }, }) if result != nil && len(result.Errors) > 0 { errs := make([]string, 0, len(result.Errors)) for _, e := range result.Errors { errs = append(errs, fmt.Sprintf("%s: %s", e.PluginName, e.Text)) } return errors.New(strings.Join(errs, ", ")) } if err := bctx.Watch(api.WatchOptions{}); err != nil { return err } fmt.Printf("watching...\n") <-make(chan struct{}) return nil }, }, }, }