Skip to content

Commit ccaf6c4

Browse files
committed
fix: handle cjs export assignment side effects
1 parent 1fd4fca commit ccaf6c4

49 files changed

Lines changed: 296 additions & 169 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_exports_dependency.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ use rspack_cacheable::{
22
cacheable, cacheable_dyn,
33
with::{AsPreset, AsVec},
44
};
5+
use rspack_collections::{IdentifierMap, IdentifierSet};
56
use rspack_core::{
6-
AsContextDependency, AsModuleDependency, Dependency, DependencyCategory,
7+
AsContextDependency, AsModuleDependency, ConnectionState, Dependency, DependencyCategory,
78
DependencyCodeGeneration, DependencyId, DependencyRange, DependencyTemplate,
89
DependencyTemplateType, DependencyType, ExportNameOrSpec, ExportSpec, ExportsInfoArtifact,
910
ExportsOfExportsSpec, ExportsSpec, InitFragmentExt, InitFragmentKey, InitFragmentStage,
10-
ModuleGraph, ModuleGraphCacheArtifact, NormalInitFragment, TemplateContext,
11-
TemplateReplaceSource, UsedName, property_access,
11+
ModuleGraph, ModuleGraphCacheArtifact, NormalInitFragment, SideEffectsStateArtifact,
12+
TemplateContext, TemplateReplaceSource, UsedName, property_access,
1213
};
1314
use swc_atoms::Atom;
1415

@@ -123,6 +124,17 @@ impl Dependency for CommonJsExportsDependency {
123124
fn could_affect_referencing_module(&self) -> rspack_core::AffectType {
124125
rspack_core::AffectType::False
125126
}
127+
128+
fn get_module_evaluation_side_effects_state(
129+
&self,
130+
_module_graph: &ModuleGraph,
131+
_module_graph_cache: &ModuleGraphCacheArtifact,
132+
_side_effects_state_artifact: &SideEffectsStateArtifact,
133+
_module_chain: &mut IdentifierSet,
134+
_connection_state_cache: &mut IdentifierMap<ConnectionState>,
135+
) -> ConnectionState {
136+
ConnectionState::Active(false)
137+
}
126138
}
127139

128140
impl AsModuleDependency for CommonJsExportsDependency {}

crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_self_reference_dependency.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ use rspack_cacheable::{
22
cacheable, cacheable_dyn,
33
with::{AsPreset, AsVec},
44
};
5+
use rspack_collections::{IdentifierMap, IdentifierSet};
56
use rspack_core::{
6-
AsContextDependency, Dependency, DependencyCategory, DependencyCodeGeneration, DependencyId,
7-
DependencyRange, DependencyTemplate, DependencyTemplateType, DependencyType, ExportsInfoArtifact,
8-
ExtendedReferencedExport, FactorizeInfo, ModuleDependency, ModuleGraph, ModuleGraphCacheArtifact,
9-
RuntimeSpec, TemplateContext, TemplateReplaceSource, UsedName, property_access_with_optional,
7+
AsContextDependency, ConnectionState, Dependency, DependencyCategory, DependencyCodeGeneration,
8+
DependencyId, DependencyRange, DependencyTemplate, DependencyTemplateType, DependencyType,
9+
ExportsInfoArtifact, ExtendedReferencedExport, FactorizeInfo, ModuleDependency, ModuleGraph,
10+
ModuleGraphCacheArtifact, RuntimeSpec, SideEffectsStateArtifact, TemplateContext,
11+
TemplateReplaceSource, UsedName, property_access_with_optional,
1012
};
1113
use swc_atoms::Atom;
1214

@@ -22,6 +24,7 @@ pub struct CommonJsSelfReferenceDependency {
2224
names: Vec<Atom>,
2325
names_optionals: Vec<bool>,
2426
is_call: bool,
27+
evaluation_side_effect_free: bool,
2528
factorize_info: FactorizeInfo,
2629
}
2730

@@ -32,6 +35,7 @@ impl CommonJsSelfReferenceDependency {
3235
names: Vec<Atom>,
3336
names_optionals: Vec<bool>,
3437
is_call: bool,
38+
evaluation_side_effect_free: bool,
3539
) -> Self {
3640
Self {
3741
id: DependencyId::new(),
@@ -40,6 +44,7 @@ impl CommonJsSelfReferenceDependency {
4044
names,
4145
names_optionals,
4246
is_call,
47+
evaluation_side_effect_free,
4348
factorize_info: Default::default(),
4449
}
4550
}
@@ -90,6 +95,17 @@ impl Dependency for CommonJsSelfReferenceDependency {
9095
fn could_affect_referencing_module(&self) -> rspack_core::AffectType {
9196
rspack_core::AffectType::True
9297
}
98+
99+
fn get_module_evaluation_side_effects_state(
100+
&self,
101+
_module_graph: &ModuleGraph,
102+
_module_graph_cache: &ModuleGraphCacheArtifact,
103+
_side_effects_state_artifact: &SideEffectsStateArtifact,
104+
_module_chain: &mut IdentifierSet,
105+
_connection_state_cache: &mut IdentifierMap<ConnectionState>,
106+
) -> ConnectionState {
107+
ConnectionState::Active(!self.evaluation_side_effect_free)
108+
}
93109
}
94110

95111
#[cacheable_dyn]

crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ impl ParserAndGenerator for JavaScriptParserAndGenerator {
311311
presentational_dependencies,
312312
mut warning_diagnostics,
313313
mut side_effects_item,
314+
module_has_side_effects,
314315
} = match scan_dependencies(
315316
&source_string,
316317
&parsed_ast,
@@ -338,7 +339,7 @@ impl ParserAndGenerator for JavaScriptParserAndGenerator {
338339
let mut side_effects_bailout = None;
339340

340341
if compiler_options.optimization.side_effects.is_true() {
341-
let has_side_effects = side_effects_item.is_some();
342+
let has_side_effects = module_has_side_effects;
342343
build_meta.side_effect_free = Some(!has_side_effects);
343344
if has_side_effects {
344345
build_info.deferred_pure_checks.clear();

crates/rspack_plugin_javascript/src/parser_plugin/common_js_exports_parse_plugin.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ fn handle_access_export(
245245
remaining.to_vec(),
246246
remaining_optionals.to_vec(),
247247
call_args.is_some(),
248+
parser.in_assign_target,
248249
)));
249250
if let Some(call_args) = call_args {
250251
parser.walk_expr_or_spread(call_args);

0 commit comments

Comments
 (0)