Created
December 12, 2023 04:29
-
-
Save mayank99/4e0c030ce7f9d04cdad129bff1dd5b20 to your computer and use it in GitHub Desktop.
import attributes polyfill
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function importCss(url) { | |
try { | |
return await (new Function(`return import("${url}", { with: { type: "css" } })`))(); | |
} catch { | |
try { | |
return await (new Function(`return import("${url}", { assert: { type: "css" } })`))(); | |
} catch { | |
return fetch(url).then(res => res.text()).then(cssText => { | |
const stylesheet = new CSSStyleSheet(); | |
stylesheet.replaceSync(cssText); | |
return { default: stylesheet }; | |
}); | |
} | |
} | |
} | |
// usage: | |
// const css = await importCss('https://unpkg.com/open-props'); | |
// document.adoptedStyleSheets = [...document.adoptedStyleSheets, css.default]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment