Skip to content

Commit 0d71338

Browse files
authored
Restructuring with linting and coverage (#17)
* Update stylelint dependency * Fix renamed rules in stylelint v7 * Add editorconfig * Simplify linter * Use chai array length assertion * Restructure with linting and coverage * Add some documentation for runCompilation and add missing use strict * Keep stylelint v7 in peerDependencies
1 parent a47b9cb commit 0d71338

File tree

34 files changed

+211
-139
lines changed

34 files changed

+211
-139
lines changed

.editorconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
root = true
2+
3+
[*]
4+
5+
# Change these settings to your own preference
6+
indent_style = space
7+
indent_size = 2
8+
9+
# We recommend you to keep these unchanged
10+
end_of_line = lf
11+
charset = utf-8
12+
trim_trailing_whitespace = true
13+
insert_final_newline = true
14+
15+
[*.md]
16+
insert_final_newline = false

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules
2-
npm-debug.log
2+
npm-debug.log
3+
.nyc_output
4+
coverage

.travis.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
sudo: false
12
language: node_js
23
node_js:
3-
- '4'
4+
- 4
5+
- "stable"
6+
7+
after_success: "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ $ npm install stylelint-webpack-plugin
1818
In your webpack configuration
1919

2020
```js
21-
var styleLintPlugin = require('stylelint-webpack-plugin');
21+
var StyleLintPlugin = require('stylelint-webpack-plugin');
2222

2323

2424
module.exports = {
2525
// ...
2626
plugins: [
27-
new styleLintPlugin(),
27+
new StyleLintPlugin(),
2828
],
2929
// ...
3030
}
@@ -47,7 +47,7 @@ See [stylelint options](http://stylelint.io/user-guide/node-api/#options), for t
4747
// Default settings
4848
module.exports = {
4949
plugins: [
50-
new styleLintPlugin({
50+
new StyleLintPlugin({
5151
configFile: '.stylelintrc',
5252
context: 'inherits from webpack',
5353
files: '**/*.s?(a|c)ss',

index.js

Lines changed: 23 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,44 @@
1+
'use strict';
2+
13
// Dependencies
2-
var loaderUtils = require('loader-utils');
3-
var assign = require('object-assign');
44
var path = require('path');
5+
var assign = require('object-assign');
56
var formatter = require('stylelint/dist/formatters/stringFormatter').default;
6-
var chalk = require('chalk');
7+
var arrify = require('arrify');
78

89
// Modules
9-
var linter = require('./lib/linter');
10+
var runCompilation = require('./lib/run-compilation');
1011

1112
function apply(options, compiler) {
1213
var context = options.context || compiler.context;
13-
var errors = [];
1414

1515
options = Object.assign({}, options, {
1616
// TODO: make it work with arrays
17-
files: options.files.map(function(file) {
17+
files: options.files.map(function (file) {
1818
return path.join(context, '/', file);
1919
})
2020
});
2121

22-
function runCompilation(compilation, done) {
23-
linter(options).then(function(lint) {
24-
if (lint.errored) {
25-
errors = lint.results.filter(function(f) {
26-
return f.errored;
27-
}).map(function(f) {
28-
return f.source; // send error instead
29-
});
30-
31-
(options.quiet !== true) && console.log(chalk.yellow(options.formatter(lint.results)));
32-
}
33-
34-
if (options.failOnError && errors.length) {
35-
done(new Error('Failed because of a stylelint error.\n'));
36-
} else {
37-
done();
38-
}
39-
}).catch(function(e) {
40-
if (options.failOnError && errors.length) {
41-
done(new Error('Failed because of a stylelint error.\n'));
42-
} else {
43-
done();
44-
}
45-
console.log(chalk.red(e));
46-
});
47-
48-
compilation.plugin && compilation.plugin('compilation', function(compilation) {
49-
errors.forEach(function(err) {
50-
compilation.errors.push(err);
51-
});
52-
});
53-
}
54-
55-
compiler.plugin('run', runCompilation);
56-
compiler.plugin('watch-run', runCompilation);
22+
compiler.plugin('run', runCompilation.bind(this, options));
23+
compiler.plugin('watch-run', runCompilation.bind(this, options));
5724
}
5825

5926
// makes it easier to pass and check options to the plugin thank you webpack doc
6027
// [https://webpack.github.io/docs/plugins.html#the-compiler-instance]
61-
module.exports = function(options) {
62-
options = options || {};
63-
// Default Glob is any directory level of scss and/or sass file,
64-
// under webpack's context and specificity changed via globbing patterns
65-
options.files = options.files || '**/*.s?(c|a)ss';
66-
!Array.isArray(options.files) && (options.files = [options.files]);
67-
options.configFile = options.configFile || '.stylelintrc';
68-
options.formatter = options.formatter || formatter;
69-
options.quiet = options.quiet || false;
70-
28+
module.exports = function (options) {
7129
return {
72-
apply: apply.bind(this, options)
30+
apply: apply.bind(this, buildOptions(options))
7331
};
74-
};
32+
};
33+
34+
function buildOptions(options) {
35+
return assign({
36+
configFile: '.stylelintrc',
37+
formatter: formatter,
38+
quiet: false
39+
}, options, {
40+
// Default Glob is any directory level of scss and/or sass file,
41+
// under webpack's context and specificity changed via globbing patterns
42+
files: arrify(options.files || '**/*.s?(c|a)ss')
43+
});
44+
}

lib/linter.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1+
'use strict';
2+
13
// Dependencies
24
var stylelint = require('stylelint');
35

46
function lint(options) {
5-
return new Promise(function(resolve, reject) {
6-
stylelint.lint(options).then(function(data) {
7-
resolve(data);
8-
}).catch(function(e) {
9-
reject(e);
10-
});
11-
});
7+
return stylelint.lint(options);
128
}
139

14-
module.exports = lint;
10+
module.exports = lint;

lib/run-compilation.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
var chalk = require('chalk');
4+
var linter = require('./linter');
5+
6+
/**
7+
* Function bound to the plugin `apply` method to run the linter and report any
8+
* errors (and their source file locations)
9+
* @param options - from the apply method, the options passed in
10+
* @param compilation - the compiler object
11+
* @param done - callback
12+
*/
13+
module.exports = function runCompilation(options, compilation, done) {
14+
var errors = [];
15+
16+
linter(options)
17+
.then(function (lint) {
18+
if (lint.errored) {
19+
errors = lint.results
20+
.filter(function (f) {
21+
return f.errored;
22+
})
23+
.map(function (f) {
24+
return f.source; // send error instead
25+
});
26+
27+
if (!options.quiet) {
28+
console.log(chalk.yellow(options.formatter(lint.results)));
29+
}
30+
}
31+
32+
if (options.failOnError && errors.length) {
33+
done(new Error('Failed because of a stylelint error.\n'));
34+
} else {
35+
done();
36+
}
37+
})
38+
.catch(function (err) {
39+
if (options.failOnError && errors.length) {
40+
done(new Error('Failed because of a stylelint error.\n'));
41+
} else {
42+
done();
43+
}
44+
console.log(chalk.red(err));
45+
});
46+
47+
// eslint-disable-next-line no-unused-expressions
48+
compilation.plugin && compilation.plugin('compilation', function (compilation) {
49+
errors.forEach(function (err) {
50+
compilation.errors.push(err);
51+
});
52+
});
53+
};

package.json

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,48 @@
2222
"url": "https://github.com/vieron/stylelint-webpack-plugin/issues"
2323
},
2424
"homepage": "https://github.com/vieron/stylelint-webpack-plugin#readme",
25+
"peerDependencies": {
26+
"stylelint": "^7.0.1"
27+
},
2528
"dependencies": {
29+
"arrify": "^1.0.1",
2630
"chalk": "^1.1.1",
2731
"extract-text-webpack-plugin": "^1.0.1",
2832
"loader-utils": "~0.2.10",
2933
"object-assign": "~4.0.1",
30-
"stylelint": "^6.0.1",
34+
"stylelint": "^7.0.1",
3135
"webpack": "^1.12.10"
3236
},
3337
"devDependencies": {
3438
"chai": "^3.4.1",
3539
"chai-as-promised": "^5.2.0",
40+
"coveralls": "^2.11.12",
3641
"memory-fs": "^0.3.0",
37-
"mocha": "^2.3.4"
38-
},
39-
"peerDependencies": {
40-
"stylelint": "^6.0.1"
42+
"mocha": "^2.3.4",
43+
"nyc": "^7.1.0",
44+
"xo": "^0.16.0"
4145
},
4246
"scripts": {
43-
"test": "mocha --harmony --full-trace --check-leaks",
47+
"pretest": "xo",
48+
"test": "nyc mocha",
4449
"preversion": "npm run test",
4550
"version": "git add -A ."
51+
},
52+
"nyc": {
53+
"reporter": [
54+
"lcov",
55+
"text-summary"
56+
]
57+
},
58+
"xo": {
59+
"space": true,
60+
"envs": [
61+
"node",
62+
"mocha"
63+
],
64+
"globals": [
65+
"getPath",
66+
"expect"
67+
]
4668
}
4769
}

test/.stylelintrc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,32 @@
3030
"declaration-colon-newline-after": "always-multi-line",
3131
"declaration-colon-space-after": "always-single-line",
3232
"declaration-colon-space-before": "never",
33-
"font-family-name-quotes": "double-where-recommended",
33+
"font-family-name-quotes": "always-unless-keyword",
3434
"function-calc-no-unspaced-operator": true,
3535
"function-comma-newline-after": "always-multi-line",
3636
"function-comma-space-after": "always-single-line",
3737
"function-comma-space-before": "never",
3838
"function-linear-gradient-no-nonstandard-direction": true,
3939
"function-parentheses-newline-inside": "always-multi-line",
4040
"function-parentheses-space-inside": "never-single-line",
41-
"function-url-quotes": "double",
4241
"function-whitespace-after": "always",
4342
"indentation": 2,
43+
"length-zero-no-unit": true,
4444
"max-empty-lines": 1,
4545
"media-feature-colon-space-after": "always",
4646
"media-feature-colon-space-before": "never",
4747
"media-feature-no-missing-punctuation": true,
48+
"media-feature-parentheses-space-inside": "never",
4849
"media-feature-range-operator-space-after": "always",
4950
"media-feature-range-operator-space-before": "always",
5051
"media-query-list-comma-newline-after": "always-multi-line",
5152
"media-query-list-comma-space-after": "always-single-line",
5253
"media-query-list-comma-space-before": "never",
53-
"media-query-parentheses-space-inside": "never",
5454
"no-eol-whitespace": true,
5555
"no-invalid-double-slash-comments": true,
56-
"no-missing-eof-newline": true,
56+
"no-missing-end-of-source-newline": true,
5757
"number-leading-zero": "always",
5858
"number-no-trailing-zeros": true,
59-
"number-zero-length-no-unit": true,
6059
"rule-non-nested-empty-line-before": [ "always-multi-line", {
6160
"ignore": ["after-comment"],
6261
} ],
@@ -71,4 +70,4 @@
7170
"value-list-comma-space-after": "always-single-line",
7271
"value-list-comma-space-before": "never",
7372
},
74-
}
73+
}

test/fixtures/test1/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require(getPath('./../../../node_modules/file-loader/index') + '!./test.scss');
2+
3+
console.log('test1');

0 commit comments

Comments
 (0)