Skip to content

Commit

Permalink
Added support for TailwindCSS 1.3
Browse files Browse the repository at this point in the history
Refactored PHP class extractor
Classes are now sorted alphabetically
  • Loading branch information
vesper8 committed Apr 21, 2020
1 parent 30be9e3 commit b3513d4
Show file tree
Hide file tree
Showing 4 changed files with 17,057 additions and 13,152 deletions.
118 changes: 66 additions & 52 deletions extractClassNames.php
Original file line number Diff line number Diff line change
@@ -1,74 +1,88 @@
<?php

// Grab contents of css file
// $file = file_get_contents('tailwind.css');
$file = file_get_contents('tailwind-ui.css');
// Get latest versions of these two files from
// https://unpkg.com/tailwindcss@%5E1.0/dist/tailwind.css
// https://cdn.jsdelivr.net/npm/@tailwindcss/ui@latest/dist/tailwind-ui.css

// Strip comments
$pattern = '!/\*[^*]*\*+([^/][^*]*\*+)*/!';
$stripped = preg_replace($pattern, '', $file);
$classes['tailwind.css'] = extractClasses(file_get_contents('tailwind.css'));
$classes['tailwind-ui.css'] = extractClasses(file_get_contents('tailwind-ui.css'));

// Strip out everything between { and }
$pattern = '/(?<=\{)(.*?)(?=\})/s';
$stripped = preg_replace($pattern, '', $stripped);
function extractClasses($file)
{
// Strip comments
$pattern = '!/\*[^*]*\*+([^/][^*]*\*+)*/!';
$stripped = preg_replace($pattern, '', $file);

// Remove double line breaks
$stripped = str_replace("\n\n", "\n", $stripped);
// Strip out everything between { and }
$pattern = '/(?<=\{)(.*?)(?=\})/s';
$stripped = preg_replace($pattern, '', $stripped);

// Convert every line to array
$classes = explode("\n", $stripped);
// Remove double line breaks
$stripped = str_replace("\n\n", "\n", $stripped);

$keepers = [];
// Convert every line to array
$classes = explode("\n", $stripped);

for ($i = 0; $i < count($classes); $i++) {
$match = trim($classes[$i]);
$keepers = [];

$match = stripslashes($match);
for ($i = 0; $i < count($classes); $i++) {
$match = trim($classes[$i]);

$excludeThesePrefixes = [
'.sm:',
'.md:',
'.lg:',
'.xl:',
$match = stripslashes($match);

'.form',
$excludeThesePrefixes = [
'.sm:',
'.md:',
'.lg:',
'.xl:',

'.group:hover',
'.group:focus',
];
'.form',

if (substr($match, 0, 1) !== '.') {
continue;
}
'.group:hover',
'.group:focus',
];

foreach ($excludeThesePrefixes as $exclude) {
if (strpos($match, $exclude) === 0) {
continue 2;
if (substr($match, 0, 1) !== '.') {
continue;
}

foreach ($excludeThesePrefixes as $exclude) {
if (strpos($match, $exclude) === 0) {
continue 2;
}
}
}

$stripThese = [
'.',
' {}',
':focus:-ms-input-placeholder',
':focus::-ms-input-placeholder',
':focus::placeholder',
'::placeholder',
'::-ms-input-placeholder',
':-ms-input-placeholder',
'::-moz-placeholder',
'::-webkit-input-placeholder',
':focus-within',
':focus',
];

foreach ($stripThese as $strip) {
$match = str_replace($strip, '', $match);
$stripThese = [
'.',
' {}',
':focus:-ms-input-placeholder',
':focus::-ms-input-placeholder',
':focus::placeholder',
'::placeholder',
'::-ms-input-placeholder',
':-ms-input-placeholder',
'::-moz-placeholder',
'::-webkit-input-placeholder',
':focus-within',
':focus',
':hover',
' > :not(template) ~ :not(template)',
];

foreach ($stripThese as $strip) {
$match = str_replace($strip, '', $match);
}

$keepers[] = sprintf(' \'%s\',', $match);
}

$keepers[] = sprintf(' \'%s\',', $match);
return $keepers;
}

$keepers = array_merge($classes['tailwind.css'], $classes['tailwind-ui.css']);

sort($keepers);

$file = '
export default {
data() {
Expand All @@ -81,4 +95,4 @@ classes: [
};
';

file_put_contents('tailwind-classes.js', $file);
file_put_contents('src/views/tailwind-classes.js', $file);
Loading

0 comments on commit b3513d4

Please sign in to comment.