-
-
Notifications
You must be signed in to change notification settings - Fork 216
/
postcss.js
153 lines (123 loc) · 4.32 KB
/
postcss.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*!
* PostCSS RFS plugin
*
* Automated responsive values for font sizes, paddings, margins and much more
*
* Licensed under MIT (https://github.com/twbs/rfs/blob/main/LICENSE)
*/
'use strict';
const postcss = require('postcss');
const RfsClass = require('./lib/rfs.js');
const DISABLE_RFS_SELECTOR = '.disable-rfs';
const ENABLE_RFS_SELECTOR = '.enable-rfs';
module.exports = (opts = {}) => {
const rfs = new RfsClass(opts);
// Get the merged options
opts = rfs.getOptions();
const mediaQuery = rfs.renderMediaQuery();
return css => {
css.walkRules(rule => {
const mediaQueryRules = [];
const extraBlocks = [];
const { parent } = rule;
let removeRule = false;
let dcRule;
let ecRule;
let ruleSelector = rule.selector;
// Prepare rule to add to media query
if (opts.class === 'enable') {
const selectors = rule.selector.split(',');
let tempRuleSelector = '';
for (const selector of selectors) {
tempRuleSelector += `${ENABLE_RFS_SELECTOR} ${selector},\n`;
tempRuleSelector += `${selector + ENABLE_RFS_SELECTOR},\n`;
}
ruleSelector = tempRuleSelector.slice(0, -2);
}
const fluidRule = postcss.rule({
selector: ruleSelector
});
// Disable classes
if (opts.class === 'disable') {
const selectors = rule.selector.split(',');
let ruleSelector = '';
for (const selector of selectors) {
ruleSelector += opts.mode === 'max-media-query' ? `${selector},\n` : '';
ruleSelector += `${DISABLE_RFS_SELECTOR} ${selector},\n`;
ruleSelector += `${selector + DISABLE_RFS_SELECTOR},\n`;
}
ruleSelector = ruleSelector.slice(0, -2);
dcRule = postcss.rule({
selector: ruleSelector,
source: rule.source
});
}
rule.walkDecls(decl => {
// Check if the selector doesn't contain the disabled selector
// and if the value contains the rfs() function
const check = !rule.selector.includes(DISABLE_RFS_SELECTOR) &&
new RegExp(`${opts.functionName}(.*)`, 'g').test(decl.value);
if (!check) {
return;
}
const value = rfs.value(decl.value);
const fluidValue = rfs.fluidValue(decl.value);
decl.value = value;
if (value !== fluidValue) {
const defaultValue = opts.mode === 'min-media-query' ? (opts.class === 'enable' ? value : fluidValue) : value;
const mediaQueryValue = opts.mode === 'min-media-query' ? value : fluidValue;
decl.value = defaultValue;
fluidRule.append(decl.clone({ value: mediaQueryValue }));
mediaQueryRules.push(fluidRule);
// Disable classes
if (opts.class === 'disable') {
const declOpts = opts.mode === 'max-media-query' ? {} : { value };
dcRule.append(decl.clone(declOpts));
extraBlocks.push(dcRule);
} else if (opts.class === 'enable' && opts.mode === 'min-media-query') {
if (ecRule === undefined) {
ecRule = postcss.rule({
selector: ruleSelector,
source: parent.source
});
}
ecRule.append(decl.clone({ value: fluidValue }));
extraBlocks.push(ecRule);
}
// Remove declaration if needed
if (opts.class === 'disable' && opts.mode === 'max-media-query') {
if (decl.prev() || decl.next()) {
decl.remove();
} else {
removeRule = true;
}
}
}
});
if (mediaQueryRules.length === 0) {
return;
}
// Safari iframe resize bug: https://github.com/twbs/rfs/issues/14
if (opts.safariIframeResizeBugFix) {
rule.append({
prop: 'min-width',
value: '0vw'
});
}
const fluidMediaQuery = mediaQuery.clone();
for (const mediaQueryRule of mediaQueryRules) {
fluidMediaQuery.append(mediaQueryRule);
}
parent.insertAfter(rule, fluidMediaQuery);
if (extraBlocks.length > 0) {
for (const disableBlock of extraBlocks) {
parent.insertAfter(rule, disableBlock);
}
}
if (removeRule) {
rule.remove();
}
});
};
};
module.exports.postcss = true;