diff --git a/src/Warning.js b/src/Warning.js new file mode 100644 index 00000000..6c67c7d4 --- /dev/null +++ b/src/Warning.js @@ -0,0 +1,16 @@ +class Warning extends Error { + constructor (warning) { + super() + + console.log(warning) + + const { line, column, text } = warning + + this.name = 'LoaderWarning' + this.message = `\n(${line}:${column}) ${text}\n` + + this.stack = false + } +} + +module.exports = Warning diff --git a/src/index.js b/src/index.js index e7d29f4c..d067a934 100644 --- a/src/index.js +++ b/src/index.js @@ -8,8 +8,9 @@ const { validateOptions } = require('@webpack-utilities/schema') const postcss = require('postcss') const postcssrc = require('postcss-load-config') -const SyntaxError = require('./Error') -const parseOptions = require('./options') +const Warning = require('./Warning.js') +const SyntaxError = require('./Error.js') +const parseOptions = require('./options.js') /** * PostCSS Loader @@ -142,7 +143,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) diff --git a/test/Warnings.test.js b/test/Warnings.test.js new file mode 100644 index 00000000..e9a84f40 --- /dev/null +++ b/test/Warnings.test.js @@ -0,0 +1,35 @@ +const { webpack } = require('@webpack-utilities/test') + +const plugin = (options = {}) => (css, result) => { + css.walkDecls((node) => { + node.warn(result, '') + }) +} + +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() + }) + }) +}) + +process.on('warning', (warning) => console.warn(warning)) diff --git a/test/__snapshots__/Warnings.test.js.snap b/test/__snapshots__/Warnings.test.js.snap new file mode 100644 index 00000000..d1d876e1 --- /dev/null +++ b/test/__snapshots__/Warnings.test.js.snap @@ -0,0 +1,7 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Warnings Plugins 1`] = ` +" +(1:5) +" +`;