@@ -41,6 +41,7 @@ import {
4141 UNKNOWN_RETURN_EXPRESSION ,
4242 UnknownValue
4343} from './shared/Expression' ;
44+ import type { ChainElement } from './shared/Node' ;
4445import { type ExpressionNode , type IncludeChildren , NodeBase } from './shared/Node' ;
4546
4647// To avoid infinite recursions
@@ -89,7 +90,10 @@ function getStringFromPath(path: PathWithPositions): string {
8990 return pathString ;
9091}
9192
92- export default class MemberExpression extends NodeBase implements DeoptimizableEntity {
93+ export default class MemberExpression
94+ extends NodeBase
95+ implements DeoptimizableEntity , ChainElement
96+ {
9397 declare computed : boolean ;
9498 declare object : ExpressionNode | Super ;
9599 declare optional : boolean ;
@@ -108,7 +112,7 @@ export default class MemberExpression extends NodeBase implements DeoptimizableE
108112 this . bound = true ;
109113 const path = getPathIfNotComputed ( this ) ;
110114 const baseVariable = path && this . scope . findVariable ( path [ 0 ] . key ) ;
111- if ( baseVariable && baseVariable . isNamespace ) {
115+ if ( baseVariable ? .isNamespace ) {
112116 const resolvedVariable = resolveNamespaceVariables (
113117 baseVariable ,
114118 path ! . slice ( 1 ) ,
@@ -296,6 +300,16 @@ export default class MemberExpression extends NodeBase implements DeoptimizableE
296300 this . accessInteraction = { thisArg : this . object , type : INTERACTION_ACCESSED } ;
297301 }
298302
303+ isSkippedAsOptional ( origin : DeoptimizableEntity ) : boolean {
304+ return (
305+ ! this . variable &&
306+ ! this . isUndefined &&
307+ ( ( this . object as ExpressionNode ) . isSkippedAsOptional ?.( origin ) ||
308+ ( this . optional &&
309+ this . object . getLiteralValueAtPath ( EMPTY_PATH , SHARED_RECURSION_TRACKER , origin ) == null ) )
310+ ) ;
311+ }
312+
299313 render (
300314 code : MagicString ,
301315 options : RenderOptions ,
@@ -440,9 +454,12 @@ function resolveNamespaceVariables(
440454 const exportName = path [ 0 ] . key ;
441455 const variable = ( baseVariable as NamespaceVariable ) . context . traceExport ( exportName ) ;
442456 if ( ! variable ) {
443- const fileName = ( baseVariable as NamespaceVariable ) . context . fileName ;
444- astContext . warn ( errorMissingExport ( exportName , astContext . module . id , fileName ) , path [ 0 ] . pos ) ;
445- return 'undefined' ;
457+ if ( path . length === 1 ) {
458+ const fileName = ( baseVariable as NamespaceVariable ) . context . fileName ;
459+ astContext . warn ( errorMissingExport ( exportName , astContext . module . id , fileName ) , path [ 0 ] . pos ) ;
460+ return 'undefined' ;
461+ }
462+ return null ;
446463 }
447464 return resolveNamespaceVariables ( variable , path . slice ( 1 ) , astContext ) ;
448465}
0 commit comments