abi
abi16mo 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 User16mo ago
Message Not Public
Sign In & Join Server To View
abi
abi16mo ago
Ah, thanks a lot 🙂
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Óscar
Óscar16mo ago
even simpler: import.meta.resolve(filename)
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
abi
abi16mo ago
That's a good one, but I think it will only resolve modules, not any file
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
Óscar
Óscar16mo ago
It resolves any URL and take into account the import_map.
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View