Skip to content

Instantly share code, notes, and snippets.

@dcdunkan
Last active March 28, 2023 10:46
Show Gist options
  • Save dcdunkan/03f33c53f32fc0dbbd2a3c1570e75b31 to your computer and use it in GitHub Desktop.
Save dcdunkan/03f33c53f32fc0dbbd2a3c1570e75b31 to your computer and use it in GitHub Desktop.
Resolve remote and local paths in Deno
import { fromFileUrl, isAbsolute } from "https://deno.land/std@0.155.0/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