UNPKG

999 BJavaScriptView 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 tailoredOptions = isConfig(options) ? { config: options } : options;
14 const stylelint = createStylelint(tailoredOptions);
15
16 return {
17 postcssPlugin: 'stylelint',
18 Once(root, { result }) {
19 let filePath = root.source && root.source.input.file;
20
21 if (filePath && !path.isAbsolute(filePath)) {
22 filePath = path.join(process.cwd(), filePath);
23 }
24
25 return stylelint._lintSource({
26 filePath,
27 existingPostcssResult: result,
28 });
29 },
30 };
31};
32
33module.exports.postcss = true;
34
35/**
36 * @param {PostcssPluginOptions} options
37 * @returns {options is StylelintConfig}
38 */
39function isConfig(options) {
40 return 'rules' in options;
41}