From f7d1111505ca4bf21f53e1d0b7e0bed19cd38b04 Mon Sep 17 00:00:00 2001 From: Vincent Tunru Date: Mon, 30 Jan 2017 21:37:05 +0100 Subject: [PATCH] Fixes #65: Add `emitErrors` option This option allows you to log errors as warnings, preventing Webpack dev server from aborting compilation. --- README.md | 1 + lib/run-compilation.js | 6 ++++- test/index.test.js | 59 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6824ba5..f483b4a 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ See [stylelint options](http://stylelint.io/user-guide/node-api/#options) for th * `configFile`: You can change the config file location. Default: (`undefined`), handled by [stylelint's cosmiconfig module](http://stylelint.io/user-guide/configuration/). * `context`: String indicating the root of your SCSS files. Default: inherits from webpack config. * `failOnError`: Have Webpack's build process die on error. Default: `false` +* `emitErrors`: Display Stylelint errors as actual errors, rather than just warnings. Default: `true` * `files`: Change the glob pattern for finding files. Default: (`['**/*.s?(a|c)ss']`) * `formatter`: Use a custom formatter to print errors to the console. Default: (`require('stylelint').formatters.string`) * `lintDirtyModulesOnly`: Lint only changed files, skip lint on start. Default (`false`) diff --git a/lib/run-compilation.js b/lib/run-compilation.js index 075a9a9..c1c4e3c 100644 --- a/lib/run-compilation.js +++ b/lib/run-compilation.js @@ -46,7 +46,11 @@ module.exports = function runCompilation (options, compiler, done) { } if (errors.length) { - compilation.errors.push(chalk.red(options.formatter(errors))); + if (options.emitErrors === false) { + compilation.warnings.push(chalk.yellow(options.formatter(errors))); + } else { + compilation.errors.push(chalk.red(options.formatter(errors))); + } errors = []; } diff --git a/test/index.test.js b/test/index.test.js index 20447cc..a0cbe85 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -57,6 +57,65 @@ describe('stylelint-webpack-plugin', function () { }); }); + it('emits errors as warnings when asked to', function () { + var config = { + context: './test/fixtures/single-error', + entry: './index', + plugins: [ + new StyleLintPlugin({ + configFile: configFilePath, + quiet: true, + emitErrors: false + }) + ] + }; + + return pack(assign({}, baseConfig, config)) + .then(function (stats) { + expect(stats.compilation.errors).to.have.length(0); + expect(stats.compilation.warnings).to.have.length(1); + }); + }); + + it('does not emit errors as warnings when asked not to', function () { + var config = { + context: './test/fixtures/single-error', + entry: './index', + plugins: [ + new StyleLintPlugin({ + configFile: configFilePath, + quiet: true, + emitErrors: true + }) + ] + }; + + return pack(assign({}, baseConfig, config)) + .then(function (stats) { + expect(stats.compilation.errors).to.have.length(1); + expect(stats.compilation.warnings).to.have.length(0); + }); + }); + + it('does not emit errors as warnings when not asked to', function () { + var config = { + context: './test/fixtures/single-error', + entry: './index', + plugins: [ + new StyleLintPlugin({ + configFile: configFilePath, + quiet: true + }) + ] + }; + + return pack(assign({}, baseConfig, config)) + .then(function (stats) { + expect(stats.compilation.errors).to.have.length(1); + expect(stats.compilation.warnings).to.have.length(0); + }); + }); + it('works with multiple source files', function () { var config = { context: './test/fixtures/multiple-sources',