Skip to content

Commit 54392cb

Browse files
committed
docs: add docs for NO_SIDE_EFFECTS notation
1 parent cc0e01c commit 54392cb

File tree

12 files changed

+270
-158
lines changed

12 files changed

+270
-158
lines changed

crates/rspack_core/src/dependency/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl std::fmt::Debug for DependencyCondition {
198198
}
199199

200200
#[rspack_cacheable::cacheable]
201-
#[derive(Debug, Clone, Serialize, Default)]
201+
#[derive(Debug, Clone, Serialize, Default, PartialEq, Eq)]
202202
pub struct ImportAttributes(FxHashMap<String, String>);
203203

204204
impl FromIterator<(String, String)> for ImportAttributes {

crates/rspack_core/src/module.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub struct BuildInfo {
6868
pub need_create_require: bool,
6969
#[cacheable(with=AsOption<AsPreset>)]
7070
pub json_data: Option<JsonValue>,
71+
#[cacheable(with=AsOption<AsVec<AsPreset>>)]
7172
pub pure_functions: Option<FxHashSet<Atom>>,
7273
#[cacheable(with=AsOption<AsVec<AsPreset>>)]
7374
pub top_level_declarations: Option<HashSet<Atom>>,
@@ -167,8 +168,8 @@ pub enum BuildMetaDefaultObject {
167168
#[cacheable]
168169
#[derive(Debug, Clone, PartialEq, Eq)]
169170
pub struct DeferredPureCheck {
170-
pub import_request: String,
171171
pub atom: Atom,
172+
pub dep_id: DependencyId,
172173
pub start: u32,
173174
pub end: u32,
174175
}

crates/rspack_plugin_javascript/src/dependency/esm/esm_import_specifier_dependency.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ impl ESMImportSpecifierDependency {
107107
.unwrap_or_else(|| self.ids.as_slice())
108108
}
109109

110+
pub fn get_name(&self) -> &Atom {
111+
&self.name
112+
}
113+
110114
pub fn get_referenced_exports_in_destructuring(
111115
&self,
112116
ids: Option<&[Atom]>,

crates/rspack_plugin_javascript/src/parser_plugin/inner_graph/plugin.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ impl InnerGraphPlugin {
104104
pub fn new(unresolved_mark: Mark) -> Self {
105105
Self {
106106
unresolved_context: SyntaxContext::empty().apply_mark(unresolved_mark),
107-
analyze_pure_annotation: true,
107+
// TODO: optimize inner graph
108+
analyze_pure_annotation: false,
108109
}
109110
}
110111

crates/rspack_plugin_javascript/src/parser_plugin/side_effects_parser_plugin.rs

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use rspack_core::{DeferredPureCheck, SideEffectsBailoutItemWithSpan};
1+
use rspack_core::{
2+
DeferredPureCheck, Dependency, ModuleDependency, SideEffectsBailoutItemWithSpan,
3+
};
4+
use rspack_util::SpanExt;
25
use rustc_hash::FxHashSet;
36
use swc_core::{
47
atoms::Atom,
@@ -15,6 +18,7 @@ use swc_core::{
1518

1619
use crate::{
1720
ClassExt, JavascriptParserPlugin,
21+
dependency::ESMImportSideEffectDependency,
1822
parser_plugin::esm_import_dependency_parser_plugin::{ESM_SPECIFIER_TAG, ESMSpecifierData},
1923
visitors::{JavascriptParser, Statement, TagInfoData, VariableDeclaration},
2024
};
@@ -154,7 +158,7 @@ impl JavascriptParserPlugin for SideEffectsParserPlugin {
154158
};
155159
ast.visit_with(&mut pure_annotation);
156160
let detected_pure_functions = pure_annotation.pure_functions;
157-
if detected_pure_functions.len() > 0 {
161+
if !detected_pure_functions.is_empty() {
158162
let pure_functions = parser.build_info.pure_functions.get_or_insert_default();
159163
pure_functions.extend(detected_pure_functions);
160164
}
@@ -227,7 +231,7 @@ impl JavascriptParserPlugin for SideEffectsParserPlugin {
227231
}
228232
}
229233

230-
if not_defined.len() > 0 {
234+
if !not_defined.is_empty() {
231235
let resource = parser.resource_data.resource();
232236
parser.add_warning(
233237
rspack_error::Diagnostic::warn("PURE_FUNCTION_NOT_FOUND".into(), format!("Following pure functions are not found in {resource}:\n[{}]\nRemove it from `module.rules[{{index}}].pureFunctions`", not_defined.iter().map(|atom| format!("`{atom}`")).collect::<Vec<_>>().join(", ")))
@@ -254,7 +258,7 @@ fn is_pure_call_expr(
254258
.unwrap_or(false);
255259

256260
if pure_flag {
257-
call_expr.args.iter().all(|arg| {
261+
return call_expr.args.iter().all(|arg| {
258262
if arg.spread.is_some() {
259263
false
260264
} else {
@@ -266,8 +270,8 @@ fn is_pure_call_expr(
266270
comments,
267271
)
268272
}
269-
})
270-
} else {
273+
});
274+
} else if analyze_pure_notation {
271275
if let Some(Expr::Ident(ident)) = callee.as_expr().map(|expr| expr.as_ref())
272276
&& parser
273277
.build_info
@@ -296,14 +300,14 @@ fn is_pure_call_expr(
296300
}
297301
});
298302
}
299-
300-
!expr.may_have_side_effects(ExprCtx {
301-
unresolved_ctxt,
302-
in_strict: false,
303-
is_unresolved_ref_safe: false,
304-
remaining_depth: 4,
305-
})
306303
}
304+
305+
!expr.may_have_side_effects(ExprCtx {
306+
unresolved_ctxt,
307+
in_strict: false,
308+
is_unresolved_ref_safe: false,
309+
remaining_depth: 4,
310+
})
307311
}
308312

309313
fn try_extract_deferred_check(
@@ -328,12 +332,25 @@ fn try_extract_deferred_check(
328332

329333
let data = ESMSpecifierData::downcast(tag_info.data.clone()?);
330334

331-
Some(DeferredPureCheck {
332-
import_request: data.source.to_string(),
333-
atom: data.name.clone(),
334-
start: callee.span().lo.0,
335-
end: callee.span().hi.0,
336-
})
335+
parser
336+
.get_dependencies()
337+
.iter()
338+
.find(|dep| {
339+
let Some(dep) = dep.downcast_ref::<ESMImportSideEffectDependency>() else {
340+
return false;
341+
};
342+
343+
let request_eq = dep.request() == &data.source;
344+
let attributes: Option<&rspack_core::ImportAttributes> = data.attributes.as_ref();
345+
let attributes_eq = attributes == dep.get_attributes();
346+
request_eq && attributes_eq
347+
})
348+
.map(|dep| DeferredPureCheck {
349+
atom: data.name.clone(),
350+
dep_id: *dep.id(),
351+
start: callee.span().real_lo(),
352+
end: callee.span().real_hi(),
353+
})
337354
}
338355

339356
impl SideEffectsParserPlugin {

0 commit comments

Comments
 (0)