/
postcssPlugin.js
41 lines (33 loc) · 999 Bytes
/
postcssPlugin.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
'use strict';
const createStylelint = require('./createStylelint');
const path = require('path');
/** @typedef {import('stylelint').PostcssPluginOptions} PostcssPluginOptions */
/** @typedef {import('stylelint').Config} StylelintConfig */
/**
* @type {import('postcss').PluginCreator<PostcssPluginOptions>}
* */
module.exports = (options = {}) => {
const tailoredOptions = isConfig(options) ? { config: options } : options;
const stylelint = createStylelint(tailoredOptions);
return {
postcssPlugin: 'stylelint',
Once(root, { result }) {
let filePath = root.source && root.source.input.file;
if (filePath && !path.isAbsolute(filePath)) {
filePath = path.join(process.cwd(), filePath);
}
return stylelint._lintSource({
filePath,
existingPostcssResult: result,
});
},
};
};
module.exports.postcss = true;
/**
* @param {PostcssPluginOptions} options
* @returns {options is StylelintConfig}
*/
function isConfig(options) {
return 'rules' in options;
}