From c93a334f48d8d03165d6c0f39ca46f6259b8cb96 Mon Sep 17 00:00:00 2001 From: Edwin Zhang Date: Sat, 15 Dec 2018 12:17:39 -0800 Subject: [PATCH 1/4] src/options.json: add emitWarningsAsErrors option for validation --- src/options.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/options.json b/src/options.json index 1141d84a..668a5a95 100644 --- a/src/options.json +++ b/src/options.json @@ -40,6 +40,9 @@ }, "sourceMap": { "type": [ "string", "boolean" ] + }, + "emitWarningsAsErrors": { + "type": ["boolean"] } }, "errorMessage": { @@ -50,7 +53,8 @@ "syntax": "should be {String|Object} (https://github.com/postcss/postcss-loader#syntax)", "stringifier": "should be {String|Object} (https://github.com/postcss/postcss-loader#stringifier)", "plugins": "should be {Array|Object|Function} (https://github.com/postcss/postcss-loader#plugins)", - "sourceMap": "should be {String|Boolean} (https://github.com/postcss/postcss-loader#sourcemap)" + "sourceMap": "should be {String|Boolean} (https://github.com/postcss/postcss-loader#sourcemap)", + "emitWarningsAsErrors": "should be {Boolean} (https://github.com/postcss/postcss-loader#emitWarningsAsError)" } }, "additionalProperties": true From 823e596fb3718105f301e9d008fa79fdde787c06 Mon Sep 17 00:00:00 2001 From: Edwin Zhang Date: Sat, 15 Dec 2018 12:18:41 -0800 Subject: [PATCH 2/4] src/index.js: emitError when emitWarningsAsErrors is set --- src/index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index bb576c32..30e09f12 100644 --- a/src/index.js +++ b/src/index.js @@ -31,6 +31,7 @@ function loader (css, map, meta) { const file = this.resourcePath const sourceMap = options.sourceMap + const emitWarningsAsErrors = options.emitWarningsAsErrors Promise.resolve().then(() => { const length = Object.keys(options) @@ -40,6 +41,7 @@ function loader (css, map, meta) { case 'ident': case 'config': case 'sourceMap': + case 'emitWarningsAsErrors': return default: return option @@ -142,8 +144,9 @@ function loader (css, map, meta) { .then((result) => { let { css, map, root, processor, messages } = result - result.warnings().forEach((warning) => { - this.emitWarning(new Warning(warning)) + result.warnings().forEach(warning => { + const emit = emitWarningsAsErrors ? this.emitError : this.emitWarning + emit(new Warning(warning)) }) messages.forEach((msg) => { From 5c960dd08718b7029518aa7aa19e9734cac4975f Mon Sep 17 00:00:00 2001 From: Edwin Zhang Date: Sat, 15 Dec 2018 12:19:20 -0800 Subject: [PATCH 3/4] test/Warnings.test.js: add test for emitting warnings as errors --- test/Warnings.test.js | 25 ++++++++++++++++++++++++ test/__snapshots__/Warnings.test.js.snap | 6 ++++++ 2 files changed, 31 insertions(+) diff --git a/test/Warnings.test.js b/test/Warnings.test.js index 7c5875b7..e7f3cbb4 100644 --- a/test/Warnings.test.js +++ b/test/Warnings.test.js @@ -30,4 +30,29 @@ describe('Warnings', () => { expect(message).toMatchSnapshot() }) }) + + test('Emit as Error', () => { + const config = { + loader: { + test: /\.css$/, + options: { + emitWarningsAsErrors: true, + plugins: [ + plugin() + ] + } + } + } + + return webpack('css/index.js', config).then((stats) => { + const error = stats.compilation.errors[0] + + const message = error.message + .split('\n') + .slice(1) + .join('\n') + + expect(message).toMatchSnapshot() + }) + }) }) diff --git a/test/__snapshots__/Warnings.test.js.snap b/test/__snapshots__/Warnings.test.js.snap index 9524418a..407d63c9 100644 --- a/test/__snapshots__/Warnings.test.js.snap +++ b/test/__snapshots__/Warnings.test.js.snap @@ -1,5 +1,11 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Warnings Emit as Error 1`] = ` +"Warning + +(1:5) " +`; + exports[`Warnings Plugins 1`] = ` "Warning From 634681ed6e44459ef5df95f09c0d0b905b82ef87 Mon Sep 17 00:00:00 2001 From: Edwin Zhang Date: Sat, 15 Dec 2018 12:28:17 -0800 Subject: [PATCH 4/4] README: add emitWarningsAsErrors to the readme --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 6b03ad79..f4ada96d 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,7 @@ module.exports = { |[`config`](#config)|`{Object}`|`undefined`|Set `postcss.config.js` config path && `ctx`| |[`plugins`](#plugins)|`{Array\|Function}`|`[]`|Set PostCSS Plugins| |[`sourceMap`](#sourcemap)|`{String\|Boolean}`|`false`|Enable Source Maps| +|[`emitWarningsAsErrors`](#emitwarningsaserrors)|`{Boolean}`|`false`|Emit warnings as errors| ### `Exec` @@ -305,6 +306,22 @@ within the CSS directly as an annotation comment. /*# sourceMappingURL=data:application/json;base64, ... */ ``` +### `EmitWarningsAsErrors` + +Enabling `emitWarningsAsErrors` will emit any warnings from postcss plugins at the error level. + +**`webpack.config.js`** +```js +{ + test: /\.css$/, + use: [ + ..., + { loader: 'postcss-loader', options: { emitWarningsAsErrors: true } } + ] +} +``` + +

Examples

### `Stylelint`