Skip to content

Commit 8ac6fb5

Browse files
committedAug 8, 2018
fix(index): emit warnings as an instance of {Error}
1 parent 2c6033b commit 8ac6fb5

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed
 

‎src/Warning.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Warning extends Error {
2+
constructor (warning) {
3+
super()
4+
5+
const { line, column, text } = warning
6+
7+
this.name = 'LoaderWarning'
8+
this.message = `\n(${line}:${column}) ${text}\n`
9+
10+
this.stack = false
11+
}
12+
}
13+
14+
module.exports = Warning

‎src/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ const validateOptions = require('schema-utils')
1010
const postcss = require('postcss')
1111
const postcssrc = require('postcss-load-config')
1212

13-
const SyntaxError = require('./Error')
13+
const Warning = require('./Warning.js')
14+
const SyntaxError = require('./Error.js')
15+
const parseOptions = require('./options.js')
1416

1517
/**
1618
* PostCSS Loader
@@ -143,7 +145,9 @@ module.exports = function loader (css, map, meta) {
143145
return postcss(plugins)
144146
.process(css, options)
145147
.then((result) => {
146-
result.warnings().forEach((msg) => this.emitWarning(msg.toString()))
148+
result.warnings().forEach((warning) => {
149+
this.emitWarning(new Warning(warning))
150+
})
147151

148152
result.messages.forEach((msg) => {
149153
if (msg.type === 'dependency') this.addDependency(msg.file)

‎test/Warnings.test.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const { webpack } = require('@webpack-utilities/test')
2+
3+
const plugin = (options = {}) => (css, result) => {
4+
css.walkDecls((node) => {
5+
node.warn(result, '<Message>')
6+
})
7+
}
8+
9+
describe('Warnings', () => {
10+
test('Plugins', () => {
11+
const config = {
12+
loader: {
13+
test: /\.css$/,
14+
options: {
15+
plugins: [
16+
plugin()
17+
]
18+
}
19+
}
20+
}
21+
22+
return webpack('css/index.js', config).then((stats) => {
23+
const warning = stats.compilation.warnings[0]
24+
25+
const message = warning.message
26+
.split('\n')
27+
.slice(1)
28+
.join('\n')
29+
30+
expect(message).toMatchSnapshot()
31+
})
32+
})
33+
})
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Warnings Plugins 1`] = `
4+
"
5+
(1:5) <Message>
6+
"
7+
`;

0 commit comments

Comments
 (0)
Please sign in to comment.