Skip to content

支持对@规则的筛选 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
const postcss = require('postcss');
const selectorParser = require('postcss-selector-parser');
const replaceTagSelector = require('./lib/wxmlTagMap');
const { isRegExp } = require('./lib/utils');

const defConfig = {
cleanSelector: ['*'],
cleanAtRule: [{
name: 'media',
params: ['print']
}],
remToRpx: 100,
replaceTagSelector
};
Expand All @@ -20,6 +25,23 @@ module.exports = postcss.plugin('postcss-mpvue-wxss', function (options) {

return function (root) {
// Transform CSS AST here: root, result
root.walkAtRules(rule => {
// 清理不支持的@开头规则
for (cleanRule of options.cleanAtRule) {
if (cleanRule.name !== rule.name) {
continue;
}
if (!cleanRule.params) {
return rule.remove();
}
for (param of cleanRule.params) {
if (isRegExp(param) && param.test(rule.params) || param === rule.params) {
return rule.remove();
}
}
}
});

root.walkRules(rule => {
const { selector } = rule || {};

Expand All @@ -43,7 +65,7 @@ module.exports = postcss.plugin('postcss-mpvue-wxss', function (options) {
// 清理不支持的选择器
if (options.cleanSelector.includes(n.value)) {
// return n.value = 'view';
return rule.remove()
return rule.remove();
}
})
})
Expand Down
5 changes: 5 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
isRegExp: function (arg) {
return Object.prototype.toString.call(arg) === '[object RegExp]';
}
}
16 changes: 15 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ it('test cleanSelector', () => {
}
*,:after,:before{
box-sizing:inherit;
}
@media print {
body {
display: block;
}
}`;
const output = ``;
const options = {};
Expand All @@ -41,10 +46,19 @@ it('test cleanSelector with options', () => {
}
bbb {
margin: 100px
}
@media screen and (max-width: 300px) {
body {
display: block;
}
}`;
const output = ``;
const options = {
cleanSelector: ['aaa', 'bbb', '*']
cleanSelector: ['aaa', 'bbb', '*'],
cleanAtRule: [{
name: 'media',
params: [/screen/]
}]
};
return run(input, output, options);
});
Expand Down
Loading