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

Function resolveTsConfigChain

packages/vite/helpers/ts-config-paths.ts:16–86  ·  view source on GitHub ↗
(configPath, visitedPaths = new Set())

Source from the content-addressed store, hash-verified

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);
68 if (fs.existsSync(baseConfigPath)) {
69 try {
70 const baseResult = resolveTsConfigChain(baseConfigPath, visitedPaths);
71 // Base config comes first, then override with current
72 const mergedPaths = { ...baseResult.paths, ...currentPaths };
73 // Use current baseUrl if specified, otherwise inherit from base

Callers 1

getTsConfigPathsFunction · 0.85

Calls 8

replaceMethod · 0.80
includesMethod · 0.80
isAbsoluteMethod · 0.80
resolveMethod · 0.80
addMethod · 0.65
parseMethod · 0.65
mapMethod · 0.65
logMethod · 0.45

Tested by

no test coverage detected