From 3f57db853fac17268358cf155268834861aba21b Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Sun, 30 Jun 2024 14:33:17 -0400 Subject: [PATCH] release notes for #3539 --- CHANGELOG.md | 6 ++++++ pkg/cli/cli.go | 9 ++++----- pkg/cli/cli_impl.go | 7 ++++++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df38fb52849..ea0d8abcfb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,12 @@ Plugins that return values from `onResolve` without resolving the path (i.e. without setting either `path` or `external: true`) will now cause a warning. This is because esbuild only uses return values from `onResolve` if it successfully resolves the path, and it's not good for invalid input to be silently ignored. +* Add a new Go API for running the CLI with plugins ([#3539](https://github.com/evanw/esbuild/pull/3539)) + + With esbuild's Go API, you can now call `cli.RunWithPlugins(args, plugins)` to pass an array of esbuild plugins to be used during the build process. This allows you to create a CLI that behaves similarly to esbuild's CLI but with additional Go plugins enabled. + + This was contributed by [@edewit](https://github.com/edewit). + ## 0.21.5 * Fix `Symbol.metadata` on classes without a class decorator ([#3781](https://github.com/evanw/esbuild/issues/3781)) diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index 3bde32aa45c..f339c7e3163 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -31,11 +31,10 @@ func Run(osArgs []string) int { return runImpl(osArgs, []api.Plugin{}) } -// This function invokes the esbuild CLI. It takes an array of command-line -// arguments (excluding the executable argument itself) and returns an exit -// code. It also takes adds some plugins that need to be added to the run -func RunWithPlugins(osArgs []string, plugin []api.Plugin) int { - return runImpl(osArgs, 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 diff --git a/pkg/cli/cli_impl.go b/pkg/cli/cli_impl.go index b3345ef4591..cc98920cc0b 100644 --- a/pkg/cli/cli_impl.go +++ b/pkg/cli/cli_impl.go @@ -1143,6 +1143,12 @@ func runImpl(osArgs []string, plugins []api.Plugin) int { osArgs, analyze := filterAnalyzeFlags(osArgs) buildOptions, transformOptions, extras, err := parseOptionsForRun(osArgs) + + // Add any plugins from the caller after parsing the build options + if buildOptions != nil { + buildOptions.Plugins = append(buildOptions.Plugins, plugins...) + } + if analyze != analyzeDisabled { addAnalyzePlugin(buildOptions, analyze, osArgs) } @@ -1280,7 +1286,6 @@ func runImpl(osArgs []string, plugins []api.Plugin) int { } } - buildOptions.Plugins = plugins // Handle post-build actions with a plugin so they also work in watch mode buildOptions.Plugins = append(buildOptions.Plugins, api.Plugin{ Name: "PostBuildActions",