Skip to content

Instantly share code, notes, and snippets.

@iamgideonidoko
Created February 5, 2025 07:38
Show Gist options
  • Save iamgideonidoko/abfae662435c33e1d279bbe63ba47e6d to your computer and use it in GitHub Desktop.
Save iamgideonidoko/abfae662435c33e1d279bbe63ba47e6d to your computer and use it in GitHub Desktop.
REPLACE TAILWIND CSS (V3) FONT-SIZE CUSTOM FONT WITH VALUE TO ARBITARY CLASSES
// REPLACE TAILWIND CSS (V3) FONT-SIZE CUSTOM FONT WITH VALUE TO ARBITARY CLASSES
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-var-requires */
import { readdirSync, statSync, readFileSync, writeFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const textSizes = {
1012: ['1rem', '1.2rem'],
1014: ['1rem', '1.4rem'],
1020: ['1rem', '2rem'],
1214: ['1.2rem', '1.4rem'],
1220: ['1.2rem', '2rem'],
1224: ['1.2rem', '2.4rem'],
1313: ['1.3rem', '1.3rem'],
1315: ['1.3rem', '1.5rem'],
1324: ['1.3rem', '2.4rem'],
1413: ['1.4rem', '1.3rem'],
1416: ['1.4rem', '1.6rem'],
1419: ['1.4rem', '1.9rem'],
1421: ['1.4rem', '2.1rem'],
1420: ['1.4rem', '2rem'],
1423: ['1.4rem', '2.3rem'],
1522: ['1.5rem', '2.2rem'],
1618: ['1.6rem', '1.8rem'],
1622: ['1.6rem', '2.2rem'],
1624: ['1.6rem', '2.4rem'],
1630: ['1.6rem', '3rem'],
1821: ['1.8rem', '2.1rem'],
1822: ['1.8rem', '2.2rem'],
1830: ['1.8rem', '3rem'],
2020: ['2rem', '2rem'],
2023: ['2rem', '2.3rem'],
2030: ['2rem', '3rem'],
2428: ['2.4rem', '2.8rem'],
2430: ['2.4rem', '3rem'],
2632: ['2.6rem', '3.2rem'],
3220: ['3.2rem', '2rem'],
3232: ['3.2rem', '3.2rem'],
4064: ['4rem', '6.4rem'],
4150: ['4.1rem', '5rem'],
4855: ['4.8rem', '5.5rem'],
};
const classRegex = /(?:text)-(\d+)(?=\s|'|"|`)/g;
/**
* @param {string} _
* @param {string} value (capture group 1)
* @return {string}
*/
const classReplacer = (_, value) => {
const textInfo = Object.entries(textSizes).find(([k]) => k === value)?.[1];
if (textInfo === undefined) return '';
return `text-[${textInfo[0]}] leading-[${textInfo[1]}]`;
};
const processFiles = (dir) => {
readdirSync(dir).forEach((file) => {
const fullPath = join(dir, file);
if (statSync(fullPath).isDirectory()) {
processFiles(fullPath);
} else if (
file.endsWith('.js') ||
file.endsWith('.jsx') ||
file.endsWith('.ts') ||
file.endsWith('.tsx')
) {
let content = readFileSync(fullPath, 'utf8');
const updatedContent = content.replace(classRegex, classReplacer);
if (content !== updatedContent) {
writeFileSync(fullPath, updatedContent, 'utf8');
console.log(`Updated: ${fullPath}`);
}
}
});
};
processFiles(join(__dirname, './src'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment