Skip to content

Commit 29fbada

Browse files
JSerFengCPunisher
authored andcommitted
fix: should use ordered map to store module outgoing refs
1 parent fda983d commit 29fbada

File tree

5 files changed

+31
-23
lines changed

5 files changed

+31
-23
lines changed

crates/rspack_core/src/concatenated_module.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
22
borrow::Cow,
3-
collections::{BTreeMap, hash_map::Entry},
3+
collections::BTreeMap,
44
fmt::Debug,
55
hash::{BuildHasherDefault, Hasher},
66
sync::{Arc, LazyLock},
@@ -22,7 +22,8 @@ use rspack_sources::{
2222
BoxSource, CachedSource, ConcatSource, RawStringSource, ReplaceSource, Source, SourceExt,
2323
};
2424
use rspack_util::{
25-
SpanExt, ext::DynHash, itoa, json_stringify, source_map::SourceMapKind, swc::join_atom,
25+
SpanExt, ext::DynHash, fx_hash::FxIndexMap, itoa, json_stringify, source_map::SourceMapKind,
26+
swc::join_atom,
2627
};
2728
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet, FxHasher};
2829
use swc_core::{
@@ -204,7 +205,7 @@ pub struct ConcatenatedModuleInfo {
204205
pub global_scope_ident: Vec<ConcatenatedModuleIdent>,
205206
pub idents: Vec<ConcatenatedModuleIdent>,
206207
pub all_used_names: HashSet<Atom>,
207-
pub binding_to_ref: HashMap<(Atom, SyntaxContext), Vec<ConcatenatedModuleIdent>>,
208+
pub binding_to_ref: FxIndexMap<(Atom, SyntaxContext), Vec<ConcatenatedModuleIdent>>,
208209

209210
pub public_path_auto_replacement: Option<bool>,
210211
pub static_url_replacement: bool,
@@ -2319,15 +2320,15 @@ impl ConcatenatedModule {
23192320
}
23202321
module_info.all_used_names = all_used_names;
23212322

2322-
let mut binding_to_ref: HashMap<(Atom, SyntaxContext), Vec<ConcatenatedModuleIdent>> =
2323-
HashMap::default();
2323+
let mut binding_to_ref: FxIndexMap<(Atom, SyntaxContext), Vec<ConcatenatedModuleIdent>> =
2324+
Default::default();
23242325

23252326
for ident in module_info.idents.iter() {
23262327
match binding_to_ref.entry((ident.id.sym.clone(), ident.id.ctxt)) {
2327-
Entry::Occupied(mut occ) => {
2328+
indexmap::map::Entry::Occupied(mut occ) => {
23282329
occ.get_mut().push(ident.clone());
23292330
}
2330-
Entry::Vacant(vac) => {
2331+
indexmap::map::Entry::Vacant(vac) => {
23312332
vac.insert(vec![ident.clone()]);
23322333
}
23332334
};

crates/rspack_plugin_esm_library/src/link.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::{
2-
collections::{self, hash_map::Entry},
2+
collections::{self},
33
hash::BuildHasher,
44
sync::Arc,
55
};
@@ -27,7 +27,7 @@ use rspack_plugin_javascript::{
2727
};
2828
use rspack_util::{
2929
atom::Atom,
30-
fx_hash::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet},
30+
fx_hash::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet, indexmap},
3131
swc::join_atom,
3232
};
3333
use swc_core::{
@@ -821,17 +821,17 @@ impl EsmLibraryPlugin {
821821
idents.push(ident);
822822
}
823823

824-
let mut binding_to_ref: FxHashMap<
824+
let mut binding_to_ref: FxIndexMap<
825825
(Atom, SyntaxContext),
826826
Vec<ConcatenatedModuleIdent>,
827-
> = FxHashMap::default();
827+
> = Default::default();
828828

829829
for ident in &idents {
830830
match binding_to_ref.entry((ident.id.sym.clone(), ident.id.ctxt)) {
831-
Entry::Occupied(mut occ) => {
831+
indexmap::map::Entry::Occupied(mut occ) => {
832832
occ.get_mut().push(ident.clone());
833833
}
834-
Entry::Vacant(vac) => {
834+
indexmap::map::Entry::Vacant(vac) => {
835835
vac.insert(vec![ident.clone()]);
836836
}
837837
};
@@ -1517,7 +1517,14 @@ impl EsmLibraryPlugin {
15171517

15181518
// link entry direct exports
15191519
if let Some(preserve_modules) = &self.preserve_modules {
1520-
for module_id in module_graph.modules().keys() {
1520+
let modules = module_graph.modules();
1521+
let mut modules = modules.keys().collect::<Vec<_>>();
1522+
modules.sort_by(|a, b| {
1523+
let ad = module_graph.get_depth(a);
1524+
let bd = module_graph.get_depth(b);
1525+
ad.cmp(&bd)
1526+
});
1527+
for module_id in modules {
15211528
if compilation.entry_modules().contains(module_id) {
15221529
continue;
15231530
}

crates/rspack_util/src/fx_hash.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::hash::BuildHasherDefault;
22

33
use dashmap::{DashMap, DashSet};
4+
pub use indexmap;
45
use indexmap::{IndexMap, IndexSet};
56
use rustc_hash::FxHasher;
6-
77
pub type BuildFxHasher = BuildHasherDefault<FxHasher>;
88
pub use rustc_hash::{FxHashMap, FxHashSet};
99
pub type FxDashMap<K, V> = DashMap<K, V, BuildHasherDefault<FxHasher>>;

tests/rspack-test/esmOutputCases/deconflict/deconflit-local/__snapshots__/esm.snap.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ const value = 42
66
// ./index.js
77

88

9-
const index_local_0 = 'index'
10-
const index_local = ''
9+
const index_local = 'index'
10+
const index_index_local = ''
1111

1212
it('should have deconflicted symbol', () => {
1313
expect(value).toBe(42)
14-
expect(index_local_0).toBe('index')
15-
expect(index_local).toBe('')
14+
expect(index_local).toBe('index')
15+
expect(index_index_local).toBe('')
1616
})
1717

1818

tests/rspack-test/esmOutputCases/deconflict/other-chunk-local/__snapshots__/esm.snap.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ const value = 42
66
// ./index.js
77

88

9-
const index_local_0 = 'index'
10-
const index_local = ''
9+
const index_local = 'index'
10+
const index_index_local = ''
1111

1212
it('should have deconflicted symbol', async () => {
1313
expect(value).toBe(42)
14-
expect(index_local_0).toBe('index')
15-
expect(index_local).toBe('')
14+
expect(index_local).toBe('index')
15+
expect(index_index_local).toBe('')
1616
const {value: v} = await import("./other_js.mjs").then((mod) => ({ value: mod.value }))
1717
expect(v).toBe(value)
1818
})

0 commit comments

Comments
 (0)