Skip to content

Commit

Permalink
fix: print compile warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 committed Oct 31, 2019
1 parent 3145422 commit 622e593
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
11 changes: 10 additions & 1 deletion src/commands/buildJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions src/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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}`
Expand Down
25 changes: 19 additions & 6 deletions src/compiler-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompilerOutput[]> {
): 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<string> {
async function compile(
extendedOpts: ExtendedCompilerOptions
): Promise<{ stdout: string; stderr?: string }> {
let opts = extendedOpts.compilerOptions;
if (isInAwsLambda()) {
rewriteNodePathForAwsLambda(opts);
Expand All @@ -108,7 +121,7 @@ async function compile(extendedOpts: ExtendedCompilerOptions): Promise<string> {
if (exitCode !== 0) {
return reject(new CompilerError(stderr || "No stderr", exitCode));
}
resolve(stdout);
resolve({ stdout, stderr });
});
});
}
Expand Down

0 comments on commit 622e593

Please sign in to comment.