-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
238 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,71 @@ | ||
// 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 ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/evanw/esbuild/internal/logging" | ||
"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 { | ||
buildOptions, transformOptions, err := parseOptionsForRun(osArgs) | ||
|
||
switch { | ||
case buildOptions != nil: | ||
// Run the build and stop if there were errors | ||
result := api.Build(*buildOptions) | ||
if len(result.Errors) > 0 { | ||
return 1 | ||
} | ||
|
||
// Special-case writing to stdout | ||
if buildOptions.Outfile == "" && buildOptions.Outdir == "" { | ||
if len(result.OutputFiles) != 1 { | ||
logging.PrintErrorToStderr(osArgs, fmt.Sprintf( | ||
"Internal error: did not expect to generate %d files when writing to stdout", len(result.OutputFiles))) | ||
} else if _, err := os.Stdout.Write(result.OutputFiles[0].Contents); err != nil { | ||
logging.PrintErrorToStderr(osArgs, fmt.Sprintf( | ||
"Failed to write to stdout: %s", err.Error())) | ||
} | ||
} else { | ||
for _, outputFile := range result.OutputFiles { | ||
if err := os.MkdirAll(filepath.Dir(outputFile.Path), 0755); err != nil { | ||
result.Errors = append(result.Errors, api.Message{Text: fmt.Sprintf( | ||
"Failed to create output directory: %s", err.Error())}) | ||
} else if err := ioutil.WriteFile(outputFile.Path, outputFile.Contents, 0644); err != nil { | ||
logging.PrintErrorToStderr(osArgs, fmt.Sprintf( | ||
"Failed to write to output file: %s", err.Error())) | ||
} | ||
} | ||
} | ||
|
||
case transformOptions != nil: | ||
// Read the input from stdin | ||
bytes, err := ioutil.ReadAll(os.Stdin) | ||
if err != nil { | ||
logging.PrintErrorToStderr(osArgs, fmt.Sprintf( | ||
"Could not read from stdin: %s", err.Error())) | ||
return 1 | ||
} | ||
|
||
// Run the transform and stop if there were errors | ||
result := api.Transform(string(bytes), *transformOptions) | ||
if len(result.Errors) > 0 { | ||
return 1 | ||
} | ||
|
||
// Write the output to stdout | ||
os.Stdout.Write(result.JS) | ||
|
||
case err != nil: | ||
logging.PrintErrorToStderr(osArgs, err.Error()) | ||
return 1 | ||
} | ||
|
||
return 0 | ||
return runImpl(osArgs) | ||
} | ||
|
||
// This returns either BuildOptions, TransformOptions, or an error | ||
func parseOptionsForRun(osArgs []string) (*api.BuildOptions, *api.TransformOptions, error) { | ||
// If there's an entry point, then we're building | ||
for _, arg := range osArgs { | ||
if !strings.HasPrefix(arg, "-") { | ||
options := newBuildOptions() | ||
|
||
// Apply defaults appropriate for the CLI | ||
options.ErrorLimit = 10 | ||
options.LogLevel = api.LogLevelInfo | ||
|
||
err := parseOptionsImpl(osArgs, &options, nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
return &options, nil, nil | ||
} | ||
} | ||
|
||
// Otherwise, we're transforming | ||
options := newTransformOptions() | ||
|
||
// Apply defaults appropriate for the CLI | ||
options.ErrorLimit = 10 | ||
options.LogLevel = api.LogLevelInfo | ||
// 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() | ||
err = parseOptionsImpl(osArgs, &options, nil) | ||
return | ||
} | ||
|
||
err := parseOptionsImpl(osArgs, nil, &options) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
if options.Sourcemap != api.SourceMapNone && options.Sourcemap != api.SourceMapInline { | ||
return nil, nil, fmt.Errorf("Must use \"inline\" source map when transforming stdin") | ||
} | ||
return nil, &options, nil | ||
// 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() | ||
err = parseOptionsImpl(osArgs, nil, &options) | ||
return | ||
} |
Oops, something went wrong.