Skip to content

Commit b1afb6e

Browse files
authored
Do not skip onwarn handler when --silent is used (#2981)
* Do not skip onwarn with --silent * Update documentation
1 parent ef7486d commit b1afb6e

File tree

9 files changed

+51
-13
lines changed

9 files changed

+51
-13
lines changed

bin/src/run/batchWarnings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ const deferredHandlers: {
253253
let lastUrl: string;
254254

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

bin/src/run/build.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,14 @@ export default function build(
7777
);
7878
})
7979
.then((bundle?: RollupBuild) => {
80-
warnings.flush();
81-
if (!silent)
80+
if (!silent) {
81+
warnings.flush();
8282
stderr(
8383
tc.green(`created ${tc.bold(files.join(', '))} in ${tc.bold(ms(Date.now() - start))}`)
8484
);
85-
if (bundle && bundle.getTimings) {
86-
printTimings(bundle.getTimings());
85+
if (bundle && bundle.getTimings) {
86+
printTimings(bundle.getTimings());
87+
}
8788
}
8889
})
8990
.catch((err: any) => {

docs/01-command-line-reference.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ $ rollup --config
165165
$ rollup --config my.config.js
166166
```
167167

168-
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`:
168+
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`:
169169

170170
```javascript
171171
// rollup.config.js
@@ -254,7 +254,7 @@ Rebuild the bundle when its source files change on disk.
254254

255255
#### `--silent`
256256

257-
Don't print warnings to the console.
257+
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).
258258

259259
#### `--environment <values>`
260260

docs/999-big-list-of-options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ Be aware that manual chunks can change the behaviour of the application if side-
317317
#### onwarn
318318
Type: `(warning: RollupWarning, defaultHandler: (warning: string | RollupWarning) => void) => void;`
319319

320-
A function that will intercept warning messages. If not supplied, warnings will be deduplicated and printed to the console.
320+
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.
321321

322322
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.
323323

src/utils/mergeOptions.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,9 @@ const defaultOnWarn: WarningHandler = warning => {
5858

5959
const getOnWarn = (
6060
config: GenericConfigObject,
61-
command: CommandConfigObject,
6261
defaultOnWarnHandler: WarningHandler = defaultOnWarn
6362
): WarningHandler =>
64-
command.silent
65-
? () => {}
66-
: config.onwarn
63+
config.onwarn
6764
? warning => (config.onwarn as WarningHandlerWithDefault)(warning, defaultOnWarnHandler)
6865
: defaultOnWarnHandler;
6966

@@ -225,7 +222,7 @@ function getInputOptions(
225222
input: getOption('input', []),
226223
manualChunks: getOption('manualChunks'),
227224
moduleContext: config.moduleContext as any,
228-
onwarn: getOnWarn(config, command, defaultOnWarnHandler),
225+
onwarn: getOnWarn(config, defaultOnWarnHandler),
229226
perf: getOption('perf', false),
230227
plugins: config.plugins as any,
231228
preserveModules: getOption('preserveModules'),
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const assert = require('assert');
2+
3+
module.exports = {
4+
description: 'triggers onwarn with --silent',
5+
command: 'rollup -c --silent',
6+
stderr: stderr => {
7+
assert.equal(stderr, '');
8+
return true;
9+
}
10+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var doIt = () => console.log('main');
2+
3+
doIt();
4+
5+
export default doIt;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import doIt from './main.js';
2+
3+
export default () => console.log('main');
4+
5+
doIt();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import assert from 'assert';
2+
3+
const warnings = [];
4+
5+
export default {
6+
input: 'main.js',
7+
output: {
8+
format: 'esm'
9+
},
10+
onwarn(warning) {
11+
warnings.push(warning);
12+
},
13+
plugins: {
14+
generateBundle(bundle) {
15+
assert.strictEqual(warnings.length, 1);
16+
assert.strictEqual(warnings[0].code, 'CIRCULAR_DEPENDENCY');
17+
}
18+
}
19+
20+
}

0 commit comments

Comments
 (0)