From 2391c3171e910b96fa28f80d06b5840c9cfab232 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Mon, 8 Mar 2021 20:43:31 -0800 Subject: [PATCH] have the js API write to stdout by default --- cmd/esbuild/service.go | 19 +++++++++---------- lib/common.ts | 1 + lib/stdio_protocol.ts | 1 + 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/cmd/esbuild/service.go b/cmd/esbuild/service.go index 9ddb8e1395e..8907b3b631d 100644 --- a/cmd/esbuild/service.go +++ b/cmd/esbuild/service.go @@ -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)} @@ -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 } @@ -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) diff --git a/lib/common.ts b/lib/common.ts index cb3cc9e7166..a1f67adf54a 100644 --- a/lib/common.ts +++ b/lib/common.ts @@ -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) { diff --git a/lib/stdio_protocol.ts b/lib/stdio_protocol.ts index 7bbb4da0bf9..3e96a8679f2 100644 --- a/lib/stdio_protocol.ts +++ b/lib/stdio_protocol.ts @@ -49,6 +49,7 @@ export interface BuildResponse { warnings: types.Message[]; outputFiles: BuildOutputFile[]; metafile: string; + writeToStdout?: Uint8Array; rebuildID?: number; watchID?: number; }