-
-
Save jlongster/a93aa8dabce06ac9392a to your computer and use it in GitHub Desktop.
This file contains 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
module.exports = function loadCSS(fullUrl) { | |
fullUrl = fullUrl.replace(/\.js$/, ''); | |
const links = [...document.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'link')]; | |
let parentNode = ( | |
document.body ? document.body : | |
document.getElementsByTagName('window')[0] | |
); | |
const found = links.find(link => { | |
// Live reloading appends query strings to force a refresh, so | |
// don't use strict equality | |
return link.href.startsWith(fullUrl); | |
}); | |
if(!found) { | |
const newLink = document.createElementNS('http://www.w3.org/1999/xhtml', 'link'); | |
newLink.rel = 'stylesheet'; | |
newLink.type = 'text/css'; | |
newLink.href = fullUrl; | |
parentNode.appendChild(newLink); | |
} | |
} | |
// You would use it like this, inside a component.js file: | |
loadCSS(require.resolve("./debugger.css")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment