MCPcopy Index your code
hub / github.com/NativeScript/NativeScript / getTsConfigPaths

Function getTsConfigPaths

packages/vite/helpers/ts-config-paths.ts:10–104  ·  view source on GitHub ↗
(debugViteLogs: boolean = false)

Source from the content-addressed store, hash-verified

8
9// Read TypeScript path mappings
10function getTsConfigPaths(debugViteLogs: boolean = false) {
11 try {
12 if (debugViteLogs) console.log('📁 Parsing tsconfig at:', tsConfigPath);
13 // The configDir should be the directory of the starting tsconfig file
14 const startingConfigDir = path.dirname(tsConfigPath);
15 // Recursive function to resolve tsconfig extends chain
16 function resolveTsConfigChain(configPath, visitedPaths = new Set()) {
17 // Prevent infinite loops
18 if (visitedPaths.has(configPath)) {
19 if (debugViteLogs) console.log('📁 Warning: Circular tsconfig extends detected, skipping:', configPath);
20 return { paths: {}, baseUrl: '.' };
21 }
22 visitedPaths.add(configPath);
23 const tsConfigContent = fs.readFileSync(configPath, 'utf8');
24 // Parse JSON (handle JSONC)
25 let tsConfig;
26 try {
27 tsConfig = JSON.parse(tsConfigContent);
28 } catch (parseError) {
29 // Clean up JSONC
30 if (debugViteLogs) console.log('📁 Cleaning JSONC for:', configPath);
31 let cleanContent = tsConfigContent
32 .replace(/\/\/.*$/gm, '')
33 .replace(/\/\*[\s\S]*?\*\//g, '')
34 .replace(/,(\s*[}\]])/g, '$1');
35 tsConfig = JSON.parse(cleanContent);
36 }
37 // Start with current config's options
38 let currentPaths = { ...(tsConfig.compilerOptions?.paths || {}) };
39 let mergedBaseUrl = tsConfig.compilerOptions?.baseUrl || '.';
40 const currentConfigDir = path.dirname(configPath);
41
42 // Handle path resolution for this config file
43 if (currentPaths) {
44 const resolvedPaths = {};
45 for (const [key, values] of Object.entries(currentPaths)) {
46 if (Array.isArray(values)) {
47 resolvedPaths[key] = values.map((value) => {
48 // Handle ${configDir} substitution - use the STARTING config directory
49 if (value.includes('${configDir}')) {
50 return value.replace(/\$\{configDir\}/g, startingConfigDir);
51 }
52 // For other paths, resolve relative to THIS config file's directory
53 if (!path.isAbsolute(value)) {
54 return path.resolve(currentConfigDir, value);
55 }
56 return value;
57 });
58 } else {
59 resolvedPaths[key] = values;
60 }
61 }
62 currentPaths = resolvedPaths;
63 }
64 // If this config extends another, resolve it first
65 if (tsConfig.extends) {
66 const baseConfigPath = path.resolve(path.dirname(configPath), tsConfig.extends);
67 if (debugViteLogs) console.log('📁 Following extends to:', baseConfigPath);

Callers 1

getTsConfigDataFunction · 0.85

Calls 6

resolveTsConfigChainFunction · 0.85
forEachMethod · 0.80
warnMethod · 0.80
keysMethod · 0.65
logMethod · 0.45
sliceMethod · 0.45

Tested by

no test coverage detected