Skip to content

Commit f0bff33

Browse files
committed
optimization: skip on-end packet when not needed
1 parent 345ac51 commit f0bff33

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

cmd/esbuild/service.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -621,32 +621,26 @@ func (service *serviceType) handleBuildRequest(id uint32, request map[string]int
621621
options.Plugins = append(options.Plugins, api.Plugin{
622622
Name: "onEnd",
623623
Setup: func(build api.PluginBuild) {
624-
build.OnStart(func() (api.OnStartResult, error) {
625-
activeBuild.mutex.Lock()
626-
activeBuild.didGetRebuild = true
627-
activeBuild.mutex.Unlock()
628-
return api.OnStartResult{}, nil
629-
})
630-
631624
build.OnEnd(func(result *api.BuildResult) (api.OnEndResult, error) {
632625
// For performance, we only send JavaScript an "onEnd" message if
633-
// it's needed. It's only needed if there are any "onEnd" callbacks
634-
// registered or if JavaScript has called our "rebuild()" function
635-
// (since the result for "rebuild" is passed via the same mechanism).
626+
// it's needed. It's only needed if one of the following is true:
627+
//
628+
// - There are any "onEnd" callbacks registered
629+
// - JavaScript has called our "rebuild()" function
630+
// - We are writing build output to JavaScript's stdout
631+
//
636632
// This is especially important if "write" is false since otherwise
637633
// we'd unnecessarily send the entire contents of all output files!
638634
//
639635
// "If a tree falls in a forest and no one is
640636
// around to hear it, does it make a sound?"
641637
//
642-
if !hasOnEndCallbacks {
643-
activeBuild.mutex.Lock()
644-
didGetRebuild := activeBuild.didGetRebuild
645-
activeBuild.didGetRebuild = false
646-
activeBuild.mutex.Unlock()
647-
if !didGetRebuild {
648-
return api.OnEndResult{}, nil
649-
}
638+
activeBuild.mutex.Lock()
639+
didGetRebuild := activeBuild.didGetRebuild
640+
activeBuild.didGetRebuild = false
641+
activeBuild.mutex.Unlock()
642+
if !hasOnEndCallbacks && !didGetRebuild && !writeToStdout {
643+
return api.OnEndResult{}, nil
650644
}
651645
request := resultToResponse(*result)
652646
request["command"] = "on-end"

0 commit comments

Comments
 (0)