I am creating an SWC plugin. I will be as brief as possible. The user can style HTML (jsx) elements with attributes.
<div background="red">123</div>
The plugin adds a class attribute with a unique id.
<div className="style642">123</div>
And creates a css file with all found styles
my problem: swc does not allow you to read files. I can't add new styles to css when they are found. I can only completely replace the contents of a file
fs::write("test.css", data).expect("Unable to write file");
I created a global variable
pub struct TransformVisitor {
styles:String,
}
When I find the style attribute, I put the style on that line, and write the whole line to a file
But this variable is cleared for each component.
export default function Home() {
return (
<>
<MyComp></MyComp>
<p background={"red"}>123</p>
</>
)}
During the processing of "MyComp", the styles of the "Home" component will be removed. Only the styles of one component remain in the file.
Help me please. I need a global variable for the duration of the plugin, or any advice.