-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathextractClassNames.php
107 lines (82 loc) · 2.5 KB
/
extractClassNames.php
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
<?php
// Get latest versions of these two files from
// https://unpkg.com/tailwindcss@%5E2.0/dist/tailwind.css
// https://cdn.jsdelivr.net/npm/@tailwindcss/ui@latest/dist/tailwind-ui.css
$classes['tailwind.css'] = extractClasses(file_get_contents('tailwind.css'));
$classes['tailwind-ui.css'] = extractClasses(file_get_contents('tailwind-ui.css'));
function extractClasses($file)
{
// Strip comments
$pattern = '!/\*[^*]*\*+([^/][^*]*\*+)*/!';
$stripped = preg_replace($pattern, '', $file);
// Strip out everything between { and }
$pattern = '/(?<=\{)(.*?)(?=\})/s';
$stripped = preg_replace($pattern, '', $stripped);
// Remove double line breaks
$stripped = str_replace("\n\n", "\n", $stripped);
// Convert every line to array
$classes = explode("\n", $stripped);
$keepers = [];
for ($i = 0; $i < count($classes); $i++) {
$match = trim($classes[$i]);
// only keep first part, ignore everything after space
$parts = explode(' ', $match);
$match = $parts[0];
$match = stripslashes($match);
$excludeThesePrefixes = [
'.sm:',
'.md:',
'.lg:',
'.xl:',
'.32xl:',
'.active:',
'.focus:',
'.focus-within:',
'.hover:',
'.group:hover',
'.group:focus',
];
if (substr($match, 0, 1) !== '.') {
continue;
}
foreach ($excludeThesePrefixes as $exclude) {
if (strpos($match, $exclude) === 0) {
continue 2;
}
}
$stripThese = [
'.',
'::-moz-placeholder',
'::-ms-check',
'::-ms-expand',
'::-ms-input-placeholder',
'::-webkit-input-placeholder',
'::placeholder',
':-ms-input-placeholder',
':active',
':after',
':checked',
':focus-within',
':focus',
':hover',
];
foreach ($stripThese as $strip) {
$match = str_replace($strip, '', $match);
}
$keepers[] = sprintf(' \'%s\',', $match);
}
return $keepers;
}
$keepers = array_merge($classes['tailwind.css'], $classes['tailwind-ui.css']);
sort($keepers);
$file = 'export default {
data() {
return {
classes: [
' . implode("\n", array_unique($keepers)) . '
],
};
},
};
';
file_put_contents('src/views/tailwind-classes.js', $file);