Skip to content

Commit

Permalink
Do not skip onwarn handler when --silent is used (#2981)
Browse files Browse the repository at this point in the history
* Do not skip onwarn with --silent

* Update documentation
  • Loading branch information
lukastaegert committed Jul 4, 2019
1 parent ef7486d commit b1afb6e
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 13 deletions.
2 changes: 1 addition & 1 deletion bin/src/run/batchWarnings.ts
Expand Up @@ -253,7 +253,7 @@ const deferredHandlers: {
let lastUrl: string;

nestedByMessage.forEach(({ key: message, items }) => {
title(`${plugin} plugin: ${message}`);
title(`Plugin ${plugin}: ${message}`);
items.forEach(warning => {
if (warning.url !== lastUrl) info((lastUrl = warning.url as string));

Expand Down
9 changes: 5 additions & 4 deletions bin/src/run/build.ts
Expand Up @@ -77,13 +77,14 @@ export default function build(
);
})
.then((bundle?: RollupBuild) => {
warnings.flush();
if (!silent)
if (!silent) {
warnings.flush();
stderr(
tc.green(`created ${tc.bold(files.join(', '))} in ${tc.bold(ms(Date.now() - start))}`)
);
if (bundle && bundle.getTimings) {
printTimings(bundle.getTimings());
if (bundle && bundle.getTimings) {
printTimings(bundle.getTimings());
}
}
})
.catch((err: any) => {
Expand Down
4 changes: 2 additions & 2 deletions docs/01-command-line-reference.md
Expand Up @@ -165,7 +165,7 @@ $ rollup --config
$ rollup --config my.config.js
```

You can also export a function that returns any of the above configuration formats. This function will be passed the current command line arguments so that you can dynamically adapt your configuration to respect e.g. `--silent`. You can even define your own command line options if you prefix them with `config`:
You can also export a function that returns any of the above configuration formats. This function will be passed the current command line arguments so that you can dynamically adapt your configuration to respect e.g. [`--silent`](guide/en/#--silent). You can even define your own command line options if you prefix them with `config`:

```javascript
// rollup.config.js
Expand Down Expand Up @@ -254,7 +254,7 @@ Rebuild the bundle when its source files change on disk.

#### `--silent`

Don't print warnings to the console.
Don't print warnings to the console. If your configuration file contains an `onwarn` handler, this handler will still be called. To manually prevent that, you can access the command line options in your configuration file as described at the end of [Configuration Files](guide/en/#configuration-files).

#### `--environment <values>`

Expand Down
2 changes: 1 addition & 1 deletion docs/999-big-list-of-options.md
Expand Up @@ -317,7 +317,7 @@ Be aware that manual chunks can change the behaviour of the application if side-
#### onwarn
Type: `(warning: RollupWarning, defaultHandler: (warning: string | RollupWarning) => void) => void;`

A function that will intercept warning messages. If not supplied, warnings will be deduplicated and printed to the console.
A function that will intercept warning messages. If not supplied, warnings will be deduplicated and printed to the console. When using the [`--silent`](guide/en/#--silent) CLI option, this handler is the only way to get notified about warnings.

The function receives two arguments: the warning object and the default handler. Warnings objects have, at a minimum, a `code` and a `message` property, allowing you to control how different kinds of warnings are handled. Other properties are added depending on the type of warning.

Expand Down
7 changes: 2 additions & 5 deletions src/utils/mergeOptions.ts
Expand Up @@ -58,12 +58,9 @@ const defaultOnWarn: WarningHandler = warning => {

const getOnWarn = (
config: GenericConfigObject,
command: CommandConfigObject,
defaultOnWarnHandler: WarningHandler = defaultOnWarn
): WarningHandler =>
command.silent
? () => {}
: config.onwarn
config.onwarn
? warning => (config.onwarn as WarningHandlerWithDefault)(warning, defaultOnWarnHandler)
: defaultOnWarnHandler;

Expand Down Expand Up @@ -225,7 +222,7 @@ function getInputOptions(
input: getOption('input', []),
manualChunks: getOption('manualChunks'),
moduleContext: config.moduleContext as any,
onwarn: getOnWarn(config, command, defaultOnWarnHandler),
onwarn: getOnWarn(config, defaultOnWarnHandler),
perf: getOption('perf', false),
plugins: config.plugins as any,
preserveModules: getOption('preserveModules'),
Expand Down
10 changes: 10 additions & 0 deletions test/cli/samples/silent-onwarn/_config.js
@@ -0,0 +1,10 @@
const assert = require('assert');

module.exports = {
description: 'triggers onwarn with --silent',
command: 'rollup -c --silent',
stderr: stderr => {
assert.equal(stderr, '');
return true;
}
};
5 changes: 5 additions & 0 deletions test/cli/samples/silent-onwarn/_expected.js
@@ -0,0 +1,5 @@
var doIt = () => console.log('main');

doIt();

export default doIt;
5 changes: 5 additions & 0 deletions test/cli/samples/silent-onwarn/main.js
@@ -0,0 +1,5 @@
import doIt from './main.js';

export default () => console.log('main');

doIt();
20 changes: 20 additions & 0 deletions test/cli/samples/silent-onwarn/rollup.config.js
@@ -0,0 +1,20 @@
import assert from 'assert';

const warnings = [];

export default {
input: 'main.js',
output: {
format: 'esm'
},
onwarn(warning) {
warnings.push(warning);
},
plugins: {
generateBundle(bundle) {
assert.strictEqual(warnings.length, 1);
assert.strictEqual(warnings[0].code, 'CIRCULAR_DEPENDENCY');
}
}

}

0 comments on commit b1afb6e

Please sign in to comment.