Skip to content

Commit

Permalink
have the js API write to stdout by default
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Mar 9, 2021
1 parent 3a3206a commit 2391c31
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
19 changes: 9 additions & 10 deletions cmd/esbuild/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,15 +356,9 @@ func (service *serviceType) handleBuildRequest(id uint32, request map[string]int
// Normally when "write" is true and there is no output file/directory then
// the output is written to stdout instead. However, we're currently using
// stdout as a communication channel and writing the build output to stdout
// would corrupt our protocol.
//
// While we could channel this back to the host process and write it to
// stdout there, the public Go API we're about to call doesn't have an option
// for "write to stdout but don't actually write" and I don't think it should.
// For now let's just forbid this case because it's not even that useful.
if err == nil && !isServe && write && options.Outfile == "" && options.Outdir == "" {
err = errors.New("Either provide \"outfile\" or set \"write\" to false")
}
// would corrupt our protocol. Special-case this to channel this back to the
// host process and write it to stdout there.
writeToStdout := err == nil && !isServe && write && options.Outfile == "" && options.Outdir == ""

if err != nil {
return outgoingPacket{bytes: encodeErrorPacket(id, err)}
Expand Down Expand Up @@ -420,6 +414,9 @@ func (service *serviceType) handleBuildRequest(id uint32, request map[string]int
if options.Metafile {
response["metafile"] = result.Metafile
}
if writeToStdout && len(result.OutputFiles) == 1 {
response["writeToStdout"] = result.OutputFiles[0].Contents
}
return response
}

Expand All @@ -433,7 +430,9 @@ func (service *serviceType) handleBuildRequest(id uint32, request map[string]int
}
}

options.Write = write
if !writeToStdout {
options.Write = write
}
options.Incremental = incremental
result := api.Build(options)
response := resultToResponse(result)
Expand Down
1 change: 1 addition & 0 deletions lib/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ export function createChannel(streamIn: StreamIn): StreamOut {
let result: types.BuildResult = { warnings };
if (response!.outputFiles) result.outputFiles = response!.outputFiles.map(convertOutputFiles);
if (response!.metafile) result.metafile = JSON.parse(response!.metafile);
if (response!.writeToStdout !== void 0) console.log(protocol.decodeUTF8(response!.writeToStdout).replace(/\n$/, ''));

// Handle incremental rebuilds
if (response!.rebuildID !== void 0) {
Expand Down
1 change: 1 addition & 0 deletions lib/stdio_protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface BuildResponse {
warnings: types.Message[];
outputFiles: BuildOutputFile[];
metafile: string;
writeToStdout?: Uint8Array;
rebuildID?: number;
watchID?: number;
}
Expand Down

0 comments on commit 2391c31

Please sign in to comment.