Skip to content

Commit 2bdca8b

Browse files
karlhorkyjoshwiens
authored andcommitted
fix(watch mode): corrects functions for watch mode (#43)
* 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
1 parent e1a5891 commit 2bdca8b

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ function apply(options, compiler) {
2929
var runner = runCompilation.bind(this, options);
3030

3131
compiler.plugin('run', runner);
32-
compiler.plugin('watch-run', runner);
32+
compiler.plugin('watch-run', function onWatchRun(watcher, callback) {
33+
runner(watcher.compiler, callback);
34+
});
3335
}
3436

3537
/**

lib/run-compilation.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,30 @@ var linter = require('./linter');
77
* Function bound to the plugin `apply` method to run the linter and report any
88
* errors (and their source file locations)
99
* @param options - from the apply method, the options passed in
10-
* @param compilation - the compiler object
10+
* @param compiler - the compiler object
1111
* @param done - webpack callback
1212
*/
13-
module.exports = function runCompilation(options, compilation, done) {
13+
module.exports = function runCompilation(options, compiler, done) {
1414
var errors = [];
1515
var warnings = [];
1616

1717
linter(options)
18-
.then(function (lint) {
19-
warnings = lint.results
20-
.filter(function (f) {
21-
return f.warnings && f.warnings.length;
22-
});
23-
errors = lint.results
24-
.filter(function (f) {
25-
return f.errored;
26-
}).map(function (f) {
27-
return f.source; // send error instead
28-
});
29-
if (!options.quiet) {
30-
console.log(chalk.yellow(options.formatter(lint.results)));
31-
}
18+
.then(function linterSuccess(lint) {
19+
warnings = lint.results.filter(function (f) {
20+
return f.warnings && f.warnings.length;
21+
});
22+
23+
errors = lint.results.filter(function (f) {
24+
return f.errored;
25+
});
3226

3327
if (options.failOnError && errors.length) {
3428
done(new Error('Failed because of a stylelint error.\n'));
3529
} else {
3630
done();
3731
}
3832
})
39-
.catch(function (err) {
33+
.catch(function linterError(err) {
4034
if (options.failOnError && errors.length) {
4135
done(new Error('Failed because of a stylelint error.\n'));
4236
} else {
@@ -45,9 +39,15 @@ module.exports = function runCompilation(options, compilation, done) {
4539
console.log(chalk.red(err));
4640
});
4741

48-
// eslint-disable-next-line no-unused-expressions
49-
compilation.plugin && compilation.plugin('compilation', function (compilation) {
50-
compilation.errors = compilation.errors.concat(errors);
51-
compilation.warnings = compilation.warnings.concat(warnings);
42+
compiler.plugin('compilation', function onCompilation(compilation) {
43+
if (warnings.length) {
44+
compilation.warnings.push(chalk.yellow(options.formatter(warnings)));
45+
warnings = [];
46+
}
47+
48+
if (errors.length) {
49+
compilation.errors.push(chalk.red(options.formatter(errors)));
50+
errors = [];
51+
}
5252
});
5353
};

test/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ describe('stylelint-webpack-plugin', function () {
7070

7171
return pack(assign({}, baseConfig, config))
7272
.then(function (stats) {
73-
expect(stats.compilation.errors).to.have.length(2);
73+
expect(stats.compilation.errors).to.have.length(1);
74+
expect(stats.compilation.errors[0]).to.contain('test/fixtures/test7/_second.scss');
75+
expect(stats.compilation.errors[0]).to.contain('test/fixtures/test7/test.scss');
7476
});
7577
});
7678

0 commit comments

Comments
 (0)