postcssPlugin.cjs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // NOTICE: This file is generated by Rollup. To modify it,
  2. // please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
  3. 'use strict';
  4. const path = require('node:path');
  5. const process = require('node:process');
  6. const createStylelint = require('./createStylelint.cjs');
  7. const validateTypes = require('./utils/validateTypes.cjs');
  8. const lintSource = require('./lintSource.cjs');
  9. /** @import {Config as StylelintConfig, PostcssPluginOptions} from 'stylelint' */
  10. /**
  11. * @type {import('postcss').PluginCreator<PostcssPluginOptions>}
  12. */
  13. function postcssPlugin(options = {}) {
  14. const [cwd, tailoredOptions] = isConfig(options)
  15. ? [process.cwd(), { config: options }]
  16. : [('cwd' in options && validateTypes.isString(options.cwd) && options.cwd) || process.cwd(), options];
  17. const stylelint = createStylelint(tailoredOptions);
  18. return {
  19. postcssPlugin: 'stylelint',
  20. /**
  21. * @param {import('postcss').Root} root
  22. * @param {import('postcss').Helpers} helpers
  23. * @returns {Promise<void>}
  24. */
  25. async Once(root, { result }) {
  26. let filePath = root.source && root.source.input.file;
  27. if (filePath && !path.isAbsolute(filePath)) {
  28. filePath = path.join(cwd, filePath);
  29. }
  30. await lintSource(stylelint, {
  31. filePath,
  32. existingPostcssResult: result,
  33. });
  34. },
  35. };
  36. }
  37. postcssPlugin.postcss = true;
  38. /**
  39. * @param {PostcssPluginOptions} options
  40. * @returns {options is StylelintConfig}
  41. */
  42. function isConfig(options) {
  43. return 'rules' in options;
  44. }
  45. module.exports = postcssPlugin;