Skip to content

Commit

Permalink
new: Intercept warnings and customize.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Oct 23, 2020
1 parent 8b534e2 commit 840a93d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
41 changes: 41 additions & 0 deletions src/Artifact.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { applyStyle } from '@boost/cli';
import Package from './Package';
import { ArtifactState, Awaitable, BuildResult, BuildOptions } from './types';

Expand Down Expand Up @@ -35,6 +36,46 @@ export default abstract class Artifact<T extends object = {}> {
return this.state === 'failed';
}

protected logWithSource(
message: string,
level: 'info' | 'warn' | 'error',
{
id,
output,
sourceColumn,
sourceFile,
sourceLine,
}: {
id?: string;
output?: string;
sourceColumn?: number;
sourceFile?: string;
sourceLine?: number;
} = {},
) {
let msg = `[${this.package.getName()}${output ? `/${output}` : ''}] ${message}`;

const meta: string[] = [];

if (id) {
meta.push(`id=${id}`);
}

if (sourceFile) {
meta.push(`file=${sourceFile}`);
}

if (sourceFile || sourceColumn) {
meta.push(`line=${sourceLine ?? '?'}:${sourceColumn ?? '?'}`);
}

if (meta.length > 0) {
msg += applyStyle(` (${meta.join(' ')})`, 'muted');
}

console[level](msg);
}

abstract getLabel(): string;

abstract getBuildTargets(): string[];
Expand Down
13 changes: 12 additions & 1 deletion src/BundleArtifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,18 @@ export default class BundleArtifact extends Artifact<BundleBuild> {
debug('Building %s bundle artifact with Rollup', this.outputName);

const { output = [], ...input } = getRollupConfig(this, this.package.getFeatureFlags());
const bundle = await rollup(input);
const bundle = await rollup({
...input,
onwarn: ({ id, loc = {}, message }) => {
this.logWithSource(message, 'warn', {
id,
output: this.outputName,
sourceColumn: loc.column,
sourceFile: loc.file,
sourceLine: loc.line,
});
},
});

if (bundle.cache) {
this.cache = bundle.cache;
Expand Down
25 changes: 24 additions & 1 deletion src/TypesArtifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,30 @@ export default class TypesArtifact extends Artifact<TypesBuild> {
// Extract all DTS into a single file
const result = Extractor.invoke(ExtractorConfig.loadFileAndPrepare(configPath), {
localBuild: process.env.NODE_ENV !== 'production',
showVerboseMessages: false,
messageCallback: (warn) => {
if (warn.messageId === 'ae-missing-release-tag' || warn.logLevel === 'verbose') {
return;
}

let level = 'info';

if (warn.logLevel === 'error') {
level = 'error';
} else if (warn.logLevel === 'warning') {
level = 'warn';
}

this.logWithSource(warn.text, level as 'info', {
id: warn.messageId,
output: outputName,
sourceColumn: warn.sourceFileColumn,
sourceFile: warn.sourceFilePath,
sourceLine: warn.sourceFileLine,
});

// eslint-disable-next-line no-param-reassign
warn.handled = true;
},
});

if (!result.succeeded) {
Expand Down

0 comments on commit 840a93d

Please sign in to comment.