fix: remove top-level await import for undici

pull/46/head
Travis Fischer 2022-12-07 00:07:42 -06:00
rodzic 3efc85fa38
commit 23b9fcd595
2 zmienionych plików z 12 dodań i 2 usunięć

Wyświetl plik

@ -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",

Wyświetl plik

@ -1,5 +1,7 @@
/// <reference lib="dom" />
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<typeof globalThis.fetch>
): Promise<Response> {
if (!_undici) {
_undici = await import('undici')
}
return _undici.fetch(...args)
}
export { fetch }