@@ -14,8 +14,12 @@ use rspack_hash::{RspackHash, RspackHashDigest};
1414use rspack_macros:: impl_source_map_config;
1515use rspack_paths:: { ArcPath , Utf8PathBuf } ;
1616use rspack_regex:: RspackRegex ;
17- use rspack_sources:: { BoxSource , ConcatSource , RawStringSource , SourceExt } ;
18- use rspack_util:: { fx_hash:: FxIndexMap , itoa, json_stringify, source_map:: SourceMapKind } ;
17+ use rspack_sources:: { BoxSource , OriginalSource , RawStringSource , SourceExt } ;
18+ use rspack_util:: {
19+ fx_hash:: FxIndexMap ,
20+ itoa, json_stringify,
21+ source_map:: { ModuleSourceMapConfig , SourceMapKind } ,
22+ } ;
1923use rustc_hash:: { FxHashMap as HashMap , FxHashSet as HashSet } ;
2024use swc_core:: atoms:: Atom ;
2125
@@ -324,8 +328,8 @@ impl ContextModule {
324328 . collect ( )
325329 }
326330
327- fn get_source_for_empty_async_context ( & self , compilation : & Compilation ) -> BoxSource {
328- RawStringSource :: from ( formatdoc ! { r#"
331+ fn get_source_for_empty_async_context ( & self , compilation : & Compilation ) -> String {
332+ formatdoc ! { r#"
329333 function webpackEmptyAsyncContext(req) {{
330334 // Here Promise.resolve().then() is used instead of new Promise() to prevent
331335 // uncaught exception popping up in devtools
@@ -342,12 +346,11 @@ impl ContextModule {
342346 "# ,
343347 keys = returning_function( & compilation. options. output. environment, "[]" , "" ) ,
344348 id = json_stringify( self . get_module_id( & compilation. module_ids_artifact) )
345- } )
346- . boxed ( )
349+ }
347350 }
348351
349- fn get_source_for_empty_context ( & self , compilation : & Compilation ) -> BoxSource {
350- RawStringSource :: from ( formatdoc ! { r#"
352+ fn get_source_for_empty_context ( & self , compilation : & Compilation ) -> String {
353+ formatdoc ! { r#"
351354 function webpackEmptyContext(req) {{
352355 var e = new Error("Cannot find module '" + req + "'");
353356 e.code = 'MODULE_NOT_FOUND';
@@ -360,16 +363,15 @@ impl ContextModule {
360363 "# ,
361364 keys = returning_function( & compilation. options. output. environment, "[]" , "" ) ,
362365 id = json_stringify( self . get_module_id( & compilation. module_ids_artifact) )
363- } )
364- . boxed ( )
366+ }
365367 }
366368
367369 #[ inline]
368370 fn get_source_string (
369371 & self ,
370372 compilation : & Compilation ,
371373 code_gen_result : & mut CodeGenerationResult ,
372- ) -> BoxSource {
374+ ) -> String {
373375 match self . options . context_options . mode {
374376 ContextMode :: Lazy => {
375377 if !self . get_blocks ( ) . is_empty ( ) {
@@ -416,7 +418,7 @@ impl ContextModule {
416418 }
417419 }
418420
419- fn get_lazy_source ( & self , compilation : & Compilation ) -> BoxSource {
421+ fn get_lazy_source ( & self , compilation : & Compilation ) -> String {
420422 let module_graph = compilation. get_module_graph ( ) ;
421423 let blocks = self
422424 . get_blocks ( )
@@ -516,7 +518,6 @@ impl ContextModule {
516518 true ,
517519 if short_mode { "invalid" } else { "ids[1]" } ,
518520 ) ;
519- let mut source = ConcatSource :: default ( ) ;
520521 let webpack_async_context = if has_no_chunk {
521522 formatdoc ! { r#"
522523 function webpackAsyncContext(req) {{
@@ -559,7 +560,7 @@ impl ContextModule {
559560 RuntimeGlobals :: HAS_OWN_PROPERTY ,
560561 }
561562 } ;
562- source . add ( RawStringSource :: from ( formatdoc ! { r#"
563+ formatdoc ! { r#"
563564 var map = {map};
564565 {webpack_async_context}
565566 webpackAsyncContext.keys = {keys};
@@ -569,16 +570,15 @@ impl ContextModule {
569570 map = json_stringify( & map) ,
570571 keys = returning_function( & compilation. options. output. environment, "Object.keys(map)" , "" ) ,
571572 id = json_stringify( self . get_module_id( & compilation. module_ids_artifact) )
572- } ) ) ;
573- source. boxed ( )
573+ }
574574 }
575575
576576 fn get_lazy_once_source (
577577 & self ,
578578 compilation : & Compilation ,
579579 block_id : & AsyncDependenciesBlockIdentifier ,
580580 code_gen_result : & mut CodeGenerationResult ,
581- ) -> BoxSource {
581+ ) -> String {
582582 let mg = compilation. get_module_graph ( ) ;
583583 let block = mg. block_by_id_expect ( block_id) ;
584584 let dependencies = block. get_dependencies ( ) ;
@@ -604,7 +604,7 @@ impl ContextModule {
604604 } else {
605605 RuntimeGlobals :: REQUIRE . name ( ) . to_string ( )
606606 } ;
607- let source = formatdoc ! { r#"
607+ formatdoc ! { r#"
608608 var map = {map};
609609 {fake_map_init_statement}
610610
@@ -631,16 +631,15 @@ impl ContextModule {
631631 has_own_property = RuntimeGlobals :: HAS_OWN_PROPERTY ,
632632 keys = returning_function( & compilation. options. output. environment, "Object.keys(map)" , "" ) ,
633633 id = json_stringify( self . get_module_id( & compilation. module_ids_artifact) )
634- } ;
635- RawStringSource :: from ( source) . boxed ( )
634+ }
636635 }
637636
638- fn get_async_weak_source ( & self , compilation : & Compilation ) -> BoxSource {
637+ fn get_async_weak_source ( & self , compilation : & Compilation ) -> String {
639638 let dependencies = self . get_dependencies ( ) ;
640639 let map = self . get_user_request_map ( dependencies, compilation) ;
641640 let fake_map = self . get_fake_map ( dependencies, compilation) ;
642641 let return_module_object = self . get_return_module_object_source ( & fake_map, true , "fakeMap[id]" ) ;
643- let source = formatdoc ! { r#"
642+ formatdoc ! { r#"
644643 var map = {map};
645644 {fake_map_init_statement}
646645
@@ -677,16 +676,15 @@ impl ContextModule {
677676 has_own_property = RuntimeGlobals :: HAS_OWN_PROPERTY ,
678677 keys = returning_function( & compilation. options. output. environment, "Object.keys(map)" , "" ) ,
679678 id = json_stringify( self . get_module_id( & compilation. module_ids_artifact) )
680- } ;
681- RawStringSource :: from ( source) . boxed ( )
679+ }
682680 }
683681
684- fn get_sync_weak_source ( & self , compilation : & Compilation ) -> BoxSource {
682+ fn get_sync_weak_source ( & self , compilation : & Compilation ) -> String {
685683 let dependencies = self . get_dependencies ( ) ;
686684 let map = self . get_user_request_map ( dependencies, compilation) ;
687685 let fake_map = self . get_fake_map ( dependencies, compilation) ;
688686 let return_module_object = self . get_return_module_object_source ( & fake_map, true , "fakeMap[id]" ) ;
689- let source = formatdoc ! { r#"
687+ formatdoc ! { r#"
690688 var map = {map};
691689 {fake_map_init_statement}
692690
@@ -718,11 +716,10 @@ impl ContextModule {
718716 has_own_property = RuntimeGlobals :: HAS_OWN_PROPERTY ,
719717 keys = returning_function( & compilation. options. output. environment, "Object.keys(map)" , "" ) ,
720718 id = json_stringify( self . get_module_id( & compilation. module_ids_artifact) )
721- } ;
722- RawStringSource :: from ( source) . boxed ( )
719+ }
723720 }
724721
725- fn get_eager_source ( & self , compilation : & Compilation ) -> BoxSource {
722+ fn get_eager_source ( & self , compilation : & Compilation ) -> String {
726723 let dependencies = self . get_dependencies ( ) ;
727724 let map = self . get_user_request_map ( dependencies, compilation) ;
728725 let fake_map = self . get_fake_map ( dependencies, compilation) ;
@@ -740,7 +737,7 @@ impl ContextModule {
740737 } else {
741738 RuntimeGlobals :: REQUIRE . name ( ) . to_string ( )
742739 } ;
743- let source = formatdoc ! { r#"
740+ formatdoc ! { r#"
744741 var map = {map};
745742 {fake_map_init_statement}
746743
@@ -769,17 +766,16 @@ impl ContextModule {
769766 has_own_property = RuntimeGlobals :: HAS_OWN_PROPERTY ,
770767 keys = returning_function( & compilation. options. output. environment, "Object.keys(map)" , "" ) ,
771768 id = json_stringify( self . get_module_id( & compilation. module_ids_artifact) )
772- } ;
773- RawStringSource :: from ( source) . boxed ( )
769+ }
774770 }
775771
776- fn get_sync_source ( & self , compilation : & Compilation ) -> BoxSource {
772+ fn get_sync_source ( & self , compilation : & Compilation ) -> String {
777773 let dependencies = self . get_dependencies ( ) ;
778774 let map = self . get_user_request_map ( dependencies, compilation) ;
779775 let fake_map = self . get_fake_map ( dependencies, compilation) ;
780776 let return_module_object =
781777 self . get_return_module_object_source ( & fake_map, false , "fakeMap[id]" ) ;
782- let source = formatdoc ! { r#"
778+ formatdoc ! { r#"
783779 var map = {map};
784780 {fake_map_init_statement}
785781
@@ -806,8 +802,23 @@ impl ContextModule {
806802 fake_map_init_statement = self . get_fake_map_init_statement( & fake_map) ,
807803 has_own_property = RuntimeGlobals :: HAS_OWN_PROPERTY ,
808804 id = json_stringify( self . get_module_id( & compilation. module_ids_artifact) )
809- } ;
810- RawStringSource :: from ( source) . boxed ( )
805+ }
806+ }
807+
808+ fn get_source ( & self , source_string : String , compilation : & Compilation ) -> BoxSource {
809+ let source_map_kind = self . get_source_map_kind ( ) ;
810+ if source_map_kind. enabled ( ) {
811+ OriginalSource :: new (
812+ source_string,
813+ format ! (
814+ "webpack://{}" ,
815+ contextify( & compilation. options. context, self . identifier. as_str( ) , )
816+ ) ,
817+ )
818+ . boxed ( )
819+ } else {
820+ RawStringSource :: from ( source_string) . boxed ( )
821+ }
811822 }
812823}
813824
@@ -991,7 +1002,10 @@ impl Module for ContextModule {
9911002 _: Option < ConcatenationScope > ,
9921003 ) -> Result < CodeGenerationResult > {
9931004 let mut code_generation_result = CodeGenerationResult :: default ( ) ;
994- let source = self . get_source_string ( compilation, & mut code_generation_result) ;
1005+ let source = self . get_source (
1006+ self . get_source_string ( compilation, & mut code_generation_result) ,
1007+ compilation,
1008+ ) ;
9951009 code_generation_result. add ( SourceType :: JavaScript , source) ;
9961010 let mut all_deps = self . get_dependencies ( ) . to_vec ( ) ;
9971011 let module_graph = compilation. get_module_graph ( ) ;
0 commit comments