index.js 672 B

12345678910111213141516171819202122232425262728
  1. /**
  2. * Module dependencies.
  3. */
  4. const postcss = require("postcss")
  5. const valueParser = require("postcss-values-parser")
  6. const color = "#639"
  7. const regexp = /(^|[^\w-])rebeccapurple([^\w-]|$)/
  8. /**
  9. * PostCSS plugin to convert colors
  10. */
  11. module.exports = postcss.plugin("postcss-color-rebeccapurple", () => (style) => {
  12. style.walkDecls((decl) => {
  13. const value = decl.value;
  14. if (value && regexp.test(value)) {
  15. const valueAST = valueParser(value).parse()
  16. valueAST.walk(node => {
  17. if (node.type === "word" && node.value === "rebeccapurple") {
  18. node.value = color
  19. }
  20. })
  21. decl.value = valueAST.toString()
  22. }
  23. })
  24. })