abi
abi•2y ago

Simpler way to read a file relative to current module?

So I wrote a little helper function for reading the text content of a file relative to the current module. I came up with the following, but is there any simpler way that I'm missing? It seems a bit wordy 😄
import * as path from "std//path/mod.ts";

const gql = (filename: string) =>
Deno.readTextFileSync(
path.resolve(
path.dirname(path.fromFileUrl(import.meta.url)),
filename,
),
);
import * as path from "std//path/mod.ts";

const gql = (filename: string) =>
Deno.readTextFileSync(
path.resolve(
path.dirname(path.fromFileUrl(import.meta.url)),
filename,
),
);
9 Replies
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
abi
abi•2y ago
Ah, thanks a lot 🙂
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Óscar
Óscar•2y ago
even simpler: import.meta.resolve(filename)
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
abi
abi•2y ago
That's a good one, but I think it will only resolve modules, not any file
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Óscar
Óscar•2y ago
It resolves any URL and take into account the import_map.
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View