Skip to content

Commit 2f0a2b9

Browse files
fix: rename injectCode to injectCodeFunction and improve CSS injection logic in vite.config.ts
1 parent 2185f5f commit 2f0a2b9

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

vite.config.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,21 @@ const config = () =>
4444
jsAssetsFilterFunction: (outputChunk) => {
4545
return outputChunk.fileName.includes("ThemeContext");
4646
},
47-
// Inject CSS at the beginning of <head> for lower specificity
48-
// This allows users to easily override Treege styles
49-
injectCode: (cssCode) => {
50-
return `try{if(typeof document!=="undefined"){var style=document.createElement("style");style.id="treege-styles";style.appendChild(document.createTextNode(${JSON.stringify(cssCode)}));document.head.insertBefore(style,document.head.firstChild);}}catch(e){console.error("vite-plugin-css-injected-by-js",e);}`;
47+
injectCodeFunction: (cssCode: string) => {
48+
// Inject CSS at the beginning of <head> for lower specificity
49+
// This allows users to easily override Treege styles
50+
const doc = (globalThis as any).document;
51+
52+
try {
53+
if (typeof doc !== "undefined") {
54+
const style = doc.createElement("style");
55+
style.id = "treege-styles";
56+
style.appendChild(doc.createTextNode(cssCode));
57+
doc.head.insertBefore(style, doc.head.firstChild);
58+
}
59+
} catch (e) {
60+
console.error("vite-plugin-css-injected-by-js", e);
61+
}
5162
},
5263
}),
5364
],

0 commit comments

Comments
 (0)