-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
90 lines (75 loc) · 2.17 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Dependencies
var csslint = require("csslint").CSSLint,
loaderUtils = require("loader-utils"),
assign = require("object-assign");
// Helper utilities
var formatter = require('./lib/formatter'),
getConfig = require('./lib/getConfig');
// extract-text-plugin runs through the files again
// adding the linted files to array to prevent duplication of linting
var lintedFiles = [];
// Saving default csslint rules
var ruleSet = csslint.getRuleset();
/**
* Css Linter
*
* @param {String|Buffer} input CSS string
* @param {Object} options csslint configuration
* @param {Object} webpack webpack instance
* @param {Function} callback optional callback for async loader
* @return {void}
*/
function lint(input, options, webpack, callback) {
var rules = getConfig(options);
// Override ruleSet with config file
if (rules) {
assign(ruleSet, rules);
}
// Run csslint
var report = csslint.verify(input, ruleSet);
// Run the formatter if there are errors/warnings and is not already linted
if (report.messages.length && lintedFiles.indexOf(webpack.resourcePath) === -1) {
// Add file to list of linted files
lintedFiles.push(webpack.resourcePath);
var output = formatter(report.messages, webpack.resourcePath);
if (output.errors || (options.failOnWarning == true && output.warnings)) {
webpack.emitError(output.message);
if (options.failOnError) {
throw new Error("Module failed because of a csslint error.");
}
} else if (output.warnings) {
webpack.emitWarning(output.message);
}
}
if (callback) {
callback(null, input)
}
}
/**
* Webpack Loader
*
* @param {String|Buffer} input JavaScript string
* @returns {String|Buffer} original input
*/
module.exports = function(input) {
var options = assign(
{
configFile: './.csslintrc',
failOnError: true,
failOnWarning: true
},
loaderUtils.parseQuery(this.query)
);
this.cacheable();
var callback = this.async();
if (!callback) { // sync
lint(input, options, this);
return input;
} else { // async
try {
lint(input, options, this, callback);
} catch(e) {
callback(e);
}
}
}