postcssPlugin.js 1.1 KB

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