Skip to content

Commit

Permalink
fix(index): emit warnings as an instance of {Error}
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-ciniawsky committed Aug 8, 2018
1 parent 2c6033b commit 8ac6fb5
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/Warning.js
@@ -0,0 +1,14 @@
class Warning extends Error {
constructor (warning) {
super()

const { line, column, text } = warning

this.name = 'LoaderWarning'
this.message = `\n(${line}:${column}) ${text}\n`

this.stack = false
}
}

module.exports = Warning
8 changes: 6 additions & 2 deletions src/index.js
Expand Up @@ -10,7 +10,9 @@ const validateOptions = require('schema-utils')
const postcss = require('postcss')
const postcssrc = require('postcss-load-config')

const SyntaxError = require('./Error')
const Warning = require('./Warning.js')
const SyntaxError = require('./Error.js')
const parseOptions = require('./options.js')

/**
* PostCSS Loader
Expand Down Expand Up @@ -143,7 +145,9 @@ module.exports = function loader (css, map, meta) {
return postcss(plugins)
.process(css, options)
.then((result) => {
result.warnings().forEach((msg) => this.emitWarning(msg.toString()))
result.warnings().forEach((warning) => {
this.emitWarning(new Warning(warning))
})

result.messages.forEach((msg) => {
if (msg.type === 'dependency') this.addDependency(msg.file)
Expand Down
33 changes: 33 additions & 0 deletions test/Warnings.test.js
@@ -0,0 +1,33 @@
const { webpack } = require('@webpack-utilities/test')

const plugin = (options = {}) => (css, result) => {
css.walkDecls((node) => {
node.warn(result, '<Message>')
})
}

describe('Warnings', () => {
test('Plugins', () => {
const config = {
loader: {
test: /\.css$/,
options: {
plugins: [
plugin()
]
}
}
}

return webpack('css/index.js', config).then((stats) => {
const warning = stats.compilation.warnings[0]

const message = warning.message
.split('\n')
.slice(1)
.join('\n')

expect(message).toMatchSnapshot()
})
})
})
7 changes: 7 additions & 0 deletions test/__snapshots__/Warnings.test.js.snap
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Warnings Plugins 1`] = `
"
(1:5) <Message>
"
`;

0 comments on commit 8ac6fb5

Please sign in to comment.