diff --git a/crates/mako/src/generate/transform.rs b/crates/mako/src/generate/transform.rs index b3598010f..93eab1617 100644 --- a/crates/mako/src/generate/transform.rs +++ b/crates/mako/src/generate/transform.rs @@ -84,47 +84,53 @@ pub fn transform_modules_in_thread( thread_pool::spawn(move || { let module_graph = context.module_graph.read().unwrap(); let deps = module_graph.get_dependencies(&module_id); - let mut resolved_deps: HashMap = deps - .into_iter() - .map(|(id, dep)| { - let replace_info = match &dep.resolve_type { - ResolveType::Worker(import_options) => { - let chunk_id = match import_options.get_chunk_name() { - Some(chunk_name) => generate_module_id(chunk_name, &context), - None => id.generate(&context), - }; - let chunk_graph = context.chunk_graph.read().unwrap(); - let chunk_name = - chunk_graph.chunk(&chunk_id.into()).unwrap().filename(); - - ResolvedReplaceInfo { - chunk_id: None, - to_replace_source: chunk_name, - resolved_module_id: id.clone(), - } - } - ResolveType::DynamicImport(import_options) => { - let chunk_id = Some(match import_options.get_chunk_name() { - Some(chunk_name) => generate_module_id(chunk_name, &context), - None => id.generate(&context), - }); - - ResolvedReplaceInfo { - chunk_id, - to_replace_source: id.generate(&context), - resolved_module_id: id.clone(), - } - } - _ => ResolvedReplaceInfo { + let mut resolved_deps: HashMap = Default::default(); + + deps.into_iter().for_each(|(id, dep)| { + let replace_info = match &dep.resolve_type { + ResolveType::Worker(import_options) => { + let chunk_id = match import_options.get_chunk_name() { + Some(chunk_name) => generate_module_id(chunk_name, &context), + None => id.generate(&context), + }; + let chunk_graph = context.chunk_graph.read().unwrap(); + let chunk_name = chunk_graph.chunk(&chunk_id.into()).unwrap().filename(); + + ResolvedReplaceInfo { chunk_id: None, + to_replace_source: chunk_name, + resolved_module_id: id.clone(), + } + } + ResolveType::DynamicImport(import_options) => { + let chunk_id = Some(match import_options.get_chunk_name() { + Some(chunk_name) => generate_module_id(chunk_name, &context), + None => id.generate(&context), + }); + + ResolvedReplaceInfo { + chunk_id, to_replace_source: id.generate(&context), resolved_module_id: id.clone(), - }, - }; + } + } + _ => ResolvedReplaceInfo { + chunk_id: None, + to_replace_source: id.generate(&context), + resolved_module_id: id.clone(), + }, + }; - (dep.source.clone(), replace_info) - }) - .collect(); + resolved_deps + .entry(dep.source.clone()) + .and_modify(|info: &mut ResolvedReplaceInfo| { + match (&replace_info.chunk_id, &info.chunk_id) { + (None, _) => {} + (Some(id), _) => info.chunk_id = Some(id.clone()), + } + }) + .or_insert(replace_info); + }); insert_swc_helper_replace(&mut resolved_deps, &context); let module = module_graph.get_module(&module_id).unwrap(); let info = module.info.as_ref().unwrap();