From 56312a602e50b2788af1e5ae78dc83f71637b53e Mon Sep 17 00:00:00 2001 From: hyrious Date: Fri, 5 Jan 2024 09:50:49 +0800 Subject: [PATCH] improve rollup loading sources --- src/components/Rollup.svelte | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/components/Rollup.svelte b/src/components/Rollup.svelte index a399782..3ee3325 100644 --- a/src/components/Rollup.svelte +++ b/src/components/Rollup.svelte @@ -13,9 +13,23 @@ } else { promise = new Promise(async (resolve, reject) => { // Rollup relies on the script src to find the wasm asset - const full = await fetch('https://unpkg.com/@rollup/browser').then((r) => r.url) + const signal = AbortSignal.timeout(5000) + const timeout = () => reject(new Error('Timeout')) + signal.addEventListener('abort', timeout) + // prettier-ignore + let resolved = await Promise.race([ + fetch('https://unpkg.com/@rollup/browser', { signal }).then((r) => r.url), + fetch('https://cdn.jsdelivr.net/npm/@rollup/browser/package.json', { signal }) + .then((r) => r.json()), + ]) + signal.removeEventListener('abort', timeout) + // is package.json + if (typeof resolved === 'object') { + // currently the 'main' does not contain './', so it is safe to append it + resolved = `https://cdn.jsdelivr.net/npm/@rollup/browser@${resolved.version}/${resolved.main}` + } const script = document.createElement('script') - script.src = full + script.src = resolved script.onload = () => resolve((window as any).rollup) script.onerror = () => reject(new Error(`Could not load Rollup from ${script.src}`)) document.head.appendChild(script)