blob: fed318110fef30548c453da280729205d410489c [file] [log] [blame]
Mathias Bynens79e2cf02020-05-29 16:46:17 +02001'use strict';
2
Mathias Bynens79e2cf02020-05-29 16:46:17 +02003const createStylelint = require('./createStylelint');
4const path = require('path');
Mathias Bynens79e2cf02020-05-29 16:46:17 +02005
Tim van der Lippe16b82282021-11-08 13:50:26 +00006/** @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;
Mathias Bynens79e2cf02020-05-29 16:46:17 +020014 const stylelint = createStylelint(tailoredOptions);
15
Tim van der Lippe16b82282021-11-08 13:50:26 +000016 return {
17 postcssPlugin: 'stylelint',
18 Once(root, { result }) {
19 let filePath = root.source && root.source.input.file;
Mathias Bynens79e2cf02020-05-29 16:46:17 +020020
Tim van der Lippe16b82282021-11-08 13:50:26 +000021 if (filePath && !path.isAbsolute(filePath)) {
22 filePath = path.join(process.cwd(), filePath);
23 }
Mathias Bynens79e2cf02020-05-29 16:46:17 +020024
Tim van der Lippe16b82282021-11-08 13:50:26 +000025 return stylelint._lintSource({
26 filePath,
27 existingPostcssResult: result,
28 });
29 },
Mathias Bynens79e2cf02020-05-29 16:46:17 +020030 };
Tim van der Lippe16b82282021-11-08 13:50:26 +000031};
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}