Skip to content

Commit

Permalink
performance: write out files in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Aug 9, 2020
1 parent 5005aab commit ddc0fb5
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions pkg/api/api_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"regexp"
"strconv"
"strings"
"sync"

"github.com/evanw/esbuild/internal/ast"
"github.com/evanw/esbuild/internal/bundler"
Expand Down Expand Up @@ -540,15 +541,22 @@ func buildImpl(buildOpts BuildOptions) BuildResult {
"Failed to write to stdout: %s", err.Error()))
}
} else {
// Write out files in parallel
waitGroup := sync.WaitGroup{}
waitGroup.Add(len(outputFiles))
for _, outputFile := range outputFiles {
if err := os.MkdirAll(filepath.Dir(outputFile.Path), 0755); err != nil {
log.AddError(nil, ast.Loc{}, fmt.Sprintf(
"Failed to create output directory: %s", err.Error()))
} else if err := ioutil.WriteFile(outputFile.Path, outputFile.Contents, 0644); err != nil {
log.AddError(nil, ast.Loc{}, fmt.Sprintf(
"Failed to write to output file: %s", err.Error()))
}
go func(outputFile OutputFile) {
if err := os.MkdirAll(filepath.Dir(outputFile.Path), 0755); err != nil {
log.AddError(nil, ast.Loc{}, fmt.Sprintf(
"Failed to create output directory: %s", err.Error()))
} else if err := ioutil.WriteFile(outputFile.Path, outputFile.Contents, 0644); err != nil {
log.AddError(nil, ast.Loc{}, fmt.Sprintf(
"Failed to write to output file: %s", err.Error()))
}
waitGroup.Done()
}(outputFile)
}
waitGroup.Wait()
}
}
}
Expand Down

0 comments on commit ddc0fb5

Please sign in to comment.