/
run-compilation.js
62 lines (51 loc) · 1.59 KB
/
run-compilation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
var R = require('ramda');
var linter = require('./linter');
var errorMessage = require('./constants').errorMessage;
/**
* 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 compiler - the compiler object
* @param done - webpack callback
*/
module.exports = function runCompilation (options, compiler, done) {
var errors = [];
var warnings = [];
linter(options)
.then(function linterSuccess (lint) {
var results = lint.results;
if (options.emitErrors === false) {
warnings = results.filter(R.either(fileHasErrors, fileHasWarnings));
} else {
warnings = results.filter(R.both(R.complement(fileHasErrors), fileHasWarnings));
errors = results.filter(fileHasErrors);
}
if (options.quiet === false) {
console.warn(options.formatter(results));
}
if (options.failOnError && errors.length) {
done(new Error(errorMessage));
} else {
done();
}
})
.catch(done);
compiler.plugin('after-emit', function afterEmit (compilation, callback) {
if (warnings.length) {
compilation.warnings.push(new Error(options.formatter(warnings)));
warnings = [];
}
if (errors.length) {
compilation.errors.push(new Error(options.formatter(errors)));
errors = [];
}
callback();
});
};
function fileHasErrors (file) {
return file.errored;
}
function fileHasWarnings (file) {
return file.warnings && file.warnings.length;
}