加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
index.js 1.36 KB
一键复制 编辑 原始数据 按行查看 历史
/**
* Filter a Rework AST out of
*
* @param {Array} [queries] Array of media query strings
* @param {String} [queries] 'none' to filter out all media blocks
* @return {Function} function for reworks use pipeline
* @api public
*/
module.exports = function(queries) {
return function(style, rework){
if (!queries || queries.length === 0) return style;
style.rules = style.rules.reduce(function(rules, rule){
if (!rule.type || rule.type !== 'media') {
rules.push(rule);
return rules;
}
// media-query free stylesheet
if (queries === 'none') return rules;
// TODO: potentially concat all matching rulesets
// together. CSS minifiers may already do this.
if (matchesQueries(rule.media, queries)) {
if (rule.media === 'print') {
rules.push(rule);
return rules;
}
return rules.concat(rule.rules);
}
return rules;
}, []);
}
};
/**
* Does the matched Rework AST rule fulfill any of the
* media queries options passed in.
*
* @param {String} rule a media query string. `screen and (min-width: 600px)`
* @param {Array} queries array of media query strings.
* @return Boolean
* @private
*/
function matchesQueries(rule, queries) {
var matches = queries.filter(function(query) {
return rule.indexOf(query) > -1;
});
return matches.length > 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化