github.com/evanw/esbuild@v0.21.4/pkg/cli/cli.go (about)

     1  // This API exposes the command-line interface for esbuild. It can be used to
     2  // run esbuild from Go without the overhead of creating a child process.
     3  //
     4  // Example usage:
     5  //
     6  //	package main
     7  //
     8  //	import (
     9  //	    "os"
    10  //
    11  //	    "github.com/evanw/esbuild/pkg/cli"
    12  //	)
    13  //
    14  //	func main() {
    15  //	    os.Exit(cli.Run(os.Args[1:]))
    16  //	}
    17  package cli
    18  
    19  import (
    20  	"errors"
    21  
    22  	"github.com/evanw/esbuild/pkg/api"
    23  )
    24  
    25  // This function invokes the esbuild CLI. It takes an array of command-line
    26  // arguments (excluding the executable argument itself) and returns an exit
    27  // code. There are some minor differences between this CLI and the actual
    28  // "esbuild" executable such as the lack of auxiliary flags (e.g. "--help" and
    29  // "--version") but it is otherwise exactly the same code.
    30  func Run(osArgs []string) int {
    31  	return runImpl(osArgs)
    32  }
    33  
    34  // This parses an array of strings into an options object suitable for passing
    35  // to "api.Build()". Use this if you need to reuse the same argument parsing
    36  // logic as the esbuild CLI.
    37  //
    38  // Example usage:
    39  //
    40  //	options, err := cli.ParseBuildOptions([]string{
    41  //	    "input.js",
    42  //	    "--bundle",
    43  //	    "--minify",
    44  //	})
    45  //
    46  //	result := api.Build(options)
    47  func ParseBuildOptions(osArgs []string) (options api.BuildOptions, err error) {
    48  	options = newBuildOptions()
    49  	_, errWithNote := parseOptionsImpl(osArgs, &options, nil, kindExternal)
    50  	if errWithNote != nil {
    51  		err = errors.New(errWithNote.Text)
    52  	}
    53  	return
    54  }
    55  
    56  // This parses an array of strings into an options object suitable for passing
    57  // to "api.Transform()". Use this if you need to reuse the same argument
    58  // parsing logic as the esbuild CLI.
    59  //
    60  // Example usage:
    61  //
    62  //	options, err := cli.ParseTransformOptions([]string{
    63  //	    "--minify",
    64  //	    "--loader=tsx",
    65  //	    "--define:DEBUG=false",
    66  //	})
    67  //
    68  //	result := api.Transform(input, options)
    69  func ParseTransformOptions(osArgs []string) (options api.TransformOptions, err error) {
    70  	options = newTransformOptions()
    71  	_, errWithNote := parseOptionsImpl(osArgs, nil, &options, kindExternal)
    72  	if errWithNote != nil {
    73  		err = errors.New(errWithNote.Text)
    74  	}
    75  	return
    76  }
    77  
    78  // This parses an array of strings into an options object suitable for passing
    79  // to "api.Serve()". The remaining non-serve arguments are returned in another
    80  // array to then be passed to "api.ParseBuildOptions()". Use this if you need
    81  // to reuse the same argument parsing logic as the esbuild CLI.
    82  //
    83  // Example usage:
    84  //
    85  //	serveOptions, args, err := cli.ParseServeOptions([]string{
    86  //	    "--serve=8000",
    87  //	})
    88  //
    89  //	buildOptions, err := cli.ParseBuildOptions(args)
    90  //
    91  //	result := api.Serve(serveOptions, buildOptions)
    92  func ParseServeOptions(osArgs []string) (options api.ServeOptions, remainingArgs []string, err error) {
    93  	return parseServeOptionsImpl(osArgs)
    94  }