idecnode/cmd/frontend/frontend.go

69 lines
1.3 KiB
Go
Raw Permalink Normal View History

2024-10-29 01:21:05 +03:00
package frontend
import (
"errors"
"log"
"github.com/evanw/esbuild/pkg/api"
"github.com/urfave/cli/v2"
)
var FrontendCommand *cli.Command = &cli.Command{
Name: "frontend",
Description: "Build/watch frontend",
Action: func(c *cli.Context) error {
dev := c.Bool("dev")
watch := c.Bool("watch")
buildOpts := api.BuildOptions{
EntryPoints: []string{
"./web/src/index.js",
"./web/src/index.css",
"./web/src/index.html",
},
Outdir: "./web/dist",
Bundle: true,
Write: true,
MinifyWhitespace: !dev,
MinifyIdentifiers: !dev,
MinifySyntax: !dev,
JSX: api.JSXAutomatic,
Loader: map[string]api.Loader{
".js": api.LoaderJSX,
".css": api.LoaderCSS,
".html": api.LoaderCopy,
},
}
if watch {
ctx, err := api.Context(buildOpts)
if err != nil {
return err
}
if err := ctx.Watch(api.WatchOptions{}); err != nil {
return err
}
log.Println("watching frontend")
<-c.Done()
} else {
if br := api.Build(buildOpts); br.Errors != nil {
for _, e := range br.Errors {
log.Println(e.Location, e.Detail, e.Text)
}
return errors.New("build failed")
}
}
return nil
},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "dev",
Value: false,
},
&cli.BoolFlag{
Name: "watch",
Value: false,
},
},
}