-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
removeUnsupported.js
253 lines (233 loc) · 6.96 KB
/
removeUnsupported.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
const remRE = /\d?\.?\d+\s*r?em/g;
function isSupportedProperty(prop, val = null) {
const rules = supportedProperties[prop];
if (!rules) return false;
if (val) {
if (unsupportedValues.some((unit) => val.endsWith(unit))) {
return false;
}
if (Array.isArray(rules)) {
return rules.includes(val);
}
}
return true;
}
function isSupportedSelector(selector) {
const hasUnsupportedPseudoSelector = unsupportedPseudoSelectors.some(
(pseudo) => selector.includes(pseudo)
);
return !hasUnsupportedPseudoSelector;
}
function isPlaceholderPseudoSelector(selector) {
return selector.includes("::placeholder");
}
/**
* @param {@} options
* @returns {import('postcss').Plugin}
*/
module.exports = (options = { debug: false }) => {
return {
postcssPlugin: "postcss-nativescript",
AtRule: {
// remove @media rules because they
// are currently not supported
// in NativeScript
media(mediaAtRule) {
mediaAtRule.remove();
},
},
Rule(rule) {
// remove empty rules
if (rule.nodes.length === 0) {
return rule.remove();
}
// remove rules with unsupported selectors
if (!isSupportedSelector(rule.selector)) {
return rule.remove();
}
// convert ::placeholder pseudo selector
// to use placeholder-color declaration
if (isPlaceholderPseudoSelector(rule.selector)) {
const placeholderSelectors = [];
rule.selectors.forEach((selector) => {
if (isPlaceholderPseudoSelector(selector)) {
placeholderSelectors.push(selector.replace(/::placeholder/g, ""));
}
});
if (placeholderSelectors.length) {
rule.selectors = placeholderSelectors;
rule.walkDecls((decl) => {
if (decl.prop === "color") {
decl.replaceWith(decl.clone({ prop: "placeholder-color" }));
}
});
}
// rule.selector.replace('::placeholder', '')
}
// remove :where() pseudo selector, introduced in Tailwind 3.4.
if (rule.selector.includes(":where(")) {
rule.selectors = rule.selectors.map((selector) =>
selector.replace(/:where\((.+)\)/, "$1")
);
}
// replace space and divide selectors to use a simpler selector that works in ns
if (rule.selector.includes(":not([hidden]) ~ :not([hidden])")) {
rule.selectors = rule.selectors.map((selector) => {
return selector.replace(":not([hidden]) ~ :not([hidden])", "* + *");
});
}
},
Declaration(decl) {
// replace visibility: hidden
// with visibility: collapse
if (decl.prop === "visibility") {
switch (decl.value) {
case "hidden":
return decl.replaceWith(decl.clone({ value: "collapse" }));
}
}
// replace vertical-align: middle
// with vertical-align: center
if (decl.prop === "vertical-align") {
switch (decl.value) {
case "middle":
return decl.replaceWith(decl.clone({ value: "center" }));
}
}
// declarations that define unsupported variables/rules
if (
[
"tw-ring",
"tw-shadow",
"tw-ordinal",
"tw-slashed-zero",
"tw-numeric",
].some((varName) => decl.prop.startsWith(`--${varName}`))
) {
return decl.remove();
}
// Convert em/rem values to device pixel values
// assuming 16 as the basis for rem and
// treating em as rem
if (decl.value.includes("rem") || decl.value.includes("em")) {
decl.value = decl.value.replace(remRE, (match, offset, value) => {
const converted = "" + parseFloat(match) * 16;
options.debug &&
console.log("replacing r?em value", {
match,
offset,
value,
converted,
});
return converted;
});
options.debug &&
console.log({
final: decl.value,
});
}
// remove unsupported properties
if (
!decl.prop.startsWith("--") &&
!isSupportedProperty(decl.prop, decl.value)
) {
// options.debug && console.log('removing ', decl.prop, decl.value)
return decl.remove();
}
},
};
};
module.exports.postcss = true;
const supportedProperties = {
"align-content": true,
"align-items": true,
"align-self": true,
"android-selected-tab-highlight-color": true,
"android-elevation": true,
"android-dynamic-elevation-offset": true,
animation: true,
"animation-delay": true,
"animation-direction": true,
"animation-duration": true,
"animation-fill-mode": true,
"animation-iteration-count": true,
"animation-name": true,
"animation-timing-function": true,
background: true,
"background-color": true,
"background-image": true,
"background-position": true,
"background-repeat": ["repeat", "repeat-x", "repeat-y", "no-repeat"],
"background-size": true,
"border-bottom-color": true,
"border-bottom-left-radius": true,
"border-bottom-right-radius": true,
"border-bottom-width": true,
"border-color": true,
"border-left-color": true,
"border-left-width": true,
"border-radius": true,
"border-right-color": true,
"border-right-width": true,
"border-top-color": true,
"border-top-left-radius": true,
"border-top-right-radius": true,
"border-top-width": true,
"border-width": true,
"box-shadow": true,
"clip-path": true,
color: true,
flex: true,
"flex-grow": true,
"flex-direction": true,
"flex-shrink": true,
"flex-wrap": true,
font: true,
"font-family": true,
"font-size": true,
"font-style": ["italic", "normal"],
"font-weight": true,
height: true,
"highlight-color": true,
"horizontal-align": ["left", "center", "right", "stretch"],
"justify-content": true,
"justify-items": true,
"justify-self": true,
"letter-spacing": true,
"line-height": true,
margin: true,
"margin-bottom": true,
"margin-left": true,
"margin-right": true,
"margin-top": true,
"min-height": true,
"min-width": true,
"off-background-color": true,
opacity: true,
order: true,
padding: true,
"padding-bottom": true,
"padding-left": true,
"padding-right": true,
"padding-top": true,
"place-content": true,
"placeholder-color": true,
"place-items": true,
"place-self": true,
"selected-tab-text-color": true,
"tab-background-color": true,
"tab-text-color": true,
"tab-text-font-size": true,
"text-transform": true,
"text-align": ["left", "center", "right"],
"text-decoration": ["none", "line-through", "underline"],
"text-shadow": true,
"text-transform": ["none", "capitalize", "uppercase", "lowercase"],
transform: true,
"vertical-align": ["top", "center", "bottom", "stretch"],
visibility: ["visible", "collapse"],
width: true,
"z-index": true,
};
const unsupportedPseudoSelectors = [":focus-within", ":hover"];
const unsupportedValues = ["max-content", "min-content", "vh", "vw"];