Skip to content

Commit 6a2dd1c

Browse files
committed
chore: reduce
1 parent 671e649 commit 6a2dd1c

File tree

6 files changed

+132
-138
lines changed

6 files changed

+132
-138
lines changed

crates/rspack_plugin_mf/src/container/container_entry_module.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ impl ContainerEntryModule {
7373
enhanced,
7474
}
7575
}
76+
77+
pub fn exposes(&self) -> &[(String, ExposeOptions)] {
78+
&self.exposes
79+
}
7680
}
7781

7882
impl Identifiable for ContainerEntryModule {

crates/rspack_plugin_mf/src/manifest/asset.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ pub fn collect_assets_from_chunk(
1919

2020
for cg in chunk.groups() {
2121
let group = compilation.chunk_group_by_ukey.expect_get(cg);
22-
if let Some(name) = group.name() {
23-
if !entry_point_names.contains(name) {
24-
for file in group.get_files(&compilation.chunk_by_ukey) {
25-
if file.ends_with(".css") {
26-
css_sync.insert(file.to_string());
27-
} else if !is_hot_file(&file) {
28-
js_sync.insert(file);
29-
}
22+
if group
23+
.name()
24+
.is_some_and(|name| !entry_point_names.contains(name))
25+
{
26+
for file in group.get_files(&compilation.chunk_by_ukey) {
27+
if file.ends_with(".css") {
28+
css_sync.insert(file.to_string());
29+
} else if !is_hot_file(&file) {
30+
js_sync.insert(file);
3031
}
3132
}
3233
}
@@ -43,14 +44,15 @@ pub fn collect_assets_from_chunk(
4344
}
4445
for cg in async_chunk.groups() {
4546
let group = compilation.chunk_group_by_ukey.expect_get(cg);
46-
if let Some(name) = group.name() {
47-
if !entry_point_names.contains(name) {
48-
for file in group.get_files(&compilation.chunk_by_ukey) {
49-
if file.ends_with(".css") {
50-
css_async.insert(file.to_string());
51-
} else if !is_hot_file(&file) {
52-
js_async.insert(file);
53-
}
47+
if group
48+
.name()
49+
.is_some_and(|name| !entry_point_names.contains(name))
50+
{
51+
for file in group.get_files(&compilation.chunk_by_ukey) {
52+
if file.ends_with(".css") {
53+
css_async.insert(file.to_string());
54+
} else if !is_hot_file(&file) {
55+
js_async.insert(file);
5456
}
5557
}
5658
}
@@ -104,8 +106,8 @@ pub fn collect_assets_for_module(
104106
return None;
105107
}
106108
let mut result = empty_assets_group();
107-
for chunk_ukey in chunk_graph.get_module_chunks(*module_identifier).iter() {
108-
let chunk_assets = collect_assets_from_chunk(compilation, &chunk_ukey, entry_point_names);
109+
for chunk_ukey in chunk_graph.get_module_chunks(*module_identifier) {
110+
let chunk_assets = collect_assets_from_chunk(compilation, chunk_ukey, entry_point_names);
109111
merge_assets_group(&mut result, chunk_assets);
110112
}
111113
normalize_assets_group(&mut result);
@@ -121,11 +123,11 @@ pub fn remove_assets(group: &mut StatsAssetsGroup, exclude: &HashSet<String>) {
121123
}
122124

123125
pub fn promote_primary_assets_to_sync(group: &mut StatsAssetsGroup) {
124-
if group.js.sync.is_empty() && !group.js.r#async.is_empty() {
125-
group.js.sync.extend(group.js.r#async.drain(..));
126+
if group.js.sync.is_empty() {
127+
group.js.sync.append(&mut group.js.r#async);
126128
}
127-
if group.css.sync.is_empty() && !group.css.r#async.is_empty() {
128-
group.css.sync.extend(group.css.r#async.drain(..));
129+
if group.css.sync.is_empty() {
130+
group.css.sync.append(&mut group.css.r#async);
129131
}
130132
normalize_assets_group(group);
131133
}

crates/rspack_plugin_mf/src/manifest/data.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
use serde::Serialize;
22

3-
#[derive(Debug, Serialize, Clone)]
3+
#[derive(Debug, Serialize, Clone, Default)]
44
pub struct StatsAssetsGroup {
55
#[serde(default)]
66
pub js: AssetsSplit,
77
#[serde(default)]
88
pub css: AssetsSplit,
99
}
1010

11-
impl Default for StatsAssetsGroup {
12-
fn default() -> Self {
13-
Self {
14-
js: AssetsSplit::default(),
15-
css: AssetsSplit::default(),
16-
}
17-
}
18-
}
19-
2011
#[derive(Debug, Serialize, Clone, Default)]
2112
pub struct AssetsSplit {
2213
#[serde(default)]

crates/rspack_plugin_mf/src/manifest/mod.rs

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ use rspack_hook::{plugin, plugin_hook};
2929
use rspack_util::fx_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
3030
use utils::{
3131
collect_expose_requirements, compose_id_with_separator, ensure_shared_entry, is_hot_file,
32-
parse_consume_shared_identifier, parse_container_exposes_from_identifier,
33-
parse_provide_shared_identifier, record_shared_usage, strip_ext,
32+
parse_consume_shared_identifier, parse_provide_shared_identifier, record_shared_usage, strip_ext,
3433
};
3534

36-
use crate::container::remote_module::RemoteModule;
35+
use crate::container::{container_entry_module::ContainerEntryModule, remote_module::RemoteModule};
3736

3837
#[plugin]
3938
#[derive(Debug)]
@@ -198,54 +197,47 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
198197
(exposes, shared, remote_list)
199198
} else {
200199
let module_graph = compilation.get_module_graph();
201-
let context = &compilation.options.context;
202200

203201
let mut exposes_map: HashMap<String, StatsExpose> = HashMap::default();
204202
let mut shared_map: HashMap<String, StatsShared> = HashMap::default();
205203
let mut shared_usage_links: Vec<(String, String)> = Vec::new();
206204
let mut consume_module_ids: HashMap<String, Vec<ModuleIdentifier>> = HashMap::default();
207205
let mut remote_module_ids: Vec<ModuleIdentifier> = Vec::new();
208206
let mut container_entry_module: Option<ModuleIdentifier> = None;
209-
let mut module_ids_by_name: HashMap<String, ModuleIdentifier> = HashMap::default();
210-
let mut self_issued_module_ids: HashMap<String, ModuleIdentifier> = HashMap::default();
207+
for (_, module) in module_graph.modules().into_iter() {
208+
let module_identifier = module.identifier();
211209

212-
for (module_id, module) in module_graph.modules().into_iter() {
213-
let module_identifier = module_id;
214-
let module_name = module.readable_identifier(context).to_string();
215-
if !module_name.is_empty() {
216-
module_ids_by_name.insert(module_name.clone(), module_identifier);
217-
if let Some(issuer_module) = module_graph.get_issuer(&module_identifier) {
218-
let issuer_name = issuer_module.readable_identifier(context).to_string();
219-
if issuer_name == module_name {
220-
self_issued_module_ids.insert(issuer_name, module_identifier);
221-
}
210+
if let Some(container_entry) = module
211+
.as_ref()
212+
.as_any()
213+
.downcast_ref::<ContainerEntryModule>()
214+
{
215+
container_entry_module = Some(module_identifier);
216+
for (expose_key, options) in container_entry.exposes() {
217+
let expose_name = options
218+
.name
219+
.clone()
220+
.filter(|name| !name.is_empty())
221+
.unwrap_or_else(|| expose_key.trim_start_matches("./").to_string());
222+
let Some(import) = options.import.iter().find(|request| !request.is_empty()) else {
223+
continue;
224+
};
225+
let id_comp = compose_id_with_separator(&container_name, &expose_name);
226+
let expose_file_key = strip_ext(import);
227+
exposes_map.entry(expose_file_key).or_insert(StatsExpose {
228+
path: expose_key.clone(),
229+
id: id_comp,
230+
name: expose_name,
231+
requires: Vec::new(),
232+
assets: StatsAssetsGroup::default(),
233+
});
222234
}
235+
continue;
223236
}
224237

225238
let module_type = module.module_type();
226239
let identifier = module_identifier.to_string();
227240

228-
if identifier.starts_with("container entry") {
229-
container_entry_module = Some(module_identifier);
230-
if let Some(exposes) = parse_container_exposes_from_identifier(&identifier) {
231-
for (expose_key, import_name, import_file) in exposes {
232-
let expose_name = import_name
233-
.filter(|name| !name.is_empty())
234-
.unwrap_or_else(|| expose_key.trim_start_matches("./").to_string());
235-
let id_comp = compose_id_with_separator(&container_name, &expose_name);
236-
let expose_file_key = strip_ext(&import_file);
237-
exposes_map.entry(expose_file_key).or_insert(StatsExpose {
238-
path: expose_key.clone(),
239-
id: id_comp,
240-
name: expose_name,
241-
requires: Vec::new(),
242-
assets: StatsAssetsGroup::default(),
243-
});
244-
}
245-
}
246-
continue;
247-
}
248-
249241
if matches!(module_type, ModuleType::Remote) {
250242
remote_module_ids.push(module_identifier);
251243
}
@@ -271,12 +263,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
271263
if let Some((pkg, required)) = parse_consume_shared_identifier(&identifier) {
272264
let mut target_ids: Vec<ModuleIdentifier> = Vec::new();
273265
if let Some(issuer_module) = module_graph.get_issuer(&module_identifier) {
274-
let issuer_name = issuer_module.readable_identifier(context).to_string();
275-
if let Some(target) = self_issued_module_ids.get(&issuer_name) {
276-
target_ids.push(*target);
277-
} else if let Some(target) = module_ids_by_name.get(&issuer_name) {
278-
target_ids.push(*target);
279-
}
266+
target_ids.push(issuer_module.identifier());
280267
}
281268
if target_ids.is_empty() {
282269
target_ids.push(module_identifier);

crates/rspack_plugin_mf/src/manifest/utils.rs

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::path::Path;
22

33
use rspack_core::{Compilation, ModuleGraph, ModuleIdentifier};
44
use rspack_util::fx_hash::FxHashMap as HashMap;
5-
use serde_json::Value;
65

76
use super::data::{StatsExpose, StatsShared};
87

@@ -32,55 +31,6 @@ pub fn strip_ext(path: &str) -> String {
3231
}
3332
}
3433

35-
pub fn parse_container_exposes_from_identifier(
36-
identifier: &str,
37-
) -> Option<Vec<(String, Option<String>, String)>> {
38-
let start = identifier.find('[')?;
39-
let slice = &identifier[start..];
40-
let mut depth = 0_usize;
41-
let mut end_idx = None;
42-
for (i, ch) in slice.char_indices() {
43-
match ch {
44-
'[' => depth += 1,
45-
']' => {
46-
depth -= 1;
47-
if depth == 0 {
48-
end_idx = Some(i);
49-
break;
50-
}
51-
}
52-
_ => {}
53-
}
54-
}
55-
let end = end_idx?;
56-
let json_str = &slice[..=end];
57-
let val: Value = serde_json::from_str(json_str).ok()?;
58-
let arr = val.as_array()?;
59-
let mut ret: Vec<(String, Option<String>, String)> = Vec::new();
60-
for item in arr {
61-
if let Some(tuple) = item.as_array() {
62-
if tuple.len() == 2 {
63-
let expose_key = tuple[0].as_str().unwrap_or_default().to_string();
64-
if let Some(obj) = tuple[1].as_object() {
65-
let import = obj
66-
.get("import")
67-
.and_then(|v| v.as_array())
68-
.and_then(|a| a.first())
69-
.and_then(|v| v.as_str())
70-
.unwrap_or_default()
71-
.to_string();
72-
let name = obj
73-
.get("name")
74-
.and_then(|v| v.as_str())
75-
.map(|s| s.to_string());
76-
ret.push((expose_key, name, import));
77-
}
78-
}
79-
}
80-
}
81-
Some(ret)
82-
}
83-
8434
pub fn ensure_shared_entry<'a>(
8535
shared_map: &'a mut HashMap<String, StatsShared>,
8636
container_name: &str,

0 commit comments

Comments
 (0)