Forked from Anush008/gist:ffebc6ceb5439dc1ca2996ff7fb88311
Created
February 26, 2024 18:36
-
-
Save cooljl31/02d91ff9bf6c6f006fd9010521122885 to your computer and use it in GitHub Desktop.
Vite classname prefixer plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function classnamePrefixPlugin() { | |
const prefix = 'tw-' | |
return { | |
name: 'classname-prefix', | |
transform: (code: string, id: string) => { | |
const classNamePattern = /(className|class)\s*(:|=)\s*"([^"]*)"/g | |
const transformedCode = code.replace(classNamePattern, (match, p1, p2, p3) => { | |
const transformedClassName = p3 | |
.split(' ') | |
.map((className: string) => `${prefix}${className}`) | |
.join(' ') | |
console.log('File: ', id, 'Classname: ', `${p1}${p2}"${transformedClassName}"`) | |
return `${p1}${p2}"${transformedClassName}"` | |
}) | |
return { | |
code: transformedCode | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment