项目作者: fi3ework

项目描述 :
🌖 Rename CSS selectors effortlessly
高级语言: TypeScript
项目地址: git://github.com/fi3ework/postcss-rename-selector.git
创建时间: 2020-04-04T08:55:11Z
项目社区:https://github.com/fi3ework/postcss-rename-selector

开源协议:MIT License

下载


postcss-rename-selector

A PostCSS plugin for modify CSS selector flexible.

Usage

Plugin support two ways to modify selectors. In raw mode, you can return a new string from original selector string. In AST mode, you can modify the AST to get a generated selector string.

raw mode

In this mode, type should be fixed to string. Argument raw is the raw selector string, and the selector will use the returned new string.

  1. import postcss from 'postcss'
  2. import { replacer } from 'postcss-rename-selector'
  3. module.exports = {
  4. plugins: [
  5. replacer({
  6. type: 'string',
  7. replacer: (raw) => {
  8. return raw.replace('.b', '.ant-b')
  9. },
  10. }),
  11. ],
  12. }
  13. // in 👇
  14. // .a, .b, .c { color: red };
  15. // out 👇
  16. // .a, .ant-b, .c { color: red };

AST mode

Based on postcss-selector-parser, type could be each, walk, walkAttributes, walkClasses, walkCombinators, walkComments, walkIds, walkNesting, walkPseudos, walkTags. Modify node on the AST, and new string will be generated after modification.

  1. import postcss from 'postcss'
  2. import { replacer } from 'postcss-rename-selector'
  3. module.exports = {
  4. plugins: [
  5. replacer({
  6. type: 'string',
  7. replacer: (node) => {
  8. const value = node.value
  9. if (!value) return
  10. node.value = value.startsWith('ant-') ? value.slice(4) : value
  11. },
  12. }),
  13. ],
  14. }
  15. // in 👇
  16. // .ant-a,
  17. // .ant-b,
  18. // .c,
  19. // .ant-d {
  20. // color: red
  21. // };
  22. // out 👇
  23. // .a,
  24. // .b,
  25. // .c,
  26. // .d {
  27. // color: red
  28. // };

License

MIT