forked from konflux-ci/konflux-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaliases.config.js
52 lines (42 loc) · 1.99 KB
/
aliases.config.js
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
41
42
43
44
45
46
47
48
49
50
51
52
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import stripJsonComments from 'strip-json-comments';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const getTsConfigBaseURLAndPath = () => {
const tsconfigPath = path.resolve(__dirname, 'tsconfig.json');
const tsconfig = JSON.parse(stripJsonComments(fs.readFileSync(tsconfigPath, 'utf-8')));
const baseUrl = tsconfig.compilerOptions.baseUrl || '.';
const pathsMapping = tsconfig.compilerOptions.paths || {};
return { baseUrl, pathsMapping };
};
export const getWebpackAliases = () => {
const { baseUrl, pathsMapping } = getTsConfigBaseURLAndPath();
const aliases = {};
// Iterate over each alias defined in tsconfig paths.
for (const [aliasKey, targetPaths] of Object.entries(pathsMapping)) {
// Remove trailing '/*' from the alias key.
const formattedKey = aliasKey.replace(/\/\*$/, '');
// Use the first target path and remove its trailing '/*' if present.
const targetPath = targetPaths[0].replace(/\/\*$/, '');
// Resolve the absolute path using the baseUrl from tsconfig.
aliases[formattedKey] = path.resolve(__dirname, baseUrl, targetPath);
}
return aliases;
};
export const getJestAliases = () => {
const { baseUrl, pathsMapping } = getTsConfigBaseURLAndPath();
const moduleNameMapper = {};
// Iterate over each alias defined in tsconfig paths.
for (const [aliasKey, targetPaths] of Object.entries(pathsMapping)) {
// Create a regex pattern from the alias (e.g. "@routes/*" becomes "^@routes/(.*)$")
const formattedKey = `^${aliasKey.replace(/\/\*$/, '')}/(.*)$`;
// Get the first target path, remove trailing '/*' and build the mapped value.
const targetPath = targetPaths[0].replace(/\/\*$/, '');
// Jest requires the path to be prefixed with "<rootDir>/".
const mappedValue = `<rootDir>/${path.join(baseUrl, targetPath)}/$1`;
moduleNameMapper[formattedKey] = mappedValue;
}
return moduleNameMapper;
};