-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
37 lines (28 loc) · 919 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const css = require('css');
function processRule(rule) {
if (!rule || !rule.declarations || !rule.declarations.length) return null;
rule.declarations.forEach(declaration => {
if (declaration.type === 'declaration') {
if (!declaration.value.endsWith('!important')) {
declaration.value += ' !important';
}
}
});
}
function processMediaQuery(rule) {
if (!rule || !rule.rules || !rule.rules.length) return null;
rule.rules.forEach(processRule);
}
module.exports = function (source) {
this.cacheable();
const ast = css.parse(source);
if (!ast || !ast.stylesheet || !ast.stylesheet.rules || !ast.stylesheet.rules.length) throw new Error('error parsing css');
ast.stylesheet.rules.forEach(rule => {
if (rule.type === 'rule') {
processRule(rule);
} else if (rule.type === 'media') {
processMediaQuery(rule);
}
});
return css.stringify(ast);
};