-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
86 lines (73 loc) · 2.43 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
var postcss = require('postcss');
function appendRule(atRule, r, forceAppend) {
if (forceAppend) {
atRule.append(r);
return;
}
var createNewSelector = true;
atRule.walkRules(function (rule) {
if (rule.selector === r.selector) {
rule.append(r.nodes[0]);
createNewSelector = false;
}
});
if (createNewSelector) {
atRule.append(r);
}
}
function createMediaRule(root, r) {
var createNewAR = true;
root.walkAtRules(function (atrule) {
if (atrule.params === r.breakpoint) {
appendRule(atrule, r);
createNewAR = false;
}
});
if (createNewAR) {
var ar = postcss.atRule({
type: 'atrule',
name: 'media',
params: r.breakpoint
});
appendRule(ar, r, true);
root.append(ar);
}
}
module.exports = postcss.plugin('postcss-responsive-properties', function () {
return function (css) {
var root = css.root();
css.walkRules(function (rule) {
if (rule.selector.indexOf(':') === rule.selector.length - 1) {
rule.walkDecls(function (decl) {
if (decl.prop === '0' || decl.type === 'comment') return;
var r = postcss.rule({
type: 'rule',
selector: decl.parent.parent.selector,
breakpoint: 'screen and (min-width: ' +
(!isNaN(decl.prop) ?
parseInt(decl.prop) + 'px' :
decl.prop) + ')',
raws: {
semicolon: true,
before: rule.raws.before.replace(/\n/, '\n')
},
source: decl.source
});
r.append({
type: 'decl',
prop: decl.parent.selector.slice(0, -1),
value: decl.value,
important: decl.important
});
createMediaRule(root, r);
});
rule.replaceWith(postcss.decl({
type: 'decl',
prop: rule.selector.slice(0, -1),
value: rule.nodes[0].value,
important: rule.nodes[0].important
}));
}
});
};
});