From 34ccc98a942098faefb5f4211b215ff9ccc7ad0e Mon Sep 17 00:00:00 2001 From: Alexander Neonxp Kiryukhin Date: Mon, 9 Dec 2024 01:07:15 +0300 Subject: Начальный MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/frontend/frontend.go | 101 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 cmd/frontend/frontend.go (limited to 'cmd/frontend') diff --git a/cmd/frontend/frontend.go b/cmd/frontend/frontend.go new file mode 100644 index 0000000..fc7dbc8 --- /dev/null +++ b/cmd/frontend/frontend.go @@ -0,0 +1,101 @@ +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 + }, + }, + }, +} -- cgit v1.2.3