22 lines
707 B
JavaScript
22 lines
707 B
JavaScript
// 开发期测试用的路径别名解析器:把 @/ 映射到 src/,并为 .ts/.tsx 探测扩展名
|
|
import { pathToFileURL } from "node:url";
|
|
import { resolve as pathResolve } from "node:path";
|
|
|
|
const SRC = pathResolve(process.cwd(), "src");
|
|
|
|
const EXTS = [".ts", ".tsx", ".js", ".mjs", "/index.ts", "/index.tsx"];
|
|
|
|
export async function resolve(specifier, context, nextResolve) {
|
|
if (specifier.startsWith("@/")) {
|
|
const base = pathResolve(SRC, specifier.slice(2));
|
|
for (const ext of EXTS) {
|
|
try {
|
|
return await nextResolve(pathToFileURL(base + ext).href, context);
|
|
} catch {
|
|
// 尝试下一个扩展名
|
|
}
|
|
}
|
|
}
|
|
return nextResolve(specifier, context);
|
|
}
|