forked from vadimdemedes/tailwind-rn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
130 lines (114 loc) · 2.88 KB
/
build.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
'use strict';
const css = require('css');
const cssToReactNative = require('css-to-react-native').default;
const remToPx = value => `${Number.parseFloat(value) * 16}px`;
const getStyles = rule => {
const styles = rule.declarations
.filter(({property, value}) => {
// Skip line-height utilities without units
if (property === 'line-height' && !value.endsWith('rem')) {
return false;
}
return true;
})
.map(({property, value}) => {
if (value.endsWith('rem')) {
return [property, remToPx(value)];
}
return [property, value];
});
return cssToReactNative(styles);
};
const supportedUtilities = [
// Flexbox
/^flex/,
/^items-/,
/^content-/,
/^justify-/,
/^self-/,
// Display
'hidden',
'overflow-hidden',
'overflow-visible',
'overflow-scroll',
// Position
'absolute',
'relative',
// Top, right, bottom, left
/^(inset-0|inset-x-0|inset-y-0)/,
/^(top|bottom|left|right)-0$/,
// Z Index
/^z-\d+$/,
// Padding
/^(p.?-\d+|p.?-px)/,
// Margin
/^-?(m.?-\d+|m.?-px)/,
// Width
/^w-(\d|\/)+|^w-px|^w-full/,
// Height
/^(h-\d+|h-px|h-full)/,
// Min/Max width/height
/^(min-w-|max-w-|min-h-0|min-h-full|max-h-full)/,
// Font size
/^text-/,
// Font style
/^(not-)?italic$/,
// Font weight
/^font-(hairline|thin|light|normal|medium|semibold|bold|extrabold|black)/,
// Letter spacing
/^tracking-/,
// Line height
/^leading-\d+/,
// Text align, color, opacity
/^text-/,
// Text transform
'uppercase',
'lowercase',
'capitalize',
'normal-case',
// Background color
/^bg-(transparent|black|white|gray|red|orange|yellow|green|teal|blue|indigo|purple|pink)/,
// Background opacity
/^bg-opacity-/,
// Border color, style, width, radius, opacity
/^(border|rounded)/,
// Opacity
/^opacity-/,
// Pointer events
/^pointer-events-/
];
const isUtilitySupported = utility => {
// Skip utilities with `currentColor` values
if (['border-current', 'text-current'].includes(utility)) {
return false;
}
for (const supportedUtility of supportedUtilities) {
if (typeof supportedUtility === 'string' && supportedUtility === utility) {
return true;
}
if (supportedUtility instanceof RegExp && supportedUtility.test(utility)) {
return true;
}
}
return false;
};
module.exports = source => {
const {stylesheet} = css.parse(source);
// Mapping of Tailwind class names to React Native styles
const styles = {};
for (const rule of stylesheet.rules) {
if (rule.type === 'rule') {
for (const selector of rule.selectors) {
const utility = selector.replace(/^\./, '').replace('\\/', '/');
if (isUtilitySupported(utility)) {
styles[utility] = getStyles(rule);
}
}
}
}
// Additional styles that we're not able to parse correctly automatically
styles.underline = {textDecorationLine: 'underline'};
styles['line-through'] = {textDecorationLine: 'line-through'};
styles['no-underline'] = {textDecorationLine: 'none'};
return styles;
};