文章目录
好久不见~
话不多说
postcss
一款css处理的平台,官方文档提供非常全面的api,可以对css的定义,修饰,语句进行定制。
常见很多postcss基础的插件,在官方git仓库中都有提到:https://github.com/postcss/postcss
开发postcss插件
我这边有一个需求,是希望可以全局添加一个namespace。
就想到可以添加postcss插件,为每条选择器添加自己的头部。
话不多说,上代码:
const postcss = require('postcss');
module.exports = postcss.plugin('postcss-global-namespace', opts => {
opts = opts || {};
let globalClsName = opts.hasOwnProperty('globalClsName') && opts.globalClsName
return root => {
root.walk(node => {
let selectorArr = (node.selector || '').split(',')
return node.selector