From 622e593384ea111c74956e2a4373664bc334bfd6 Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Thu, 31 Oct 2019 19:39:14 +0900 Subject: [PATCH] fix: print compile warnings --- src/commands/buildJs.ts | 11 ++++++++++- src/commands/serve.ts | 4 ++-- src/compiler-core.ts | 25 +++++++++++++++++++------ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/commands/buildJs.ts b/src/commands/buildJs.ts index 01bc07320..2cfeceeef 100644 --- a/src/commands/buildJs.ts +++ b/src/commands/buildJs.ts @@ -72,12 +72,21 @@ export async function buildJs( } logWithCount(entryConfigPath, runningJobCount++, "Compiling"); - const outputs = await compileFn(options); + const [outputs, warnings] = await compileFn(options); const promises = outputs.map(async output => { await mkdir(path.dirname(output.path), { recursive: true }); return writeFile(output.path, output.src); }); await Promise.all(promises); + if (warnings && warnings.length > 0) { + warnings + .filter(warn => warn.key) + .forEach(warn => { + console.error(`[${warn.key}]${warn.description}`); + console.error(`${warn.line}:${warn.column} ${warn.source}`); + console.error(warn.context); + }); + } logWithCount(entryConfigPath, completedJobCount++, "Compiled"); } catch (e) { logWithCount(entryConfigPath, completedJobCount++, "Failed"); diff --git a/src/commands/serve.ts b/src/commands/serve.ts index 5c841afbe..bb52020d4 100644 --- a/src/commands/serve.ts +++ b/src/commands/serve.ts @@ -201,7 +201,7 @@ export async function serve(config: DuckConfig, watch = true) { createModuleUris ); updateDepsJsCache(config); - const chunkOutputs = await compileToJson(options); + const [chunkOutputs] = await compileToJson(options); const chunkIdToOutput: { [id: string]: CompilerOutput } = {}; sortedChunkIds.forEach((id, index) => { chunkIdToOutput[id] = chunkOutputs[index]; @@ -231,7 +231,7 @@ export async function serve(config: DuckConfig, watch = true) { duckConfig: DuckConfig ) { const options = createCompilerOptionsForPage(entryConfig, duckConfig, false); - const compileOutputs = await compileToJson(options); + const [compileOutputs] = await compileToJson(options); if (compileOutputs.length !== 1) { throw new Error( `Unexpectedly chunkOutputs.length must be 1, but actual ${compileOutputs.length}` diff --git a/src/compiler-core.ts b/src/compiler-core.ts index b3a84faaa..7ac1fe804 100644 --- a/src/compiler-core.ts +++ b/src/compiler-core.ts @@ -59,29 +59,42 @@ export interface CompilerOutput { source_map: string; } +export interface WarningOoutput { + line: number; + column: number; + description: string; + key: string; + context: string; + source: string; +} + /** * @throws If compiler throws errors */ export async function compileToJson( extendedOpts: ExtendedCompilerOptions -): Promise { +): Promise<[CompilerOutput[], WarningOoutput[]]> { extendedOpts.compilerOptions = { ...extendedOpts.compilerOptions, json_streams: "OUT", error_format: "JSON", }; - const outputs: CompilerOutput[] = JSON.parse(await compile(extendedOpts)); + const { stdout, stderr } = await compile(extendedOpts); + const outputs: CompilerOutput[] = JSON.parse(stdout); + const warnings: WarningOoutput[] = stderr ? JSON.parse(stderr) : []; if (extendedOpts.batch) { // Reduce transfer size in batch mode. // The maximum request/response size of AWS Lambda is 6MB each. // See https://faastjs.org/docs/aws#queue-vs-https-mode - return outputs.map(({ path, src }) => ({ path, src, source_map: "" })); + return [outputs.map(({ path, src }) => ({ path, src, source_map: "" })), warnings]; } else { - return outputs; + return [outputs, warnings]; } } -async function compile(extendedOpts: ExtendedCompilerOptions): Promise { +async function compile( + extendedOpts: ExtendedCompilerOptions +): Promise<{ stdout: string; stderr?: string }> { let opts = extendedOpts.compilerOptions; if (isInAwsLambda()) { rewriteNodePathForAwsLambda(opts); @@ -108,7 +121,7 @@ async function compile(extendedOpts: ExtendedCompilerOptions): Promise { if (exitCode !== 0) { return reject(new CompilerError(stderr || "No stderr", exitCode)); } - resolve(stdout); + resolve({ stdout, stderr }); }); }); }