UNPKG

1.05 kBJavaScriptView Raw
1'use strict';
2
3const createStylelint = require('./createStylelint');
4const path = require('path');
5
6/** @typedef {import('stylelint').PostcssPluginOptions} PostcssPluginOptions */
7/** @typedef {import('stylelint').Config} StylelintConfig */
8
9/**
10 * @type {import('postcss').PluginCreator<PostcssPluginOptions>}
11 * */
12module.exports = (options = {}) => {
13 const [cwd, tailoredOptions] = isConfig(options)
14 ? [process.cwd(), { config: options }]
15 : [options.cwd || process.cwd(), options];
16 const stylelint = createStylelint(tailoredOptions);
17
18 return {
19 postcssPlugin: 'stylelint',
20 Once(root, { result }) {
21 let filePath = root.source && root.source.input.file;
22
23 if (filePath && !path.isAbsolute(filePath)) {
24 filePath = path.join(cwd, filePath);
25 }
26
27 return stylelint._lintSource({
28 filePath,
29 existingPostcssResult: result,
30 });
31 },
32 };
33};
34
35module.exports.postcss = true;
36
37/**
38 * @param {PostcssPluginOptions} options
39 * @returns {options is StylelintConfig}
40 */
41function isConfig(options) {
42 return 'rules' in options;
43}