Skip to content

Commit abd35d5

Browse files
authored
feat: static analyse import then for tree shaking (#11665)
1 parent 24a3348 commit abd35d5

File tree

40 files changed

+654
-222
lines changed

40 files changed

+654
-222
lines changed

crates/rspack_core/src/concatenated_module.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,11 +1199,15 @@ impl Module for ConcatenatedModule {
11991199
)));
12001200
}
12011201

1202-
let exports_info =
1202+
let root_exports_info =
12031203
module_graph.get_prefetched_exports_info(&self.id(), PrefetchExportsInfoMode::Default);
1204-
let used = exports_info.other_exports_info().get_used(runtime);
12051204
// Add ESM compatibility flag (must be first because of possible circular dependencies)
1206-
if used != UsageState::Unused {
1205+
if root_exports_info.other_exports_info().get_used(runtime) != UsageState::Unused
1206+
|| root_exports_info
1207+
.get_read_only_export_info(&"__esModule".into())
1208+
.get_used(runtime)
1209+
!= UsageState::Unused
1210+
{
12071211
should_add_esm_flag = true
12081212
}
12091213

crates/rspack_core/src/context_module.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -888,14 +888,30 @@ impl Module for ContextModule {
888888
id += ")/";
889889
}
890890
id += &contextify(options.context, self.options.resource.as_str());
891-
id.push(' ');
892-
id.push_str(self.options.context_options.mode.as_str());
891+
id += " ";
892+
id += self.options.context_options.mode.as_str();
893893
if self.options.context_options.recursive {
894-
id.push_str(" recursive");
894+
id += " recursive";
895+
}
896+
if !self.options.addon.is_empty() {
897+
id += " ";
898+
id += &self.options.addon;
895899
}
896900
if let Some(regexp) = &self.options.context_options.reg_exp {
897-
id.push(' ');
898-
id.push_str(&regexp.to_pretty_string(true));
901+
id += " ";
902+
id += &regexp.to_pretty_string(true);
903+
}
904+
if let Some(include) = &self.options.context_options.include {
905+
id += " include: ";
906+
id += &include.to_pretty_string(true);
907+
}
908+
if let Some(exclude) = &self.options.context_options.exclude {
909+
id += " exclude: ";
910+
id += &exclude.to_pretty_string(true);
911+
}
912+
if let Some(exports) = &self.options.context_options.referenced_exports {
913+
id += " referencedExports: ";
914+
id += &exports.iter().map(|ids| ids.iter().join(".")).join(", ");
899915
}
900916
Some(Cow::Owned(id))
901917
}
@@ -1115,17 +1131,7 @@ fn create_identifier(options: &ContextModuleOptions) -> Identifier {
11151131
}
11161132
if let Some(exports) = &options.context_options.referenced_exports {
11171133
id += "|referencedExports: ";
1118-
id += &format!(
1119-
"[{}]",
1120-
exports
1121-
.iter()
1122-
.map(|ids| if ids.is_empty() {
1123-
String::from("*")
1124-
} else {
1125-
ids.iter().join(".")
1126-
})
1127-
.join(", ")
1128-
);
1134+
id += &exports.iter().map(|ids| ids.iter().join(".")).join(", ");
11291135
}
11301136

11311137
if let Some(GroupOptions::ChunkGroup(group)) = &options.context_options.group_options {

crates/rspack_plugin_javascript/src/dependency/context/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ fn create_resource_identifier_for_context_dependency(
5151
let referenced_exports = options
5252
.referenced_exports
5353
.as_ref()
54-
.map(|x| {
55-
x.iter()
56-
.map(|x| x.iter().map(|x| format!(r#""{x}""#)).join("."))
57-
.join(",")
58-
})
54+
.map(|ids| ids.iter().map(|ids| ids.iter().join(".")).join(", "))
5955
.unwrap_or_default();
6056
let mut group_options = String::new();
6157

crates/rspack_plugin_javascript/src/parser_plugin/define_plugin/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl JavascriptParserPlugin for DefineParserPlugin {
148148
None
149149
}
150150

151-
fn collect_destructuring_assignment_properties(
151+
fn can_collect_destructuring_assignment_properties(
152152
&self,
153153
parser: &mut JavascriptParser,
154154
expr: &Expr,

crates/rspack_plugin_javascript/src/parser_plugin/drive.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,13 +519,13 @@ impl JavascriptParserPlugin for JavaScriptParserPluginDrive {
519519
None
520520
}
521521

522-
fn collect_destructuring_assignment_properties(
522+
fn can_collect_destructuring_assignment_properties(
523523
&self,
524524
parser: &mut JavascriptParser,
525525
expr: &Expr,
526526
) -> Option<bool> {
527527
for plugin in &self.plugins {
528-
let res = plugin.collect_destructuring_assignment_properties(parser, expr);
528+
let res = plugin.can_collect_destructuring_assignment_properties(parser, expr);
529529
// `SyncBailHook`
530530
if res.is_some() {
531531
return res;
@@ -610,10 +610,15 @@ impl JavascriptParserPlugin for JavaScriptParserPluginDrive {
610610
None
611611
}
612612

613-
fn import_call(&self, parser: &mut JavascriptParser, expr: &CallExpr) -> Option<bool> {
613+
fn import_call(
614+
&self,
615+
parser: &mut JavascriptParser,
616+
expr: &CallExpr,
617+
import_then: Option<&CallExpr>,
618+
) -> Option<bool> {
614619
assert!(expr.callee.is_import());
615620
for plugin in &self.plugins {
616-
let res = plugin.import_call(parser, expr);
621+
let res = plugin.import_call(parser, expr, import_then);
617622
// `SyncBailHook`
618623
if res.is_some() {
619624
return res;

crates/rspack_plugin_javascript/src/parser_plugin/esm_import_dependency_parser_plugin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl JavascriptParserPlugin for ESMImportDependencyParserPlugin {
147147
Some(true)
148148
}
149149

150-
fn collect_destructuring_assignment_properties(
150+
fn can_collect_destructuring_assignment_properties(
151151
&self,
152152
parser: &mut JavascriptParser,
153153
expr: &Expr,

crates/rspack_plugin_javascript/src/parser_plugin/import_meta_plugin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl JavascriptParserPlugin for ImportMetaPlugin {
148148
}
149149
}
150150

151-
fn collect_destructuring_assignment_properties(
151+
fn can_collect_destructuring_assignment_properties(
152152
&self,
153153
_parser: &mut JavascriptParser,
154154
expr: &Expr,

0 commit comments

Comments
 (0)