Last active
March 28, 2023 10:46
-
-
Save dcdunkan/03f33c53f32fc0dbbd2a3c1570e75b31 to your computer and use it in GitHub Desktop.
Resolve remote and local paths in Deno
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { fromFileUrl, isAbsolute } from "https://deno.land/[email protected]/path/mod.ts"; | |
const isRemoteImport = ["http:", "https:"] | |
.includes(new URL(import.meta.url).protocol); | |
function isUrl(path: string) { | |
try { | |
new URL(path); | |
return true; | |
} catch (_) { | |
return false; | |
} | |
} | |
function resolvePath(filepath: string) { | |
return isAbsolute(filepath) | |
? filepath | |
: isUrl(filepath) && isRemoteImport(filepath) | |
? filepath | |
: fromFileUrl( | |
filepath.startsWith("file:///") | |
? filepath | |
: import.meta.resolve("../" + filepath), | |
) | |
} | |
console.log(resolvePath("assets/onig.wasm")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment