@@ -592,6 +592,7 @@ impl ContextModule {
592592 ( user_request, value)
593593 } )
594594 . collect :: < HashMap < _ , _ > > ( ) ;
595+
595596 let chunks_position = if has_fake_map { 2 } else { 1 } ;
596597 let async_deps_position = chunks_position + 1 ;
597598 let request_prefix = if has_no_chunk {
@@ -621,48 +622,57 @@ impl ContextModule {
621622 if short_mode { "invalid" } else { "ids[1]" } ,
622623 runtime_template,
623624 ) ;
625+
626+ let has_own_property =
627+ runtime_template. render_runtime_globals ( & RuntimeGlobals :: HAS_OWN_PROPERTY ) ;
624628 let async_context = if has_no_chunk {
629+ let then_function = runtime_template. basic_function (
630+ "" ,
631+ & formatdoc ! {
632+ r#"if(!{has_own_property}(map, req)) {{
633+ var e = new Error("Cannot find module '" + req + "'");
634+ e.code = 'MODULE_NOT_FOUND';
635+ throw e;
636+ }}
637+
638+ {}
639+ return {return_module_object};"# ,
640+ if short_mode {
641+ "var id = map[req];"
642+ } else {
643+ "var ids = map[req], id = ids[0];"
644+ }
645+ } ,
646+ ) ;
625647 formatdoc ! { r#"
626648 function __rspack_async_context(req) {{
627- return Promise.resolve().then(function() {{
628- if(!{}(map, req)) {{
629- var e = new Error("Cannot find module '" + req + "'");
630- e.code = 'MODULE_NOT_FOUND';
631- throw e;
632- }}
633-
634- {}
635- return {return_module_object};
636- }});
649+ return Promise.resolve().then({then_function});
637650 }}
638- "# ,
639- runtime_template. render_runtime_globals( & RuntimeGlobals :: HAS_OWN_PROPERTY ) ,
640- if short_mode {
641- "var id = map[req];"
642- } else {
643- "var ids = map[req], id = ids[0];"
644- }
645- }
651+ "# }
646652 } else {
653+ let then_function = runtime_template. returning_function ( & return_module_object, "" ) ;
654+ let module_not_found = runtime_template. basic_function (
655+ "" ,
656+ & formatdoc ! {
657+ r#"var e = new Error("Cannot find module '" + req + "'");
658+ e.code = 'MODULE_NOT_FOUND';
659+ throw e;"#
660+ } ,
661+ ) ;
647662 formatdoc ! { r#"
648663 function __rspack_async_context(req) {{
649664 if(!{}(map, req)) {{
650- return Promise.resolve().then(function() {{
651- var e = new Error("Cannot find module '" + req + "'");
652- e.code = 'MODULE_NOT_FOUND';
653- throw e;
654- }});
665+ return Promise.resolve().then({module_not_found});
655666 }}
656667
657668 var ids = map[req], id = ids[0];
658- return {request_prefix}.then(function() {{
659- return {return_module_object};
660- }});
669+ return {request_prefix}.then({then_function});
661670 }}
662671 "# ,
663672 runtime_template. render_runtime_globals( & RuntimeGlobals :: HAS_OWN_PROPERTY ) ,
664673 }
665674 } ;
675+
666676 formatdoc ! { r#"
667677 var map = {map};
668678 {async_context}
@@ -697,13 +707,30 @@ impl ContextModule {
697707 . is_defer ( )
698708 . then ( || self . get_module_deferred_async_deps_map ( dependencies, compilation) ) ;
699709
700- let then_function = formatdoc ! { r#"
701- function(id) {{
702- return {};
703- }}
704- "# ,
705- self . get_return_module_object_source( & fake_map, true , async_deps_map. is_some( ) . then( || "asyncDepsMap[id]" . to_string( ) ) , "fakeMap[id]" , runtime_template) ,
706- } ;
710+ let return_module_object_source = self . get_return_module_object_source (
711+ & fake_map,
712+ true ,
713+ async_deps_map
714+ . is_some ( )
715+ . then ( || "asyncDepsMap[id]" . to_string ( ) ) ,
716+ "fakeMap[id]" ,
717+ runtime_template,
718+ ) ;
719+ let then_function = runtime_template. returning_function ( & return_module_object_source, "id" ) ;
720+
721+ let has_own_property =
722+ runtime_template. render_runtime_globals ( & RuntimeGlobals :: HAS_OWN_PROPERTY ) ;
723+ let module_not_found = runtime_template. basic_function (
724+ "" ,
725+ & formatdoc ! {
726+ r#"if(!{has_own_property}(map, req)) {{
727+ var e = new Error("Cannot find module '" + req + "'");
728+ e.code = 'MODULE_NOT_FOUND';
729+ throw e;
730+ }}
731+ return map[req];"#
732+ } ,
733+ ) ;
707734
708735 formatdoc ! { r#"
709736 var map = {map};
@@ -714,14 +741,7 @@ impl ContextModule {
714741 return __rspack_async_context_resolve(req).then({then_function});
715742 }}
716743 function __rspack_async_context_resolve(req) {{
717- return {promise}.then(function() {{
718- if(!{has_own_property}(map, req)) {{
719- var e = new Error("Cannot find module '" + req + "'");
720- e.code = 'MODULE_NOT_FOUND';
721- throw e;
722- }}
723- return map[req];
724- }})
744+ return {promise}.then({module_not_found});
725745 }}
726746 __rspack_async_context.keys = {keys};
727747 __rspack_async_context.resolve = __rspack_async_context_resolve;
@@ -732,7 +752,6 @@ impl ContextModule {
732752 map = json_stringify( & map) ,
733753 fake_map_init_statement = self . get_fake_map_init_statement( & fake_map) ,
734754 async_deps_map_init_statement = self . get_module_deferred_async_deps_map_init_statement( async_deps_map. as_ref( ) ) ,
735- has_own_property = runtime_template. render_runtime_globals( & RuntimeGlobals :: HAS_OWN_PROPERTY ) ,
736755 keys = runtime_template. returning_function( "Object.keys(map)" , "" ) ,
737756 id = json_stringify( self . get_module_id( & compilation. module_ids_artifact) )
738757 }
@@ -753,6 +772,7 @@ impl ContextModule {
753772 . unwrap_or_default ( )
754773 . is_defer ( )
755774 . then ( || self . get_module_deferred_async_deps_map ( dependencies, compilation) ) ;
775+
756776 let return_module_object = self . get_return_module_object_source (
757777 & fake_map,
758778 true ,
@@ -762,32 +782,45 @@ impl ContextModule {
762782 "fakeMap[id]" ,
763783 runtime_template,
764784 ) ;
785+ let module_factories =
786+ runtime_template. render_runtime_globals ( & RuntimeGlobals :: MODULE_FACTORIES ) ;
787+ let then_function = runtime_template. basic_function (
788+ "id" ,
789+ & formatdoc ! {
790+ r#"if(!{module_factories}[id]) {{
791+ var e = new Error("Module '" + req + "' ('" + id + "') is not available (weak dependency)");
792+ e.code = 'MODULE_NOT_FOUND';
793+ throw e;
794+ }}
795+ return {return_module_object};"#
796+ } ,
797+ ) ;
798+ let has_own_property =
799+ runtime_template. render_runtime_globals ( & RuntimeGlobals :: HAS_OWN_PROPERTY ) ;
800+ let module_not_found = runtime_template. basic_function (
801+ "" ,
802+ & formatdoc ! {
803+ r#"if(!{has_own_property}(map, req)) {{
804+ var e = new Error("Cannot find module '" + req + "'");
805+ e.code = 'MODULE_NOT_FOUND';
806+ throw e;
807+ }}
808+ return map[req];"#
809+ } ,
810+ ) ;
811+
765812 formatdoc ! { r#"
766813 var map = {map};
767814 {fake_map_init_statement}
768815 {async_deps_map_init_statement}
769816
770817 function __rspack_async_context(req) {{
771- return __rspack_async_context_resolve(req).then(function(id) {{
772- if(!{module_factories}[id]) {{
773- var e = new Error("Module '" + req + "' ('" + id + "') is not available (weak dependency)");
774- e.code = 'MODULE_NOT_FOUND';
775- throw e;
776- }}
777- return {return_module_object};
778- }});
818+ return __rspack_async_context_resolve(req).then({then_function});
779819 }}
780820 function __rspack_async_context_resolve(req) {{
781821 // Here Promise.resolve().then() is used instead of new Promise() to prevent
782822 // uncaught exception popping up in devtools
783- return Promise.resolve().then(function() {{
784- if(!{has_own_property}(map, req)) {{
785- var e = new Error("Cannot find module '" + req + "'");
786- e.code = 'MODULE_NOT_FOUND';
787- throw e;
788- }}
789- return map[req];
790- }})
823+ return Promise.resolve().then({module_not_found});
791824 }}
792825 __rspack_async_context.keys = {keys};
793826 __rspack_async_context.resolve = __rspack_async_context_resolve;
@@ -798,8 +831,6 @@ impl ContextModule {
798831 map = json_stringify( & map) ,
799832 fake_map_init_statement = self . get_fake_map_init_statement( & fake_map) ,
800833 async_deps_map_init_statement = self . get_module_deferred_async_deps_map_init_statement( async_deps_map. as_ref( ) ) ,
801- module_factories = runtime_template. render_runtime_globals( & RuntimeGlobals :: MODULE_FACTORIES ) ,
802- has_own_property = runtime_template. render_runtime_globals( & RuntimeGlobals :: HAS_OWN_PROPERTY ) ,
803834 keys = runtime_template. returning_function( "Object.keys(map)" , "" ) ,
804835 id = json_stringify( self . get_module_id( & compilation. module_ids_artifact) )
805836 }
@@ -866,13 +897,30 @@ impl ContextModule {
866897 . unwrap_or_default ( )
867898 . is_defer ( )
868899 . then ( || self . get_module_deferred_async_deps_map ( dependencies, compilation) ) ;
869- let then_function = formatdoc ! { r#"
870- function(id) {{
871- return {};
872- }}
873- "# ,
874- self . get_return_module_object_source( & fake_map, true , async_deps_map. is_some( ) . then( || "asyncDepsMap[id]" . to_string( ) ) , "fakeMap[id]" , runtime_template) ,
875- } ;
900+ let return_module_object_source = self . get_return_module_object_source (
901+ & fake_map,
902+ true ,
903+ async_deps_map
904+ . is_some ( )
905+ . then ( || "asyncDepsMap[id]" . to_string ( ) ) ,
906+ "fakeMap[id]" ,
907+ runtime_template,
908+ ) ;
909+ let then_function = runtime_template. returning_function ( & return_module_object_source, "id" ) ;
910+ let has_own_property =
911+ runtime_template. render_runtime_globals ( & RuntimeGlobals :: HAS_OWN_PROPERTY ) ;
912+ let module_not_found = runtime_template. basic_function (
913+ "" ,
914+ & formatdoc ! {
915+ r#"if(!{has_own_property}(map, req)) {{
916+ var e = new Error("Cannot find module '" + req + "'");
917+ e.code = 'MODULE_NOT_FOUND';
918+ throw e;
919+ }}
920+ return map[req];"#
921+ } ,
922+ ) ;
923+
876924 formatdoc ! { r#"
877925 var map = {map};
878926 {fake_map_init_statement}
@@ -884,14 +932,7 @@ impl ContextModule {
884932 function __rspack_async_context_resolve(req) {{
885933 // Here Promise.resolve().then() is used instead of new Promise() to prevent
886934 // uncaught exception popping up in devtools
887- return Promise.resolve().then(function() {{
888- if(!{has_own_property}(map, req)) {{
889- var e = new Error("Cannot find module '" + req + "'");
890- e.code = 'MODULE_NOT_FOUND';
891- throw e;
892- }}
893- return map[req];
894- }})
935+ return Promise.resolve().then({module_not_found});
895936 }}
896937 __rspack_async_context.keys = {keys};
897938 __rspack_async_context.resolve = __rspack_async_context_resolve;
@@ -902,7 +943,6 @@ impl ContextModule {
902943 map = json_stringify( & map) ,
903944 fake_map_init_statement = self . get_fake_map_init_statement( & fake_map) ,
904945 async_deps_map_init_statement = self . get_module_deferred_async_deps_map_init_statement( async_deps_map. as_ref( ) ) ,
905- has_own_property = runtime_template. render_runtime_globals( & RuntimeGlobals :: HAS_OWN_PROPERTY ) ,
906946 keys = runtime_template. returning_function( "Object.keys(map)" , "" ) ,
907947 id = json_stringify( self . get_module_id( & compilation. module_ids_artifact) )
908948 }
@@ -934,9 +974,7 @@ impl ContextModule {
934974 }}
935975 return map[req];
936976 }}
937- __rspack_context.keys = function webpackContextKeys() {{
938- return Object.keys(map);
939- }};
977+ __rspack_context.keys = {keys};
940978 __rspack_context.resolve = __rspack_context_resolve;
941979 {module}.exports = __rspack_context;
942980 __rspack_context.id = {id};
@@ -945,6 +983,7 @@ impl ContextModule {
945983 map = json_stringify( & map) ,
946984 fake_map_init_statement = self . get_fake_map_init_statement( & fake_map) ,
947985 has_own_property = runtime_template. render_runtime_globals( & RuntimeGlobals :: HAS_OWN_PROPERTY ) ,
986+ keys = runtime_template. returning_function( "Object.keys(map)" , "" ) ,
948987 id = json_stringify( self . get_module_id( & compilation. module_ids_artifact) )
949988 }
950989 }
0 commit comments