Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for watch mode of create-react-app #43

Merged
merged 10 commits into from
Dec 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: chai's .to.contain matchers work on arrays so since you're asserting on the length above I think you can remove the index

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, that doesn't work, since that's not the full content of the string:

1) stylelint-webpack-plugin works with multiple source files:
     AssertionError: expected [ Array(1) ] to include 'test/fixtures/test7/_second.scss'
      at ~/projects/stylelint-webpack-plugin/test/index.js:74:45

});
});

Expand Down