@@ -5,6 +5,7 @@ import { BLANK } from '../../utils/blank';
55import { logIllegalImportReassignment } from '../../utils/logs' ;
66import { PureFunctionKey } from '../../utils/pureFunctions' ;
77import type { NodeRenderOptions , RenderOptions } from '../../utils/renderHelpers' ;
8+ import { markModuleAndImpureDependenciesAsExecuted } from '../../utils/traverseStaticDependencies' ;
89import type { DeoptimizableEntity } from '../DeoptimizableEntity' ;
910import type { HasEffectsContext , InclusionContext } from '../ExecutionContext' ;
1011import type { NodeInteraction , NodeInteractionCalled } from '../NodeInteractions' ;
@@ -220,9 +221,10 @@ export default class Identifier extends NodeBase implements PatternNode {
220221 this . variable instanceof LocalVariable &&
221222 this . variable . kind &&
222223 tdzVariableKinds . has ( this . variable . kind ) &&
223- // we ignore possible TDZs due to circular module dependencies as
224- // otherwise we get many false positives
225- this . variable . module === this . scope . context . module
224+ // We ignore modules that did not receive a treeshaking pass yet as that
225+ // causes many false positives due to circular dependencies or disabled
226+ // moduleSideEffects.
227+ this . variable . module . hasTreeShakingPassStarted
226228 )
227229 ) {
228230 return ( this . isTDZAccess = false ) ;
@@ -241,9 +243,7 @@ export default class Identifier extends NodeBase implements PatternNode {
241243 return ( this . isTDZAccess = true ) ;
242244 }
243245
244- // We ignore the case where the module is not yet executed because
245- // moduleSideEffects are false.
246- if ( ! this . variable . initReached && this . scope . context . module . isExecuted ) {
246+ if ( ! this . variable . initReached ) {
247247 // Either a const/let TDZ violation or
248248 // var use before declaration was encountered.
249249 return ( this . isTDZAccess = true ) ;
@@ -294,6 +294,12 @@ export default class Identifier extends NodeBase implements PatternNode {
294294 protected applyDeoptimizations ( ) : void {
295295 this . deoptimized = true ;
296296 if ( this . variable instanceof LocalVariable ) {
297+ // When accessing a variable from a module without side effects, this
298+ // means we use an export of that module and therefore need to potentially
299+ // include it in the bundle.
300+ if ( ! this . variable . module . isExecuted ) {
301+ markModuleAndImpureDependenciesAsExecuted ( this . variable . module ) ;
302+ }
297303 this . variable . consolidateInitializers ( ) ;
298304 this . scope . context . requestTreeshakingPass ( ) ;
299305 }
0 commit comments