Skip to content

Commit 4f65f2a

Browse files
authored
Merge pull request #1 from mpvue/feat/filter
支持对@规则的筛选
2 parents d4caaa4 + aaa3c7e commit 4f65f2a

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

index.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
const postcss = require('postcss');
55
const selectorParser = require('postcss-selector-parser');
66
const replaceTagSelector = require('./lib/wxmlTagMap');
7+
const { isRegExp } = require('./lib/utils');
78

89
const defConfig = {
910
cleanSelector: ['*'],
11+
cleanAtRule: [{
12+
name: 'media',
13+
params: ['print']
14+
}],
1015
remToRpx: 100,
1116
replaceTagSelector
1217
};
@@ -20,6 +25,23 @@ module.exports = postcss.plugin('postcss-mpvue-wxss', function (options) {
2025

2126
return function (root) {
2227
// Transform CSS AST here: root, result
28+
root.walkAtRules(rule => {
29+
// 清理不支持的@开头规则
30+
for (cleanRule of options.cleanAtRule) {
31+
if (cleanRule.name !== rule.name) {
32+
continue;
33+
}
34+
if (!cleanRule.params) {
35+
return rule.remove();
36+
}
37+
for (param of cleanRule.params) {
38+
if (isRegExp(param) && param.test(rule.params) || param === rule.params) {
39+
return rule.remove();
40+
}
41+
}
42+
}
43+
});
44+
2345
root.walkRules(rule => {
2446
const { selector } = rule || {};
2547

@@ -43,7 +65,7 @@ module.exports = postcss.plugin('postcss-mpvue-wxss', function (options) {
4365
// 清理不支持的选择器
4466
if (options.cleanSelector.includes(n.value)) {
4567
// return n.value = 'view';
46-
return rule.remove()
68+
return rule.remove();
4769
}
4870
})
4971
})

lib/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
isRegExp: function (arg) {
3+
return Object.prototype.toString.call(arg) === '[object RegExp]';
4+
}
5+
}

test/index.test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ it('test cleanSelector', () => {
2525
}
2626
*,:after,:before{
2727
box-sizing:inherit;
28+
}
29+
@media print {
30+
body {
31+
display: block;
32+
}
2833
}`;
2934
const output = ``;
3035
const options = {};
@@ -41,10 +46,19 @@ it('test cleanSelector with options', () => {
4146
}
4247
bbb {
4348
margin: 100px
49+
}
50+
@media screen and (max-width: 300px) {
51+
body {
52+
display: block;
53+
}
4454
}`;
4555
const output = ``;
4656
const options = {
47-
cleanSelector: ['aaa', 'bbb', '*']
57+
cleanSelector: ['aaa', 'bbb', '*'],
58+
cleanAtRule: [{
59+
name: 'media',
60+
params: [/screen/]
61+
}]
4862
};
4963
return run(input, output, options);
5064
});

0 commit comments

Comments
 (0)