1// Extract destructured names from dynamic imports like: 2// const {foo, bar} = await import('./path.mjs') 3// const {foo: aliased} = isDevPc ? await import('./path.mjs') : fallback 5// Given an AwaitExpression or ImportExpression node, walk up to find the VariableDeclarator 6// and extract destructured property names from its id pattern 9export const extractDynamicImportNames = (importExprNode: AcornNode, ancestors: AcornNode[]) => { 10 if (!ancestors?.length) return null 12 // Walk up ancestors to find VariableDeclarator 13 // Pattern: VariableDeclarator -> AwaitExpression -> ImportExpression 14 // Or with ternary: VariableDeclarator -> ConditionalExpression -> AwaitExpression -> ImportExpression 16 for (const ancestor of ancestors) { 17 if (ancestor.type === 'VariableDeclarator') { 18 varDeclarator = ancestor 23 if (!varDeclarator?.id) return null 25 if (varDeclarator.id.type === 'ObjectPattern') { 26 const names: string[] = [] 27 for (const prop of varDeclarator.id.properties) { 28 if (prop.type === 'Property' && prop.key?.type === 'Identifier') { 29 // Use the key name (the exported name), not the local alias 30 names.push(prop.key.name) 33 return names.length > 0 ? names : null 36 // Single identifier: const foo = await import('./path.mjs') 37 // This imports the module namespace, similar to: import * as foo from './path.mjs' 38 // Return null to trigger namespace expansion 39 if (varDeclarator.id.type === 'Identifier') { 40 return null // Will fall back to 'dynamic' or namespace expansion