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