Skip to content

Commit

Permalink
fix(watch mode): corrects functions for watch mode (#43)
Browse files Browse the repository at this point in the history
* Pass in compiler to 'watch-run' plugin

* Fix incorrect variable name

* Display formatted warnings and errors

* Fix linting error

* Fix test

* Remove unnecessary eslint configuration

* Name anonymous functions

* correct style nits

* fixes eslint nit

* Fix linting errors
  • Loading branch information
karlhorky authored and JaKXz committed Dec 16, 2016
1 parent dc97d01 commit 996eb21
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

/**
Expand Down
42 changes: 21 additions & 21 deletions lib/run-compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,30 @@ 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'));
} else {
done();
}
})
.catch(function (err) {
.catch(function linterError(err) {
if (options.failOnError && errors.length) {
done(new Error('Failed because of a stylelint error.\n'));
} else {
Expand All @@ -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 = [];
}
});
};
4 changes: 3 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

Expand Down

0 comments on commit 996eb21

Please sign in to comment.