@@ -85,6 +85,7 @@ global[`${__METRO_GLOBAL_PREFIX__}__d`] = (define: DefineFn);
8585global . __c = clear ;
8686global . __registerSegment = registerSegment ;
8787global . __resetAllModules = resetAllModules ;
88+ global . __clearModule = clearModule ;
8889
8990var modules = clear ( ) ;
9091
@@ -95,6 +96,14 @@ function resetAllModules() {
9596 } ) ;
9697}
9798
99+ function clearModule ( moduleId : ModuleID ) {
100+ if ( ! modules . has ( moduleId ) ) {
101+ return ;
102+ }
103+
104+ modules . delete ( moduleId ) ;
105+ }
106+
98107// Don't use a Symbol here, it would pull in an extra polyfill with all sorts of
99108// additional stuff (e.g. Array.from).
100109const EMPTY = { } ;
@@ -223,15 +232,6 @@ function metroRequire(
223232
224233 const module = modules . get ( moduleIdReallyIsNumber ) ;
225234
226- // Optionally return a lazy proxy that triggers evaluation on first access.
227- if (
228- module &&
229- ! module . isInitialized &&
230- shouldUseLazyModuleProxy ( moduleIdReallyIsNumber , module )
231- ) {
232- return createModuleEvaluationProxy ( moduleIdReallyIsNumber ) ;
233- }
234-
235235 return module && module . isInitialized
236236 ? module . publicModule . exports
237237 : guardedLoadModule ( moduleIdReallyIsNumber , module ) ;
@@ -398,92 +398,6 @@ function packModuleId(value: {
398398}
399399metroRequire . packModuleId = packModuleId ;
400400
401- // ------------------------------
402- // Lazy evaluation via Proxy (opt-in)
403- // ------------------------------
404- const LAZY_REQUIRE_FLAG_KEY =
405- __METRO_GLOBAL_PREFIX__ + '__LAZY_REQUIRE_BY_ACCESS' ;
406- const LAZY_REQUIRE_ALLOW_DEV_KEY =
407- __METRO_GLOBAL_PREFIX__ + '__LAZY_REQUIRE_ALLOW_DEV' ;
408- const LAZY_REQUIRE_WHITELIST_KEY =
409- __METRO_GLOBAL_PREFIX__ + '__LAZY_REQUIRE_WHITELIST' ;
410-
411- function isLazyRequireEnabled ( ) : boolean {
412- // const enabled = global[LAZY_REQUIRE_FLAG_KEY];
413- // if (!enabled) {
414- // return false;
415- // }
416- // // In DEV, require explicit opt-in due to HMR/export inspection.
417- // return __DEV__ ? !!global[LAZY_REQUIRE_ALLOW_DEV_KEY] : true;
418- return true ;
419- }
420-
421- function isModuleWhitelisted ( moduleId : ModuleID ) : boolean {
422- const list = global [ LAZY_REQUIRE_WHITELIST_KEY ] ;
423- if ( list == null ) {
424- // No whitelist provided -> treat as disabled for safety unless explicitly enabled
425- // at call site (we still require shouldUseLazyModuleProxy to decide).
426- return false ;
427- }
428- return Array . isArray ( list ) && list . indexOf ( moduleId ) !== - 1 ;
429- }
430-
431- function shouldUseLazyModuleProxy (
432- moduleId : ModuleID ,
433- module : ?ModuleDefinition
434- ) : boolean {
435- if ( ! isLazyRequireEnabled ( ) ) {
436- return false ;
437- }
438- if ( ! module || module . isInitialized ) {
439- return false ;
440- }
441- // Only enable for explicitly whitelisted modules to avoid changing semantics
442- // of side-effect-only requires.
443- return isModuleWhitelisted ( moduleId ) ;
444- }
445-
446- function createModuleEvaluationProxy ( moduleId : ModuleID ) : Exports {
447- let evaluated = false ;
448- const ensure = ( ) : Exports => {
449- if ( ! evaluated ) {
450- // Evaluate the module using the same guarded path used by metroRequire.
451- // Pass the current (possibly undefined) ModuleDefinition for better error messages.
452- const existing = modules . get ( moduleId ) ;
453- // guardedLoadModule will throw appropriately if unknown or failing.
454- // It will also set module.isInitialized and publicModule.exports.
455- // $FlowFixMe[incompatible-call]
456- guardedLoadModule ( moduleId , existing ) ;
457- console . log ( 'evaluated module' , moduleId ) ;
458- evaluated = true ;
459- }
460- // $FlowFixMe[incompatible-type]
461- const initializedModule : ModuleDefinition | void = modules . get ( moduleId ) ;
462- // $FlowFixMe[incompatible-use]
463- return initializedModule
464- ? initializedModule . publicModule . exports
465- : undefined ;
466- } ;
467-
468- // Use an object target since CommonJS exports are objects in Metro.
469- const target = { } ;
470- return new Proxy ( target , {
471- get : ( _t , prop ) => Reflect . get ( ensure ( ) , prop ) ,
472- set : ( _t , prop , value ) => Reflect . set ( ensure ( ) , prop , value ) ,
473- has : ( _t , prop ) => Reflect . has ( ensure ( ) , prop ) ,
474- ownKeys : ( ) => Reflect . ownKeys ( ensure ( ) ) ,
475- getOwnPropertyDescriptor : ( _t , prop ) =>
476- Reflect . getOwnPropertyDescriptor ( ensure ( ) , prop ) ,
477- getPrototypeOf : ( ) => Reflect . getPrototypeOf ( ensure ( ) ) ,
478- setPrototypeOf : ( _t , proto ) => Reflect . setPrototypeOf ( ensure ( ) , proto ) ,
479- isExtensible : ( ) => Reflect . isExtensible ( ensure ( ) ) ,
480- preventExtensions : ( ) => Reflect . preventExtensions ( ensure ( ) ) ,
481- defineProperty : ( _t , prop , desc ) =>
482- Reflect . defineProperty ( ensure ( ) , prop , desc ) ,
483- deleteProperty : ( _t , prop ) => Reflect . deleteProperty ( ensure ( ) , prop ) ,
484- } ) ;
485- }
486-
487401const moduleDefinersBySegmentID : Array < ?ModuleDefiner > = [ ] ;
488402const definingSegmentByModuleID : Map < ModuleID , number > = new Map ( ) ;
489403
0 commit comments