样式隔离插件代码postcss-namespace.js如下:
const postcss = require('postcss');
function specailTest(rule) {
if (
rule.parent &&
rule.parent.name &&
rule.parent.name.indexOf('keyframes') > -1
) {
return true
}
return false
}
function classMatchesTest(clss, test) {
if (!test) {
return false
}
clss = clss.trim()
if (test instanceof RegExp) {
return test.exec(clss)
}
if (Array.isArray(test)) {
var tests = test
return tests.some(function(testItem) {
if (testItem instanceof RegExp) {
return testItem.exec(clss)
} else {
return clss === testItem
}
})
}
return clss === test
}
module.exports = postcss.plugin('postcss-namespace', options => {
options = options || {};
const prefix = options.hasOwnProperty('globalName') && options.globalName;
return root => {
if (!prefix || typeof prefix !== 'string') {
return;
}
root.walkRules((rule) => {
if (!rule.selectors) {
return rule;
}