Skip to content

Instantly share code, notes, and snippets.

@eltorio
Created October 23, 2022 07:52
Show Gist options
  • Save eltorio/785188ecb98285a74b991c46cc8f6012 to your computer and use it in GitHub Desktop.
Save eltorio/785188ecb98285a74b991c46cc8f6012 to your computer and use it in GitHub Desktop.
/*
=========================================================
* © 2022 Ronan LE MEILLAT for %INTERNAL_CLIENT%
* MIT License
=========================================================
This website use:
- Vuejs v3
- Tailwindcss
- Font Awesome
- And many others
*/
import { readFileSync } from "fs";
import glob from "glob";
import path from "path";
import { fileURLToPath } from "url";
function getSetOfClasses(data: string): string[] {
const regExp = new RegExp(
/(?:class)=(?:["']\W+\s*(?:\w+)\()?["']([^'"]+)['"]/g
);
const tresult = [] as string[];
for (const match of data.matchAll(regExp)) {
const noNewLine = match[1].replace(new RegExp(/\n/g), " ");
const noMultipleSpace = noNewLine.replace(new RegExp(/\s\s+/g), " ");
const arrayOfSingleClasses = noMultipleSpace.trim().split(" ");
tresult.push(...arrayOfSingleClasses);
}
return [...new Set(tresult)];
}
function getAllClasses(pattern: string): Promise<string[]> {
const tresult = [] as string[];
return new Promise((resolve, reject) => {
{
glob(pattern, {}, (err, files) => {
if (err) {
reject(err);
}
files.forEach((file: string) => {
const data = readFileSync(file).toString();
tresult.push(...getSetOfClasses(data));
});
resolve([...new Set(tresult)]);
});
}
});
}
function getFilteredClasses(setOfClasses:string[],filter:RegExp):string[]{
const tresult = [] as string[]
setOfClasses.forEach((_class:string)=>{
if(_class.match(filter)){
tresult.push(_class)
}
})
return [...new Set(tresult)]
}
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const filteredClassnamesRegExp = /(bg|[-]*p[xylrbt]*|[-]*m[xylrbt]*|[-]*left|[-]*top|[-]*right|[-]*bottom|[-]*w|[-]*z|h|fa|fas|far|fab|fad|justify|overflow|border|max|flex|text|font|inline|rounded|from|sr|to|via|contrast|brightness|leading|items|backdrop|shadow|duration|whitespace|self|cursor|transition|translate|outline)-[a-z0-9_-]+|shadow|flex|rounded|border/;
const nonFilteredClasses = await getAllClasses(__dirname + "/src/**/*.vue")
console.log(getFilteredClasses(nonFilteredClasses,filteredClassnamesRegExp));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment