Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down Expand Up @@ -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 } }
]
}
```


<h2 align="center">Examples</h2>

### `Stylelint`
Expand Down
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -40,6 +41,7 @@ function loader (css, map, meta) {
case 'ident':
case 'config':
case 'sourceMap':
case 'emitWarningsAsErrors':
return
default:
return option
Expand Down Expand Up @@ -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) => {
Expand Down
6 changes: 5 additions & 1 deletion src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
},
"sourceMap": {
"type": [ "string", "boolean" ]
},
"emitWarningsAsErrors": {
"type": ["boolean"]
}
},
"errorMessage": {
Expand All @@ -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
Expand Down
25 changes: 25 additions & 0 deletions test/Warnings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
})
6 changes: 6 additions & 0 deletions test/__snapshots__/Warnings.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Warnings Emit as Error 1`] = `
"Warning

(1:5) <Message>"
`;

exports[`Warnings Plugins 1`] = `
"Warning

Expand Down