diff --git a/package.json b/package.json index 2287edbf..07f49dcf 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "dev": "tsup --watch", "clean": "del build", "prebuild": "run-s clean", - "postbuild": "[ -n CI ] && sed -i '' 's/ *\\?\\? *(await import(\"undici\")).fetch//' build/browser/index.js || echo 'skipping postbuild on CI'", + "postbuild": "[ -n CI ] && sed -i '' 's/await import(\"undici\")/null/' build/browser/index.js || echo 'skipping postbuild on CI'", "predev": "run-s clean", "pretest": "run-s build", "docs": "typedoc", diff --git a/src/fetch.ts b/src/fetch.ts index 84480101..f3ea9616 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -1,5 +1,7 @@ /// +let _undici: any + // Use `undici` for node.js 16 and 17 // Use `fetch` for node.js >= 18 // Use `fetch` for all other environments, including browsers @@ -7,6 +9,14 @@ // browser build const fetch = globalThis.fetch ?? - ((await import('undici')).fetch as unknown as typeof globalThis.fetch) + async function undiciFetchWrapper( + ...args: Parameters + ): Promise { + if (!_undici) { + _undici = await import('undici') + } + + return _undici.fetch(...args) + } export { fetch }