File: cli.go

package info (click to toggle)
golang-github-evanw-esbuild 0.27.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,260 kB
  • sloc: javascript: 28,782; makefile: 820; sh: 17
file content (100 lines) | stat: -rw-r--r-- 2,994 bytes parent folder | download | duplicates (3)
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
// This API exposes the command-line interface for esbuild. It can be used to
// run esbuild from Go without the overhead of creating a child process.
//
// Example usage:
//
//	package main
//
//	import (
//	    "os"
//
//	    "github.com/evanw/esbuild/pkg/cli"
//	)
//
//	func main() {
//	    os.Exit(cli.Run(os.Args[1:]))
//	}
package cli

import (
	"errors"

	"github.com/evanw/esbuild/pkg/api"
)

// This function invokes the esbuild CLI. It takes an array of command-line
// arguments (excluding the executable argument itself) and returns an exit
// code. There are some minor differences between this CLI and the actual
// "esbuild" executable such as the lack of auxiliary flags (e.g. "--help" and
// "--version") but it is otherwise exactly the same code.
func Run(osArgs []string) int {
	return runImpl(osArgs, []api.Plugin{})
}

// This is similar to "Run()" but also takes an array of plugins to be used
// during the build process.
func RunWithPlugins(osArgs []string, plugins []api.Plugin) int {
	return runImpl(osArgs, plugins)
}

// This parses an array of strings into an options object suitable for passing
// to "api.Build()". Use this if you need to reuse the same argument parsing
// logic as the esbuild CLI.
//
// Example usage:
//
//	options, err := cli.ParseBuildOptions([]string{
//	    "input.js",
//	    "--bundle",
//	    "--minify",
//	})
//
//	result := api.Build(options)
func ParseBuildOptions(osArgs []string) (options api.BuildOptions, err error) {
	options = newBuildOptions()
	_, errWithNote := parseOptionsImpl(osArgs, &options, nil, kindExternal)
	if errWithNote != nil {
		err = errors.New(errWithNote.Text)
	}
	return
}

// This parses an array of strings into an options object suitable for passing
// to "api.Transform()". Use this if you need to reuse the same argument
// parsing logic as the esbuild CLI.
//
// Example usage:
//
//	options, err := cli.ParseTransformOptions([]string{
//	    "--minify",
//	    "--loader=tsx",
//	    "--define:DEBUG=false",
//	})
//
//	result := api.Transform(input, options)
func ParseTransformOptions(osArgs []string) (options api.TransformOptions, err error) {
	options = newTransformOptions()
	_, errWithNote := parseOptionsImpl(osArgs, nil, &options, kindExternal)
	if errWithNote != nil {
		err = errors.New(errWithNote.Text)
	}
	return
}

// This parses an array of strings into an options object suitable for passing
// to "api.Serve()". The remaining non-serve arguments are returned in another
// array to then be passed to "api.ParseBuildOptions()". Use this if you need
// to reuse the same argument parsing logic as the esbuild CLI.
//
// Example usage:
//
//	serveOptions, args, err := cli.ParseServeOptions([]string{
//	    "--serve=8000",
//	})
//
//	buildOptions, err := cli.ParseBuildOptions(args)
//
//	result := api.Serve(serveOptions, buildOptions)
func ParseServeOptions(osArgs []string) (options api.ServeOptions, remainingArgs []string, err error) {
	return parseServeOptionsImpl(osArgs)
}