From 23b9fcd5955b181de218d0a60f2043fc88829ea6 Mon Sep 17 00:00:00 2001 From: Travis Fischer Date: Wed, 7 Dec 2022 00:07:42 -0600 Subject: [PATCH] fix: remove top-level await import for undici --- package.json | 2 +- src/fetch.ts | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 2287edb..07f49dc 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 8448010..f3ea961 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 }