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

Function createTsConfigAliases

packages/vite/helpers/ts-config-paths.ts:107–215  ·  view source on GitHub ↗
(opts: { paths: any; baseUrl: string; platform: string; verbose?: boolean })

Source from the content-addressed store, hash-verified

105
106// Function to create TypeScript aliases with platform support
107function createTsConfigAliases(opts: { paths: any; baseUrl: string; platform: string; verbose?: boolean }) {
108 const aliases = [];
109
110 // Process patterns in order: wildcards first, then exact matches
111 const sortedPatterns = Object.entries(opts.paths).sort(([a], [b]) => {
112 // Wildcards (with *) come first
113 const aHasWildcard = a.includes('*');
114 const bHasWildcard = b.includes('*');
115 if (aHasWildcard && !bHasWildcard) return -1;
116 if (!aHasWildcard && bHasWildcard) return 1;
117 // Within same type, longer patterns first (more specific)
118 return b.length - a.length;
119 });
120
121 for (const [pattern, destinations] of sortedPatterns) {
122 if (Array.isArray(destinations) && destinations.length > 0) {
123 if (pattern.includes('*')) {
124 // Handle wildcard patterns (like "@scope/plugins/*")
125 const aliasKey = pattern.replace(/\/\*$/, '');
126 const destination = destinations[0].replace(/\/\*$/, '');
127
128 // Check if destination is already absolute (resolved by tsconfig chain)
129 const resolvedDestination = path.isAbsolute(destination) ? destination : path.resolve(projectRoot, opts.baseUrl, destination);
130
131 // console.log(
132 // `📁 Creating wildcard alias: ${aliasKey} -> ${resolvedDestination}`,
133 // );
134
135 aliases.push({
136 find: new RegExp(`^${aliasKey.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?:/(.*))?$`),
137 replacement: (match, subpath) => {
138 const fullPath = subpath ? path.join(resolvedDestination, subpath) : resolvedDestination;
139 if (opts.verbose) {
140 console.log(`📁 TypeScript wildcard alias: ${match} -> ${fullPath}`);
141 }
142
143 // Check if this resolves to a directory, and if so, try to find index files
144 if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
145 // Try platform-specific index files first
146 const platformIndexPatterns = [`index.${opts.platform}.ts`, `index.${opts.platform}.js`, `index.${opts.platform}.mjs`];
147 for (const indexFile of platformIndexPatterns) {
148 const indexPath = path.join(fullPath, indexFile);
149 if (fs.existsSync(indexPath)) {
150 if (opts.verbose) {
151 console.log(`📁 Found platform-specific directory index: ${indexPath}`);
152 }
153 return indexPath;
154 }
155 }
156
157 // Try standard index files
158 const indexPatterns = ['index.ts', 'index.js', 'index.mjs'];
159 for (const indexFile of indexPatterns) {
160 const indexPath = path.join(fullPath, indexFile);
161 if (fs.existsSync(indexPath)) {
162 if (opts.verbose) {
163 console.log(`📁 Found directory index: ${indexPath}`);
164 }

Callers 1

getTsConfigDataFunction · 0.85

Calls 8

sortMethod · 0.80
includesMethod · 0.80
replaceMethod · 0.80
isAbsoluteMethod · 0.80
resolveMethod · 0.80
joinMethod · 0.80
pushMethod · 0.45
logMethod · 0.45

Tested by

no test coverage detected