diff --git a/index.js b/index.js index d24365a..1faaaf1 100644 --- a/index.js +++ b/index.js @@ -29,7 +29,9 @@ function apply(options, compiler) { var runner = runCompilation.bind(this, options); compiler.plugin('run', runner); - compiler.plugin('watch-run', runner); + compiler.plugin('watch-run', function onWatchRun(watcher, callback) { + runner(watcher.compiler, callback); + }); } /** diff --git a/lib/run-compilation.js b/lib/run-compilation.js index 91eccbd..40858db 100644 --- a/lib/run-compilation.js +++ b/lib/run-compilation.js @@ -7,28 +7,22 @@ var linter = require('./linter'); * Function bound to the plugin `apply` method to run the linter and report any * errors (and their source file locations) * @param options - from the apply method, the options passed in - * @param compilation - the compiler object + * @param compiler - the compiler object * @param done - webpack callback */ -module.exports = function runCompilation(options, compilation, done) { +module.exports = function runCompilation(options, compiler, done) { var errors = []; var warnings = []; linter(options) - .then(function (lint) { - warnings = lint.results - .filter(function (f) { - return f.warnings && f.warnings.length; - }); - errors = lint.results - .filter(function (f) { - return f.errored; - }).map(function (f) { - return f.source; // send error instead - }); - if (!options.quiet) { - console.log(chalk.yellow(options.formatter(lint.results))); - } + .then(function linterSuccess(lint) { + warnings = lint.results.filter(function (f) { + return f.warnings && f.warnings.length; + }); + + errors = lint.results.filter(function (f) { + return f.errored; + }); if (options.failOnError && errors.length) { done(new Error('Failed because of a stylelint error.\n')); @@ -36,7 +30,7 @@ module.exports = function runCompilation(options, compilation, done) { done(); } }) - .catch(function (err) { + .catch(function linterError(err) { if (options.failOnError && errors.length) { done(new Error('Failed because of a stylelint error.\n')); } else { @@ -45,9 +39,15 @@ module.exports = function runCompilation(options, compilation, done) { console.log(chalk.red(err)); }); - // eslint-disable-next-line no-unused-expressions - compilation.plugin && compilation.plugin('compilation', function (compilation) { - compilation.errors = compilation.errors.concat(errors); - compilation.warnings = compilation.warnings.concat(warnings); + compiler.plugin('compilation', function onCompilation(compilation) { + if (warnings.length) { + compilation.warnings.push(chalk.yellow(options.formatter(warnings))); + warnings = []; + } + + if (errors.length) { + compilation.errors.push(chalk.red(options.formatter(errors))); + errors = []; + } }); }; diff --git a/test/index.js b/test/index.js index b338f20..402cbaa 100644 --- a/test/index.js +++ b/test/index.js @@ -70,7 +70,9 @@ describe('stylelint-webpack-plugin', function () { return pack(assign({}, baseConfig, config)) .then(function (stats) { - expect(stats.compilation.errors).to.have.length(2); + expect(stats.compilation.errors).to.have.length(1); + expect(stats.compilation.errors[0]).to.contain('test/fixtures/test7/_second.scss'); + expect(stats.compilation.errors[0]).to.contain('test/fixtures/test7/test.scss'); }); });