diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index c0f8bc9ff3e5..efe72d5914f0 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -1128,6 +1128,7 @@ export interface RawAssetGeneratorOptions { filename?: JsFilename publicPath?: "auto" | JsFilename dataUrl?: RawAssetGeneratorDataUrlOptions | ((arg: RawAssetGeneratorDataUrlFnArgs) => string) + experimentalLibReExport?: boolean } export interface RawAssetInlineGeneratorOptions { @@ -1151,6 +1152,7 @@ export interface RawAssetResourceGeneratorOptions { emit?: boolean filename?: JsFilename publicPath?: "auto" | JsFilename + experimentalLibReExport?: boolean } export interface RawBannerPluginOptions { diff --git a/crates/rspack_binding_options/src/options/raw_module/mod.rs b/crates/rspack_binding_options/src/options/raw_module/mod.rs index baf6781e3981..2939c3b15e50 100644 --- a/crates/rspack_binding_options/src/options/raw_module/mod.rs +++ b/crates/rspack_binding_options/src/options/raw_module/mod.rs @@ -503,6 +503,8 @@ pub struct RawAssetGeneratorOptions { ts_type = "RawAssetGeneratorDataUrlOptions | ((arg: RawAssetGeneratorDataUrlFnArgs) => string)" )] pub data_url: Option, + + pub __experimental_lib_re_export: Option, } impl From for AssetGeneratorOptions { @@ -514,6 +516,7 @@ impl From for AssetGeneratorOptions { data_url: value .data_url .map(|i| RawAssetGeneratorDataUrlWrapper(i).into()), + __experimental_lib_re_export: value.__experimental_lib_re_export.map(|i| i.into()), } } } @@ -545,6 +548,8 @@ pub struct RawAssetResourceGeneratorOptions { pub filename: Option, #[napi(ts_type = "\"auto\" | JsFilename")] pub public_path: Option, + + pub __experimental_lib_re_export: Option, } impl From for AssetResourceGeneratorOptions { @@ -553,6 +558,7 @@ impl From for AssetResourceGeneratorOptions { emit: value.emit, filename: value.filename.map(|i| i.into()), public_path: value.public_path.map(|i| i.into()), + __experimental_lib_re_export: value.__experimental_lib_re_export.map(|i| i.into()), } } } diff --git a/crates/rspack_core/src/options/module.rs b/crates/rspack_core/src/options/module.rs index b60cbfd28375..999ea57af722 100644 --- a/crates/rspack_core/src/options/module.rs +++ b/crates/rspack_core/src/options/module.rs @@ -357,6 +357,17 @@ impl GeneratorOptions { .and_then(|x| x.emit) .or_else(|| self.get_asset_resource().and_then(|x| x.emit)) } + + pub fn asset_experimental_lib_re_export(&self) -> Option { + self + .get_asset() + .and_then(|x| x.__experimental_lib_re_export) + .or_else(|| { + self + .get_asset_resource() + .and_then(|x| x.__experimental_lib_re_export) + }) + } } #[cacheable] @@ -371,6 +382,7 @@ pub struct AssetResourceGeneratorOptions { pub emit: Option, pub filename: Option, pub public_path: Option, + pub __experimental_lib_re_export: Option, } #[cacheable] @@ -380,6 +392,7 @@ pub struct AssetGeneratorOptions { pub filename: Option, pub public_path: Option, pub data_url: Option, + pub __experimental_lib_re_export: Option, } pub struct AssetGeneratorDataUrlFnArgs { diff --git a/crates/rspack_plugin_asset/src/lib.rs b/crates/rspack_plugin_asset/src/lib.rs index 1f5d1198e3f4..762485a456e8 100644 --- a/crates/rspack_plugin_asset/src/lib.rs +++ b/crates/rspack_plugin_asset/src/lib.rs @@ -452,6 +452,12 @@ impl ParserAndGenerator for AssetParserAndGenerator { &source_file_name, )?; + dbg!(&module_generator_options); + + let experimental_lib_re_export = module_generator_options + .and_then(|x| x.asset_experimental_lib_re_export()) + .unwrap_or(false); + let asset_path = if let Some(public_path) = module_generator_options.and_then(|x| x.asset_public_path()) { @@ -621,6 +627,7 @@ async fn render_manifest( .get::() .expect("should have asset_info") .inner(); + dbg!(&asset_filename, &asset_info); RenderManifestEntry { source: source.clone(), filename: asset_filename.to_owned(), diff --git a/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs b/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs index ceac4894f526..e66773d4345a 100644 --- a/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs +++ b/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs @@ -555,6 +555,9 @@ impl ModuleConcatenationPlugin { let box_module = module_graph .module_by_identifier(&root_module_id) .expect("should have module"); + let root_module_source_types = box_module.source_types().clone(); + let is_root_module_asset_module = root_module_source_types.contains(&SourceType::Asset); + let root_module_ctxt = RootModuleContext { id: root_module_id, readable_identifier: box_module @@ -677,8 +680,26 @@ impl ModuleConcatenationPlugin { // .module_identifier_to_module // .remove(&root_module_id); // compilation.chunk_graph.clear + if is_root_module_asset_module { + chunk_graph.replace_module(&root_module_id, &new_module.id()); + chunk_graph.add_module(root_module_id); + for chunk_ukey in chunk_graph.get_module_chunks(new_module.id()).clone() { + let module = module_graph + .module_by_identifier(&root_module_id) + .expect("should exist module"); - chunk_graph.replace_module(&root_module_id, &new_module.id()); + let source_types = chunk_graph.get_chunk_module_source_types(&chunk_ukey, module); + let new_source_types = source_types + .iter() + .filter(|source_type| !matches!(source_type, SourceType::JavaScript)) + .copied() + .collect(); + chunk_graph.set_chunk_modules_source_types(&chunk_ukey, root_module_id, new_source_types); + chunk_graph.connect_chunk_and_module(chunk_ukey, root_module_id); + } + } else { + chunk_graph.replace_module(&root_module_id, &new_module.id()); + } module_graph.move_module_connections(&root_module_id, &new_module.id(), |c, dep| { let other_module = if *c.module_identifier() == root_module_id {