-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvitest.aliases.ts
More file actions
37 lines (29 loc) · 1.22 KB
/
vitest.aliases.ts
File metadata and controls
37 lines (29 loc) · 1.22 KB
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
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { readConfigFile, sys } from "typescript";
const tsconfigPath = fileURLToPath(new URL("./tooling/tsconfig/base.json", import.meta.url));
const tsconfigDir = dirname(tsconfigPath);
const { config, error } = readConfigFile(tsconfigPath, sys.readFile);
if (error) {
throw new Error(`Failed to read ${tsconfigPath}: ${String(error.messageText)}`);
}
const baseUrl = config.compilerOptions?.baseUrl ?? ".";
const paths = (config.compilerOptions?.paths ?? {}) as Record<string, string[]>;
const normalizedEntries = Object.entries(paths).flatMap(([find, targets]) => {
const target = targets[0];
if (!target) {
return [];
}
const normalizedFind = find.includes("*") ? find.replace(/\*.*$/, "") : find;
const normalizedTarget = target.includes("*") ? target.replace(/\*.*$/, "") : target;
const replacement = resolve(tsconfigDir, baseUrl, normalizedTarget);
return [[
normalizedFind,
normalizedFind.endsWith("/") ? `${replacement}/` : replacement,
] as const];
});
export const aliases = normalizedEntries
.map(([find, replacement]) => {
return { find, replacement };
})
.sort((a, b) => b.find.length - a.find.length);