Skip to content

Commit 2b45815

Browse files
authored
fix: concate with external runtime specific modules (#9490)
1 parent 7ffa2fa commit 2b45815

File tree

9 files changed

+101
-15
lines changed

9 files changed

+101
-15
lines changed

crates/rspack_core/src/concatenated_module.rs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ impl Module for ConcatenatedModule {
672672
let mut top_level_declarations: HashSet<Atom> = HashSet::default();
673673
let mut public_path_auto_replace: bool = false;
674674

675-
for module_info_id in modules_with_info.iter() {
675+
for (module_info_id, _raw_condition) in modules_with_info.iter() {
676676
let Some(ModuleInfo::Concatenated(info)) = module_to_info_map.get_mut(module_info_id) else {
677677
continue;
678678
};
@@ -866,7 +866,7 @@ impl Module for ConcatenatedModule {
866866
let name = &reference.id.sym;
867867
let match_result = ConcatenationScope::match_module_reference(name.as_str());
868868
if let Some(match_info) = match_result {
869-
let referenced_info_id = &modules_with_info[match_info.index];
869+
let referenced_info_id = &modules_with_info[match_info.index].0;
870870
refs.push((
871871
reference.clone(),
872872
referenced_info_id,
@@ -1175,7 +1175,7 @@ impl Module for ConcatenatedModule {
11751175

11761176
// Evaluate modules in order
11771177
let module_graph = compilation.get_module_graph();
1178-
for module_info_id in modules_with_info {
1178+
for (module_info_id, item_runtime_condition) in modules_with_info {
11791179
let name;
11801180
let mut is_conditional = false;
11811181
let info = module_to_info_map
@@ -1208,11 +1208,9 @@ impl Module for ConcatenatedModule {
12081208

12091209
runtime_requirements.insert(RuntimeGlobals::REQUIRE);
12101210

1211-
let runtime_condition = &info.runtime_condition;
1212-
12131211
let condition = runtime_condition_expression(
12141212
&compilation.chunk_graph,
1215-
Some(runtime_condition),
1213+
item_runtime_condition.as_ref(),
12161214
runtime,
12171215
&mut runtime_requirements,
12181216
);
@@ -1426,7 +1424,10 @@ impl ConcatenatedModule {
14261424
&self,
14271425
mg: &ModuleGraph,
14281426
runtime: Option<&RuntimeSpec>,
1429-
) -> (Vec<ModuleIdentifier>, IdentifierIndexMap<ModuleInfo>) {
1427+
) -> (
1428+
Vec<(ModuleIdentifier, Option<RuntimeCondition>)>,
1429+
IdentifierIndexMap<ModuleInfo>,
1430+
) {
14301431
let ordered_concatenation_list = self.create_concatenation_list(
14311432
self.root_module_ctxt.id,
14321433
self.modules.iter().map(|item| item.id).collect(),
@@ -1439,7 +1440,17 @@ impl ConcatenatedModule {
14391440
let module_id = concatenation_entry.module(mg);
14401441
match map.entry(module_id) {
14411442
indexmap::map::Entry::Occupied(_) => {
1442-
list.push(module_id);
1443+
let runtime_condition =
1444+
if let ConcatenationEntry::External(ConcatenationEntryExternal {
1445+
runtime_condition,
1446+
..
1447+
}) = &concatenation_entry
1448+
{
1449+
Some(runtime_condition.clone())
1450+
} else {
1451+
None
1452+
};
1453+
list.push((module_id, runtime_condition));
14431454
}
14441455
indexmap::map::Entry::Vacant(vac) => {
14451456
match concatenation_entry {
@@ -1450,13 +1461,13 @@ impl ConcatenatedModule {
14501461
..Default::default()
14511462
};
14521463
vac.insert(ModuleInfo::Concatenated(Box::new(info)));
1453-
list.push(module_id);
1464+
list.push((module_id, None));
14541465
}
14551466
ConcatenationEntry::External(e) => {
14561467
let info = ExternalModuleInfo {
14571468
index: i,
14581469
module: module_id,
1459-
runtime_condition: e.runtime_condition,
1470+
runtime_condition: e.runtime_condition.clone(),
14601471
interop_namespace_object_used: false,
14611472
interop_namespace_object_name: None,
14621473
interop_namespace_object2_used: false,
@@ -1466,7 +1477,7 @@ impl ConcatenatedModule {
14661477
name: None,
14671478
};
14681479
vac.insert(ModuleInfo::External(info));
1469-
list.push(module_id)
1480+
list.push((module_id, Some(e.runtime_condition)))
14701481
}
14711482
};
14721483
}
@@ -1523,7 +1534,7 @@ impl ConcatenatedModule {
15231534
let exist_entry = match exists_entry.get(module) {
15241535
Some(RuntimeCondition::Boolean(true)) => return,
15251536
None => None,
1526-
Some(_condition) => Some(runtime_condition.clone()),
1537+
Some(condition) => Some(condition.clone()),
15271538
};
15281539
if module_set.contains(module) {
15291540
exists_entry.insert(*module, RuntimeCondition::Boolean(true));
@@ -1552,16 +1563,22 @@ impl ConcatenatedModule {
15521563
},
15531564
));
15541565
} else {
1555-
if let Some(cond) = exist_entry {
1566+
let runtime_condition = if let Some(cond) = exist_entry {
15561567
let reduced_runtime_condition =
15571568
subtract_runtime_condition(&runtime_condition, &cond, runtime);
15581569
if matches!(reduced_runtime_condition, RuntimeCondition::Boolean(false)) {
15591570
return;
15601571
}
1561-
exists_entry.insert(*con.module_identifier(), reduced_runtime_condition);
1572+
exists_entry.insert(
1573+
*con.module_identifier(),
1574+
merge_runtime_condition_non_false(&cond, &reduced_runtime_condition, runtime),
1575+
);
1576+
reduced_runtime_condition
15621577
} else {
15631578
exists_entry.insert(*con.module_identifier(), runtime_condition.clone());
1564-
}
1579+
runtime_condition
1580+
};
1581+
15651582
if let Some(ConcatenationEntry::External(last)) = list.last_mut()
15661583
&& last.module(mg) == *con.module_identifier()
15671584
{
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Provide } from "./should-concat/lib";
2+
import Comp from "./comp";
3+
4+
Provide();
5+
Comp();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Provide } from "./should-concat/lib";
2+
3+
Provide();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { connect } from "./should-concat/lib";
2+
3+
export default connect;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**@type {import("@rspack/core").Configuration}*/
2+
module.exports = {
3+
mode: "production",
4+
entry: {
5+
a: "./a.js",
6+
b: "./b.js"
7+
},
8+
output: {
9+
filename: "[name].js"
10+
},
11+
module: {
12+
rules: [
13+
{
14+
test: /value\.js/,
15+
sideEffects: false
16+
}
17+
]
18+
},
19+
optimization: {
20+
usedExports: true,
21+
innerGraph: true,
22+
sideEffects: true,
23+
concatenateModules: true,
24+
splitChunks: {
25+
cacheGroups: {
26+
shared: {
27+
test: /should-concat/,
28+
chunks: "all",
29+
minSize: 0,
30+
name: "shared"
31+
}
32+
}
33+
}
34+
}
35+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { value } from "./value";
2+
3+
export function connect() {
4+
value;
5+
}
6+
7+
console.log('')
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export { connect } from "./connect";
2+
// external value for runtime a
3+
import { value } from "./value";
4+
5+
6+
console.log('')
7+
8+
export function Provide() {
9+
value;
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const value = 1;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
findBundle() {
3+
return ["b.js", "a.js", "shared.js"];
4+
}
5+
};

0 commit comments

Comments
 (0)