|
| 1 | +import { promises as fs } from "node:fs"; |
| 2 | +import { createRequire } from "node:module"; |
| 3 | +import path from "node:path"; |
| 4 | +import { fileURLToPath } from "node:url"; |
| 5 | + |
| 6 | +/** |
| 7 | + * 🚨 Why this hack exists: |
| 8 | + * |
| 9 | + * Unlike Node or Bun, Deno's `import.meta.resolve("npm:...")` does NOT return a |
| 10 | + * filesystem path. It just echoes back the npm: specifier. The actual package |
| 11 | + * tarballs are unpacked into `node_modules/.deno/...` when you use |
| 12 | + * `--node-modules-dir`, and normal `node_modules/<pkg>` symlinks only exist for |
| 13 | + * *direct* dependencies. Transitive deps (like @rescript/runtime in our case) |
| 14 | + * only live inside `.deno/` and have no symlink. |
| 15 | + * |
| 16 | + * Because Deno doesn't expose an API for “give me the absolute path of this npm |
| 17 | + * package”, the only way to emulate Node’s/Bun’s `require.resolve` behaviour is |
| 18 | + * to glob inside `.deno/` and reconstruct the path manually. |
| 19 | + * |
| 20 | + * TL;DR: This function exists to compensate for the fact that Deno deliberately hides its |
| 21 | + * npm cache layout. If you want a stable on‑disk path for a package in Deno, |
| 22 | + * you have to spelunk `node_modules/.deno/>pkg@version>/node_modules/<pkg>`. |
| 23 | + * |
| 24 | + * If Deno ever ships a proper API for this, replace this hack immediately. |
| 25 | + */ |
| 26 | +async function resolvePackageInDeno(pkgName) { |
| 27 | + const base = path.resolve("node_modules/.deno"); |
| 28 | + const pkgId = pkgName.startsWith("@") ? pkgName.replace("/", "+") : pkgName; |
| 29 | + |
| 30 | + const { expandGlob } = await import("https://deno.land/std/fs/mod.ts"); |
| 31 | + for await (const entry of expandGlob( |
| 32 | + path.join(base, `${pkgId}@*/node_modules/${pkgName}`), |
| 33 | + )) { |
| 34 | + if (entry.isDirectory) { |
| 35 | + return await fs.realpath(entry.path); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + throw new Error( |
| 40 | + `Could not resolve ${pkgName} in Deno. Did you enable --node-modules-dir?`, |
| 41 | + ); |
| 42 | +} |
| 43 | + |
| 44 | +async function resolvePackageRoot(pkgName) { |
| 45 | + const specifier = |
| 46 | + typeof globalThis.Deno !== "undefined" |
| 47 | + ? `npm:${pkgName}/package.json` |
| 48 | + : `${pkgName}/package.json`; |
| 49 | + |
| 50 | + if (typeof import.meta.resolve === "function") { |
| 51 | + const url = import.meta.resolve(specifier); |
| 52 | + |
| 53 | + if (url.startsWith("file://")) { |
| 54 | + // Node & Bun: real local file |
| 55 | + const abs = path.dirname(fileURLToPath(url)); |
| 56 | + return await fs.realpath(abs); |
| 57 | + } |
| 58 | + |
| 59 | + if (typeof globalThis.Deno !== "undefined") { |
| 60 | + return await resolvePackageInDeno(pkgName); |
| 61 | + } |
| 62 | + |
| 63 | + throw new Error( |
| 64 | + `Could not resolve ${pkgName} (no physical path available)`, |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + // Node fallback |
| 69 | + const require = createRequire(import.meta.url); |
| 70 | + try { |
| 71 | + const abs = path.dirname(require.resolve(`${pkgName}/package.json`)); |
| 72 | + return await fs.realpath(abs); |
| 73 | + } catch { |
| 74 | + throw new Error(`Could not resolve ${pkgName} in Node runtime`); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +export const runtimePath = await resolvePackageRoot("@rescript/runtime"); |
0 commit comments