Skip to content

Instantly share code, notes, and snippets.

@Anush008
Created July 25, 2023 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Anush008/ffebc6ceb5439dc1ca2996ff7fb88311 to your computer and use it in GitHub Desktop.
Save Anush008/ffebc6ceb5439dc1ca2996ff7fb88311 to your computer and use it in GitHub Desktop.
Vite classname prefixer plugin
export default function classnamePrefixPlugin() {
const prefix = "tw-";
return {
name: "classname-prefix",
transform: (code, id) => {
let classNamePattern = /(className|class)\s*(:|=)\s*"([^"]*)"/g;
const transformedCode = code.replace(
classNamePattern,
(match, p1, p2, p3) => {
const transformedClassName = p3
.split(" ")
.map((className) => `${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