From b1afb6ea0816b444c33d63a8d187aeccda3207d9 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Thu, 4 Jul 2019 16:34:10 +0200 Subject: [PATCH] Do not skip onwarn handler when --silent is used (#2981) * Do not skip onwarn with --silent * Update documentation --- bin/src/run/batchWarnings.ts | 2 +- bin/src/run/build.ts | 9 +++++---- docs/01-command-line-reference.md | 4 ++-- docs/999-big-list-of-options.md | 2 +- src/utils/mergeOptions.ts | 7 ++----- test/cli/samples/silent-onwarn/_config.js | 10 ++++++++++ test/cli/samples/silent-onwarn/_expected.js | 5 +++++ test/cli/samples/silent-onwarn/main.js | 5 +++++ .../samples/silent-onwarn/rollup.config.js | 20 +++++++++++++++++++ 9 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 test/cli/samples/silent-onwarn/_config.js create mode 100644 test/cli/samples/silent-onwarn/_expected.js create mode 100644 test/cli/samples/silent-onwarn/main.js create mode 100644 test/cli/samples/silent-onwarn/rollup.config.js diff --git a/bin/src/run/batchWarnings.ts b/bin/src/run/batchWarnings.ts index 7fa1d5dd74a..463ec8e69d0 100644 --- a/bin/src/run/batchWarnings.ts +++ b/bin/src/run/batchWarnings.ts @@ -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)); diff --git a/bin/src/run/build.ts b/bin/src/run/build.ts index 73fb65a2e2f..274194c909f 100644 --- a/bin/src/run/build.ts +++ b/bin/src/run/build.ts @@ -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) => { diff --git a/docs/01-command-line-reference.md b/docs/01-command-line-reference.md index 40dea3ad64c..e3a50604e7c 100755 --- a/docs/01-command-line-reference.md +++ b/docs/01-command-line-reference.md @@ -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 @@ -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 ` diff --git a/docs/999-big-list-of-options.md b/docs/999-big-list-of-options.md index fcbb5273c65..8649dcac4a4 100755 --- a/docs/999-big-list-of-options.md +++ b/docs/999-big-list-of-options.md @@ -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. diff --git a/src/utils/mergeOptions.ts b/src/utils/mergeOptions.ts index 873fe30fb72..5456aede10a 100644 --- a/src/utils/mergeOptions.ts +++ b/src/utils/mergeOptions.ts @@ -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; @@ -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'), diff --git a/test/cli/samples/silent-onwarn/_config.js b/test/cli/samples/silent-onwarn/_config.js new file mode 100644 index 00000000000..0bbc667c79a --- /dev/null +++ b/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; + } +}; diff --git a/test/cli/samples/silent-onwarn/_expected.js b/test/cli/samples/silent-onwarn/_expected.js new file mode 100644 index 00000000000..29af3b17871 --- /dev/null +++ b/test/cli/samples/silent-onwarn/_expected.js @@ -0,0 +1,5 @@ +var doIt = () => console.log('main'); + +doIt(); + +export default doIt; diff --git a/test/cli/samples/silent-onwarn/main.js b/test/cli/samples/silent-onwarn/main.js new file mode 100644 index 00000000000..0be80e4f2dc --- /dev/null +++ b/test/cli/samples/silent-onwarn/main.js @@ -0,0 +1,5 @@ +import doIt from './main.js'; + +export default () => console.log('main'); + +doIt(); diff --git a/test/cli/samples/silent-onwarn/rollup.config.js b/test/cli/samples/silent-onwarn/rollup.config.js new file mode 100644 index 00000000000..8a1950bb762 --- /dev/null +++ b/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'); + } + } + +}