Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cooljl31/02d91ff9bf6c6f006fd9010521122885 to your computer and use it in GitHub Desktop.
Save cooljl31/02d91ff9bf6c6f006fd9010521122885 to your computer and use it in GitHub Desktop.
Vite classname prefixer plugin
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