11import type { Program } from 'oxc-parser' ;
2- import { collectPropertyValues , hasImportSpecifier } from '../../typescript/ast-helpers.ts' ;
2+ import {
3+ collectPropertyValues ,
4+ findProperty ,
5+ getPropertyKey ,
6+ getStringValue ,
7+ hasImportSpecifier ,
8+ } from '../../typescript/ast-helpers.ts' ;
39
410export const getSrcDir = ( program : Program ) : string => {
511 const values = collectPropertyValues ( program , 'srcDir' ) ;
@@ -8,3 +14,43 @@ export const getSrcDir = (program: Program): string => {
814
915export const usesPassthroughImageService = ( program : Program ) =>
1016 hasImportSpecifier ( program , 'astro/config' , 'passthroughImageService' ) ;
17+
18+ // First string literal reachable via CallExpression/NewExpression arguments. Handles common
19+ // config patterns: `'./src'`, `path.resolve('./src')`, `path.resolve(__dirname, 'src')`,
20+ // `fileURLToPath(new URL('./src', import.meta.url))`.
21+ const findFirstStringArg = ( node : any ) : string | undefined => {
22+ const literal = getStringValue ( node ) ;
23+ if ( literal ) return literal ;
24+ if ( node ?. type === 'CallExpression' || node ?. type === 'NewExpression' ) {
25+ for ( const arg of node . arguments ?? [ ] ) {
26+ const found = findFirstStringArg ( arg ) ;
27+ if ( found ) return found ;
28+ }
29+ }
30+ } ;
31+
32+ // Extract `vite.resolve.alias` from the default-exported config object.
33+ // Supports `export default { ... }` and `export default defineConfig({ ... })`.
34+ export const getViteAliases = ( program : Program ) : Record < string , string > => {
35+ const aliases : Record < string , string > = { } ;
36+ for ( const node of ( program as unknown as { body : any [ ] } ) . body ?? [ ] ) {
37+ if ( node . type !== 'ExportDefaultDeclaration' ) continue ;
38+ const decl = node . declaration ;
39+ const root =
40+ decl ?. type === 'ObjectExpression'
41+ ? decl
42+ : decl ?. type === 'CallExpression' && decl . arguments ?. [ 0 ] ?. type === 'ObjectExpression'
43+ ? decl . arguments [ 0 ]
44+ : undefined ;
45+ const aliasNode = findProperty ( findProperty ( findProperty ( root , 'vite' ) , 'resolve' ) , 'alias' ) ;
46+ if ( aliasNode ?. type !== 'ObjectExpression' ) continue ;
47+ for ( const prop of aliasNode . properties ?? [ ] ) {
48+ if ( prop . type !== 'Property' ) continue ;
49+ const key = getPropertyKey ( prop ) ;
50+ if ( ! key ) continue ;
51+ const raw = findFirstStringArg ( prop . value ) ;
52+ if ( raw ) aliases [ key ] = raw ;
53+ }
54+ }
55+ return aliases ;
56+ } ;
0 commit comments