@@ -11,10 +11,10 @@ use rspack_core::{
1111 rspack_sources:: { BoxSource , CachedSource , RawBufferSource , RawStringSource , SourceExt } ,
1212 AssetGeneratorDataUrl , AssetGeneratorDataUrlFnCtx , AssetInfo , AssetParserDataUrl ,
1313 BuildMetaDefaultObject , BuildMetaExportsType , ChunkGraph , ChunkUkey , CodeGenerationDataAssetInfo ,
14- CodeGenerationDataFilename , CodeGenerationDataUrl , Compilation , CompilationRenderManifest ,
15- CompilerOptions , Filename , GenerateContext , GeneratorOptions , LocalFilenameFn , Module ,
16- ModuleGraph , NormalModule , ParseContext , ParserAndGenerator , PathData , Plugin , PublicPath ,
17- RenderManifestEntry , ResourceData , RuntimeGlobals , RuntimeSpec , SourceType ,
14+ CodeGenerationDataFilename , CodeGenerationDataUrl , CodeGenerationPublicPathAutoMark , Compilation ,
15+ CompilationRenderManifest , CompilerOptions , Filename , GenerateContext , GeneratorOptions ,
16+ LocalFilenameFn , Module , ModuleGraph , NormalModule , ParseContext , ParserAndGenerator , PathData ,
17+ Plugin , PublicPath , RenderManifestEntry , ResourceData , RuntimeGlobals , RuntimeSpec , SourceType ,
1818 NAMESPACE_OBJECT_EXPORT ,
1919} ;
2020use rspack_error:: { error, Diagnostic , IntoTWithDiagnosticArray , Result } ;
@@ -459,6 +459,9 @@ impl ParserAndGenerator for AssetParserAndGenerator {
459459 . expect ( "module should be a NormalModule in AssetParserAndGenerator" ) ;
460460 let module_generator_options = normal_module. get_generator_options ( ) ;
461461
462+ let ( experimental_lib_preserve_import, experimental_lib_re_export) =
463+ self . get_experimental_lib_options ( module_generator_options) ?;
464+
462465 let result = match generate_context. requested_source_type {
463466 SourceType :: JavaScript => {
464467 let exported_content = if parsed_asset_config. is_inline ( ) {
@@ -520,6 +523,16 @@ impl ParserAndGenerator for AssetParserAndGenerator {
520523 }
521524 PublicPath :: Auto => public_path. render ( compilation, & filename) ,
522525 } ;
526+
527+ if experimental_lib_preserve_import || experimental_lib_re_export {
528+ let place_holder = "__webpack_auto_public_path__" ;
529+ dbg ! ( place_holder) ;
530+ serde_json:: to_string ( & format ! ( "{place_holder}{original_filename}" ) ) ;
531+ generate_context
532+ . data
533+ . insert ( CodeGenerationPublicPathAutoMark ( true ) ) ;
534+ }
535+
523536 serde_json:: to_string ( & format ! ( "{public_path}{original_filename}" ) )
524537 . map_err ( |e| error ! ( e. to_string( ) ) ) ?
525538 } else {
@@ -554,65 +567,50 @@ impl ParserAndGenerator for AssetParserAndGenerator {
554567 unreachable ! ( )
555568 } ;
556569
557- let ( experimental_lib_preserve_import, experimental_lib_re_export) =
558- self . get_experimental_lib_options ( module_generator_options) ?;
559-
560570 if experimental_lib_preserve_import || experimental_lib_re_export {
561- if let Some ( ref mut scope ) = generate_context . concatenation_scope {
562- let is_module = compilation . options . output . module ;
563- if parsed_asset_config . is_resource ( ) {
571+ let is_module = compilation . options . output . module ;
572+ if is_module {
573+ if let Some ( ref mut scope ) = generate_context . concatenation_scope {
564574 scope. register_namespace_export ( NAMESPACE_OBJECT_EXPORT ) ;
565575 if experimental_lib_re_export {
566- if is_module {
567- return Ok (
568- RawStringSource :: from ( format ! (
569- r#"import {NAMESPACE_OBJECT_EXPORT} from {exported_content};
570- export default {NAMESPACE_OBJECT_EXPORT};"#
571- ) )
572- . boxed ( ) ,
573- ) ;
574- } else {
575- return Ok (
576- RawStringSource :: from ( format ! (
577- r#"module.exports = require({NAMESPACE_OBJECT_EXPORT});"#
578- ) )
579- . boxed ( ) ,
580- ) ;
581- }
576+ return Ok (
577+ RawStringSource :: from ( format ! (
578+ r#"import {NAMESPACE_OBJECT_EXPORT} from {exported_content};
579+ export default {NAMESPACE_OBJECT_EXPORT}"#
580+ ) )
581+ . boxed ( ) ,
582+ ) ;
582583 } else if experimental_lib_preserve_import {
583- scope. register_namespace_export ( NAMESPACE_OBJECT_EXPORT ) ;
584-
585- if is_module {
586- return Ok (
587- RawStringSource :: from ( format ! (
588- r#"import {NAMESPACE_OBJECT_EXPORT} from {exported_content};"#
589- ) )
590- . boxed ( ) ,
591- ) ;
592- } else {
593- let supports_const = compilation. options . output . environment . supports_const ( ) ;
594- let declaration_kind = if supports_const { "const" } else { "var" } ;
595-
596- return Ok (
597- RawStringSource :: from ( format ! (
598- r#"{declaration_kind} {NAMESPACE_OBJECT_EXPORT} = require({NAMESPACE_OBJECT_EXPORT});"#
599- ) )
600- . boxed ( ) ,
601- ) ;
602- }
584+ return Ok (
585+ RawStringSource :: from ( format ! (
586+ r#"import {NAMESPACE_OBJECT_EXPORT} from {exported_content};"#
587+ ) )
588+ . boxed ( ) ,
589+ ) ;
603590 }
591+ }
592+ } else {
593+ if let Some ( ref mut scope) = generate_context. concatenation_scope {
594+ scope. register_namespace_export ( NAMESPACE_OBJECT_EXPORT ) ;
595+ let supports_const = compilation. options . output . environment . supports_const ( ) ;
596+ let declaration_kind = if supports_const { "const" } else { "var" } ;
597+ return Ok (
598+ RawStringSource :: from ( format ! (
599+ r#"{declaration_kind} {NAMESPACE_OBJECT_EXPORT} = require({exported_content});"#
600+ ) )
601+ . boxed ( ) ,
602+ ) ;
604603 } else {
605604 if experimental_lib_re_export {
606- if is_module {
607- return Ok (
608- RawStringSource :: from ( format ! ( r#"export default {exported_content};"# ) ) . boxed ( ) ,
609- ) ;
610- } else {
611- return Ok (
612- RawStringSource :: from ( format ! ( r#"module.exports = {exported_content};"# ) )
613- . boxed ( ) ,
614- ) ;
615- }
605+ generate_context
606+ . runtime_requirements
607+ . insert ( RuntimeGlobals :: MODULE ) ;
608+ return Ok (
609+ RawStringSource :: from ( format ! (
610+ r#"module.exports = require({exported_content});"#
611+ ) )
612+ . boxed ( ) ,
613+ ) ;
616614 }
617615 }
618616 }
0 commit comments