-
Notifications
You must be signed in to change notification settings - Fork 0
/
css-injector.js
25 lines (23 loc) · 910 Bytes
/
css-injector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
export const customStyleInjector = (packageName) => (varname, id) => {
const idSplit = id.split("/");
const srcIndex = idSplit.indexOf("src");
const resolvedId = JSON.stringify([packageName, ...idSplit.slice(srcIndex + 1)].join("/"));
if (process.env.NODE_ENV === "development") {
return `const $prevStyle = document.getElementById(${resolvedId});
if (!$prevStyle) {
const $style = document.createElement("style");
$style.id = ${resolvedId};
$style.type = "text/css";
$style.innerHTML = ${varname};
document.head.appendChild($style);
} else if ($prevStyle.innerHTML !== ${varname}) {
$prevStyle.innerHTML = ${varname};
}`;
} else {
return `const $style = document.createElement("style");
$style.id = ${resolvedId};
$style.type = "text/css";
$style.innerHTML = ${varname};
document.head.appendChild($style);`;
}
};