Skip to content

Commit

Permalink
optimization: skip on-end packet when not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jan 17, 2023
1 parent 345ac51 commit f0bff33
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions cmd/esbuild/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,32 +621,26 @@ func (service *serviceType) handleBuildRequest(id uint32, request map[string]int
options.Plugins = append(options.Plugins, api.Plugin{
Name: "onEnd",
Setup: func(build api.PluginBuild) {
build.OnStart(func() (api.OnStartResult, error) {
activeBuild.mutex.Lock()
activeBuild.didGetRebuild = true
activeBuild.mutex.Unlock()
return api.OnStartResult{}, nil
})

build.OnEnd(func(result *api.BuildResult) (api.OnEndResult, error) {
// For performance, we only send JavaScript an "onEnd" message if
// it's needed. It's only needed if there are any "onEnd" callbacks
// registered or if JavaScript has called our "rebuild()" function
// (since the result for "rebuild" is passed via the same mechanism).
// it's needed. It's only needed if one of the following is true:
//
// - There are any "onEnd" callbacks registered
// - JavaScript has called our "rebuild()" function
// - We are writing build output to JavaScript's stdout
//
// This is especially important if "write" is false since otherwise
// we'd unnecessarily send the entire contents of all output files!
//
// "If a tree falls in a forest and no one is
// around to hear it, does it make a sound?"
//
if !hasOnEndCallbacks {
activeBuild.mutex.Lock()
didGetRebuild := activeBuild.didGetRebuild
activeBuild.didGetRebuild = false
activeBuild.mutex.Unlock()
if !didGetRebuild {
return api.OnEndResult{}, nil
}
activeBuild.mutex.Lock()
didGetRebuild := activeBuild.didGetRebuild
activeBuild.didGetRebuild = false
activeBuild.mutex.Unlock()
if !hasOnEndCallbacks && !didGetRebuild && !writeToStdout {
return api.OnEndResult{}, nil
}
request := resultToResponse(*result)
request["command"] = "on-end"
Expand Down

0 comments on commit f0bff33

Please sign in to comment.