Skip to content

Commit

Permalink
add: bundle tsconfig flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Aylur committed Nov 8, 2024
1 parent 250d501 commit f89742e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 26 deletions.
20 changes: 18 additions & 2 deletions cmd/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
"github.com/spf13/cobra"
)

var (
tsconfig string
tsconfigRaw string
)

var bundleCommand = &cobra.Command{
Use: "bundle [entryfile] [outfile]",
Short: "Bundle an app",
Expand All @@ -28,10 +33,21 @@ var bundleCommand = &cobra.Command{
lib.Err(err)
}

opt := lib.BundleOptions{
Tsconfig: tsconfig,
TsconfigRaw: tsconfigRaw,
}

if info.IsDir() {
lib.Bundle(path, getAppEntry(path), outfile)
lib.Bundle(getAppEntry(path), outfile, opt)
} else {
lib.Bundle(filepath.Dir(path), path, outfile)
lib.Bundle(path, outfile, opt)
}
},
}

func init() {
f := bundleCommand.Flags()
f.StringVar(&tsconfig, "tsconfig", "", "path to tsconfig.json")
f.StringVar(&tsconfigRaw, "tsconfig-raw", "", "content of tsconfig.json")
}
5 changes: 1 addition & 4 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ func initConfig(cmd *cobra.Command, args []string) {
lib.WriteFile(configDir+"/widget/Bar.tsx", bartsx)
lib.WriteFile(configDir+"/app.ts", appts)

if err := genTypes(configDir, "*"); err != nil {
lib.Err(err)
}

genTypes(configDir, "*")
fmt.Println(lib.Green("project ready") + " at " + lib.Cyan(configDir))
}
12 changes: 6 additions & 6 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ var runCommand = &cobra.Command{
}

if info.IsDir() {
run(getAppEntry(path), path)
run(getAppEntry(path))
} else {
run(path, filepath.Dir(path))
run(path)
}

} else {
run(getAppEntry(targetDir), targetDir)
run(getAppEntry(targetDir))
}
},
}
Expand Down Expand Up @@ -83,9 +83,9 @@ func getAppEntry(dir string) string {
return infile
}

func run(infile, dir string) {
func run(infile string) {
outfile := getOutfile()
lib.Bundle(dir, infile, outfile)
lib.Bundle(infile, outfile, lib.BundleOptions{})

if gtk4 {
os.Setenv("LD_PRELOAD", gtk4LayerShell)
Expand All @@ -95,7 +95,7 @@ func run(infile, dir string) {
gjs.Stdout = os.Stdout
gjs.Stderr = os.Stderr
gjs.Stdin = os.Stdin
gjs.Dir = dir
gjs.Dir = filepath.Dir(infile)

// TODO: watch and restart
if err := gjs.Run(); err != nil {
Expand Down
9 changes: 6 additions & 3 deletions cmd/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ func showCursor() {
fmt.Print("\033[?25h")
}

func genTypes(configDir, pattern string) error {
func genTypes(configDir, pattern string) {
lib.Mkdir(configDir)

npx, err := exec.LookPath("npx")
if err != nil {
return err
lib.Err(err)
}

flags := []string{
Expand All @@ -105,5 +105,8 @@ func genTypes(configDir, pattern string) error {

showCursor()

return err
if err != nil {
lib.Err("type generation failed, try running\n" +
lib.Yellow(npx+" "+strings.Join(flags, " ")))
}
}
39 changes: 28 additions & 11 deletions lib/esbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,27 @@ var blpPlugin api.Plugin = api.Plugin{
},
}

type BundleOptions struct {
Tsconfig string
TsconfigRaw string
}

// TODO:
// svg loader
// other css preproceccors
// http plugin with caching
func Bundle(srcdir, infile, outfile string) {
result := api.Build(api.BuildOptions{
Color: api.ColorAlways,
LogLevel: api.LogLevelWarning,
EntryPoints: []string{infile},
Bundle: true,
Outfile: outfile,
Format: api.FormatESModule,
Platform: api.PlatformNeutral,
Write: true,
func Bundle(infile, outfile string, opts BundleOptions) {
srcdir := filepath.Dir(infile)
options := api.BuildOptions{
Color: api.ColorAlways,
LogLevel: api.LogLevelWarning,
EntryPoints: []string{infile},
Bundle: true,
Outfile: outfile,
Format: api.FormatESModule,
Platform: api.PlatformNeutral,
Write: true,
AbsWorkingDir: srcdir,
Define: map[string]string{
"SRC": fmt.Sprintf(`"%s"`, srcdir),
},
Expand All @@ -153,7 +160,17 @@ func Bundle(srcdir, infile, outfile string) {
sassPlugin,
blpPlugin,
},
})
}

if opts.Tsconfig != "" {
options.Tsconfig = opts.Tsconfig
}

if opts.TsconfigRaw != "" {
options.TsconfigRaw = opts.TsconfigRaw
}

result := api.Build(options)

// TODO: custom error logs
if len(result.Errors) > 0 {
Expand Down

0 comments on commit f89742e

Please sign in to comment.