Skip to content

Instantly share code, notes, and snippets.

@jlongster
Created January 14, 2016 18:13
Show Gist options
  • Save jlongster/a93aa8dabce06ac9392a to your computer and use it in GitHub Desktop.
Save jlongster/a93aa8dabce06ac9392a to your computer and use it in GitHub Desktop.
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