Skip to content

Commit

Permalink
refactor: 🎨 fix case one source with different import type
Browse files Browse the repository at this point in the history
  • Loading branch information
stormslowly committed Nov 6, 2024
1 parent 8cef532 commit 8609332
Showing 1 changed file with 43 additions and 37 deletions.
80 changes: 43 additions & 37 deletions crates/mako/src/generate/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, ResolvedReplaceInfo> = 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<String, ResolvedReplaceInfo> = 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),

Check warning on line 94 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L91-L94

Added lines #L91 - L94 were not covered by tests
};
let chunk_graph = context.chunk_graph.read().unwrap();
let chunk_name = chunk_graph.chunk(&chunk_id.into()).unwrap().filename();

Check warning on line 97 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L96-L97

Added lines #L96 - L97 were not covered by tests

ResolvedReplaceInfo {
chunk_id: None,

Check warning on line 100 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L99-L100

Added lines #L99 - L100 were not covered by tests
to_replace_source: chunk_name,
resolved_module_id: id.clone(),

Check warning on line 102 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L102

Added line #L102 was not covered by tests
}
}
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),

Check warning on line 108 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L104-L108

Added lines #L104 - L108 were not covered by tests
});

ResolvedReplaceInfo {

Check warning on line 111 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L111

Added line #L111 was not covered by tests
chunk_id,
to_replace_source: id.generate(&context),
resolved_module_id: id.clone(),

Check warning on line 114 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L113-L114

Added lines #L113 - L114 were not covered by tests
},
};
}
}

Check warning on line 116 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L116

Added line #L116 was not covered by tests
_ => 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) {

Check warning on line 127 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L127

Added line #L127 was not covered by tests
(None, _) => {}
(Some(id), _) => info.chunk_id = Some(id.clone()),

Check warning on line 129 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L129

Added line #L129 was not covered by tests
}
})

Check warning on line 131 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L131

Added line #L131 was not covered by tests
.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();
Expand Down

0 comments on commit 8609332

Please sign in to comment.