File size: 1,396 Bytes
78664f6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | /**
* Resolves the `@/*` -> `src/*` TypeScript path alias for plain `node` runs.
*
* Node reads neither tsconfig.json nor its `paths`, so maintenance scripts that
* import app modules (`@/models/teams`) fail to resolve without this. Node 24
* strips the TypeScript itself; only the alias and the missing file extension
* need handling.
*
* Registered via `--import ./scripts/register-alias.mjs` in the npm scripts.
*/
import { existsSync } from "node:fs";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
const __dirname = import.meta.dirname ?? path.dirname(fileURLToPath(import.meta.url));
const SRC = path.resolve(__dirname, "..", "src");
// Extensionless imports need a suffix; try the same order tsc would.
const CANDIDATES = ["", ".ts", ".tsx", ".js", ".mjs", "/index.ts", "/index.tsx"];
export async function resolve(specifier, context, next) {
if (specifier === "server-only") {
return next("next/dist/compiled/server-only/empty", context);
}
if (!specifier.startsWith("@/")) return next(specifier, context);
const base = path.join(SRC, specifier.slice(2));
for (const ext of CANDIDATES) {
const candidate = base + ext;
if (existsSync(candidate)) {
return next(pathToFileURL(candidate).href, context);
}
}
throw new Error(
`[register-alias] could not resolve "${specifier}" under ${SRC}`
);
}
|