Skip to content

Commit

Permalink
cli: implement --failAfterWarnings flag
Browse files Browse the repository at this point in the history
closes #3021
  • Loading branch information
tjenkinson committed Aug 10, 2020
1 parent 8333387 commit 29a9465
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions cli/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Basic options:
--preserveSymlinks Do not follow symlinks when resolving files
--shimMissingExports Create shim variables for missing exports
--silent Don't print warnings
--failAfterWarnings Exit with an error code if there was a warning during the build
--sourcemapExcludeSources Do not include source code in source maps
--sourcemapFile <file> Specify bundle position for source maps
--no-stdin do not read "-" from stdin
Expand Down
7 changes: 7 additions & 0 deletions cli/run/batchWarnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@ import { stderr } from '../logging';

export interface BatchWarnings {
add: (warning: RollupWarning) => void;
readonly total: number;
readonly count: number;
flush: () => void;
}

export default function batchWarnings() {
let deferredWarnings = new Map<keyof typeof deferredHandlers, RollupWarning[]>();
let total = 0;
let count = 0;

return {
get total() {
return total;
},

get count() {
return count;
},

add: (warning: RollupWarning) => {
total += 1;
count += 1;

if (warning.code! in deferredHandlers) {
Expand Down
6 changes: 6 additions & 0 deletions cli/run/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ export default async function runRollup(command: any) {
for (const inputOptions of options) {
await build(inputOptions, warnings, command.silent);
}
if (command.failAfterWarnings && warnings.total) {
throw {
code: 'FAIL_AFTER_WARNINGS',
message: 'Warnings occurred and --failAfterWarnings flag present'
};
}
} catch (err) {
warnings.flush();
handleError(err);
Expand Down
1 change: 1 addition & 0 deletions src/utils/options/mergeOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export function mergeOptions(
'environment',
'plugin',
'silent',
'failAfterWarnings',
'stdin',
'waitForBundleInput'
),
Expand Down
9 changes: 9 additions & 0 deletions test/cli/samples/fail-after-warnings/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { assertIncludes } = require('../../../utils.js');

module.exports = {
description: 'errors on warnings with --failAfterWarnings',
command: 'rollup -i main.js --failAfterWarnings',
error: () => true,
stderr: stderr =>
assertIncludes(stderr, '[!] Warnings occurred and --failAfterWarnings flag present')
};
5 changes: 5 additions & 0 deletions test/cli/samples/fail-after-warnings/_expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

require('unknown');

console.log(42);
3 changes: 3 additions & 0 deletions test/cli/samples/fail-after-warnings/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import something from 'unknown';

console.log(42);

0 comments on commit 29a9465

Please sign in to comment.