Skip to content

Commit

Permalink
feat: import css as separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
infodusha committed Aug 24, 2023
1 parent cbb34b4 commit f69f8c5
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/define-custom-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function defineCustomElement(definedElement) {
#execScripts() {
for (const script of scripts) {
if (script.getAttribute('type') === 'module') {
const url = URL.createObjectURL(new Blob([script.innerText], {type: 'text/javascript'}));
const url = URL.createObjectURL(new Blob([script.innerText], { type: 'text/javascript' }));
import(url).then(() => URL.revokeObjectURL(url)).catch(console.error);
} else {
const code = Function(script.innerText);
Expand Down Expand Up @@ -113,14 +113,13 @@ function setEmulatedStyles(styles, selector) {
cssRules.push(rule);
}

const element = style.cloneNode(true);
const cssText = cssRules.map((rule) => rule.cssText).join('\n');
const url = URL.createObjectURL(new Blob([cssText], { type: 'text/css' }));
const element = document.createElement('link');
element.setAttribute('rel', 'stylesheet');
element.setAttribute('type', 'text/css');
element.href = url;
document.head.appendChild(element);

while (element.sheet.cssRules.length !== 0) {
element.sheet.deleteRule(0);
}
for (const rule of cssRules) {
element.sheet.insertRule(rule.cssText);
}
URL.revokeObjectURL(url);
}
}

0 comments on commit f69f8c5

Please sign in to comment.