index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. module.exports = function postcssPrefixSelector(options) {
  2. const prefix = options.prefix;
  3. const prefixWithSpace = /\s+$/.test(prefix) ? prefix : `${prefix} `;
  4. return function(root) {
  5. root.walkRules(rule => {
  6. const keyframeRules = [
  7. 'keyframes',
  8. '-webkit-keyframes',
  9. '-moz-keyframes',
  10. '-o-keyframes'
  11. ];
  12. if (rule.parent && keyframeRules.includes(rule.parent.name)) {
  13. return;
  14. }
  15. rule.selectors = rule.selectors.map(selector => {
  16. if (options.exclude && excludeSelector(selector, options.exclude)) {
  17. return selector;
  18. }
  19. if (options.transform) {
  20. return options.transform(
  21. prefix,
  22. selector,
  23. prefixWithSpace + selector
  24. );
  25. }
  26. return prefixWithSpace + selector;
  27. });
  28. });
  29. };
  30. };
  31. function excludeSelector(selector, excludeArr) {
  32. return excludeArr.some(excludeRule => {
  33. if (excludeRule instanceof RegExp) {
  34. return excludeRule.test(selector);
  35. }
  36. return selector === excludeRule;
  37. });
  38. }