| 52 | } |
| 53 | |
| 54 | async function loadCSSItem(item: CSSItem) { |
| 55 | const url = (item.type === 'stylesheet' && item.data.href) || ''; |
| 56 | item.loaded ||= cssCache[url]; |
| 57 | if (!item.loaded) { |
| 58 | const deferred = defer<void>(); |
| 59 | item.loaded = deferred.promise; |
| 60 | if (url) cssCache[url] = item.loaded; |
| 61 | if (item.type === 'style') { |
| 62 | document.head.append( |
| 63 | hm('style', { |
| 64 | textContent: item.data, |
| 65 | }), |
| 66 | ); |
| 67 | deferred.resolve(); |
| 68 | } else if (url) { |
| 69 | document.head.append( |
| 70 | hm('link', { |
| 71 | rel: 'stylesheet', |
| 72 | ...item.data, |
| 73 | }), |
| 74 | ); |
| 75 | fetch(url) |
| 76 | .then((res) => { |
| 77 | if (res.ok) return res.text(); |
| 78 | throw res; |
| 79 | }) |
| 80 | .then(() => deferred.resolve(), deferred.reject); |
| 81 | } |
| 82 | } |
| 83 | await item.loaded; |
| 84 | } |
| 85 | |
| 86 | export async function loadJS(items: JSItem[], context?: object): Promise<void> { |
| 87 | items.forEach((item) => { |