AddlocalIdentName.js 1.61 KB
Newer Older
陈帅's avatar
陈帅 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/* eslint-disable */
const postcss = require('postcss');
const syntax = require('postcss-less');

const LocalIdentNameplugin = postcss.plugin('LocalIdentNameplugin', ({ localIdentName }) => {
  return lessAST => {
    // loop add localIdentName
    const loop = nodes => {
      nodes.forEach(item => {
        // Not converted :global
        if (item.nodes && item.selector !== ':global') {
          loop(item.nodes);
        }
        // 将global的 节点加到 parents
        if (item.selector === ':global') {
          const parentNodes = item.parent.nodes;
          const childrenNodes = item.nodes;
          const index = parentNodes.findIndex(node => {
            return node.selector === ':global';
          });
          childrenNodes.unshift(index, 1);
          Array.prototype.splice.apply(parentNodes, item.nodes);
          item.parent.nodes = parentNodes;
          return;
        }
        // 删除 :global(className) 保留 className
        if (item.selector && !item.selector.includes('(')) {
          if (item.selector.includes(':global(')) {
            // converted :global(.className)
            const className = item.selector.match(/:global\((\S*)\)/)[1];
            item.selector = className;
            return;
          }
          const className = item.selector.replace(/\./g, `.${localIdentName}`);
          item.selector = className;
        }
      });
    };
    loop(lessAST.nodes);
  };
});

const AddlocalIdentName = (lessPath, lessText, localIdentName) => {
  return postcss([LocalIdentNameplugin({ localIdentName })]).process(lessText, { syntax });
};

module.exports = AddlocalIdentName;