resolveSpecifier
uses node:module
Where spec, imported from from, is: one of Node's modules, a file (in the same package, or another one found
in node_modules above it, by its export map for conditions), or nothing found.
import { moduleFile, resolvePackageSubpath } from "#codebase/tcmts/cli/npm"; import type { PackageJson } from "#codebase/tcmts/cli/npm"; import fs from "node:fs"; import { isBuiltin } from "node:module"; import path from "node:path"; export function resolveSpecifier( from: string, spec: string, conditions: readonly string[], ): { builtin: string } | { file: string } | undefined { if (isBuiltin(spec)) return { builtin: spec.startsWith("node:") ? spec : `node:${spec}` }; if (spec.startsWith(".") || spec.startsWith("/")) { const file = moduleFile(path.resolve(path.dirname(from), spec)); return file ? { file } : undefined; } const parts = spec.split("/"); const name = spec.startsWith("@") ? parts.slice(0, 2).join("/") : parts[0]!; const sub = `.${spec.slice(name.length)}` || "."; for (let dir = path.dirname(from); ; dir = path.dirname(dir)) { const root = path.join(dir, "node_modules", name); if (fs.existsSync(path.join(root, "package.json"))) { const pkg = JSON.parse( fs.readFileSync(path.join(root, "package.json"), "utf8"), ) as PackageJson; const rel = resolvePackageSubpath(pkg, sub === "." ? "." : sub, [...conditions]); const file = rel && moduleFile(path.join(fs.realpathSync(root), rel)); return file ? { file } : undefined; } if (path.dirname(dir) === dir) return undefined; } }