summaryrefslogtreecommitdiff
path: root/cmd/frontend/frontend.go
blob: fc7dbc85692d35892c28e87113199af913f74011 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
			},
		},
	},
}