UNPKG

cssloader

Version:

Simple Asyncronous CSS loader and dependency manager, with promises.

2 lines (1 loc) 900 B
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.cssloader=t()}(this,function(){"use strict";const e=e=>{const t=new Promise((t,n)=>{if(!/(^css!|\.css$)/.test(e))return n(`Invalid css file: ${e}`);const o=document.styleSheets,r=new RegExp(e.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&"));for(let c=0,s=o.length;c<s;c+=1)if((o[c].href||"").match(r))return t(e);const u=document.createElement("link");let f;const d=o=>{try{return u.sheet.rules.length?t(e,f,o.defaultPrevented):n(`No css rules in file: ${e}`)}catch(t){n(`Fail on load css file: ${e}`)}return o};u.rel="stylesheet",u.href=e.replace(/^css!/,""),u.onload=d,u.onbeforeload=d,u.onerror=(()=>{n(`Fail on load css file: ${e}`)});try{document.head.appendChild(u)}catch(t){n(`Fail on load css file: ${e}`)}}).catch(e=>{console.error(e)});return t};return e});