Skip to content

Commit

Permalink
refactor: harmony import dependency parser plugin (#6752)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk authored Jun 7, 2024
1 parent ac3cc13 commit 0d00059
Show file tree
Hide file tree
Showing 44 changed files with 960 additions and 464 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ resolver = "2" # See https://doc.rust-lang.org/cargo/reference/resolver

[workspace.dependencies]
anyhow = { version = "1.0.81", features = ["backtrace"] }
anymap = { version = "=1.0.0-beta.2" }
async-recursion = { version = "1.1.0" }
async-scoped = { version = "0.9.0" }
async-trait = { version = "0.1.79" }
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repository = "https://github.com/web-infra-dev/rspack"
version = "0.1.0"

[dependencies]
anymap = "1.0.0-beta.2"
anymap = { workspace = true }
async-recursion = { workspace = true }
async-trait = { workspace = true }
bitflags = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ mod t {
let module_deps_2 = ModuleDeps::from_module(&mg, &module1_id);

assert_eq!(module_deps_1, module_deps_2);
dbg!(module_deps_1.clone());

let dep2 = Box::new(TestDep::new(vec!["bar"]));
let dep2_id = *dep2.id();
Expand All @@ -316,6 +315,5 @@ mod t {

let module_deps_3 = ModuleDeps::from_module(&mg, &module_orig_id);
assert_ne!(module_deps_3, module_deps_1);
dbg!(module_deps_3);
}
}
2 changes: 1 addition & 1 deletion crates/rspack_loader_runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repository = "https://github.com/web-infra-dev/rspack"
version = "0.1.0"

[dependencies]
anymap = "1.0.0-beta.2"
anymap = { workspace = true }
async-recursion = { workspace = true }
async-trait = { workspace = true }
bitflags = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_javascript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ repository = "https://github.com/web-infra-dev/rspack"
version = "0.1.0"

[dependencies]
anymap = { workspace = true }
async-trait = { workspace = true }
bitflags = { workspace = true }
dashmap = { workspace = true }
Expand All @@ -28,7 +29,6 @@ rspack_ids = { path = "../rspack_ids/" }
rspack_regex = { path = "../rspack_regex" }
rspack_util = { path = "../rspack_util" }
rustc-hash = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
sugar_path = { workspace = true }
swc_core = { workspace = true, features = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ pub struct HarmonyImportSideEffectDependency {
pub id: DependencyId,
pub span: Option<ErrorSpan>,
pub source_span: Option<ErrorSpan>,
pub specifiers: Vec<Specifier>,
pub dependency_type: DependencyType,
pub export_all: bool,
resource_identifier: String,
Expand All @@ -78,7 +77,6 @@ impl HarmonyImportSideEffectDependency {
source_order: i32,
span: Option<ErrorSpan>,
source_span: Option<ErrorSpan>,
specifiers: Vec<Specifier>,
dependency_type: DependencyType,
export_all: bool,
) -> Self {
Expand All @@ -89,7 +87,6 @@ impl HarmonyImportSideEffectDependency {
request,
span,
source_span,
specifiers,
dependency_type,
export_all,
resource_identifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ impl DependencyTemplate for HarmonyImportSpecifierDependency {
}
} else {
harmony_import_dependency_apply(self, self.source_order, code_generatable_context);
// dbg!(&self.shorthand, self.asi_safe);
export_from_import(
code_generatable_context,
true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ use crate::{

const NESTED_WEBPACK_IDENTIFIER_TAG: &str = "_identifier__nested_webpack_identifier__";

#[derive(serde::Deserialize, serde::Serialize, Clone)]
#[derive(Debug, Clone)]
struct NestedRequireData {
name: String,
update: bool,
loc: DependencyLocation,
}

impl TagInfoData for NestedRequireData {
fn serialize(data: &Self) -> serde_json::Value {
serde_json::to_value(data).expect("serialize failed for `NestedRequireData`")
fn into_any(data: Self) -> Box<dyn anymap::CloneAny + 'static> {
Box::new(data)
}

fn deserialize(value: serde_json::Value) -> Self {
serde_json::from_value(value).expect("deserialize failed for `NestedRequireData`")
fn downcast(any: Box<dyn anymap::CloneAny>) -> Self {
*(any as Box<dyn std::any::Any>)
.downcast()
.expect("NestedRequireData should be downcasted from correct tag info")
}
}

Expand Down Expand Up @@ -124,37 +126,28 @@ impl JavascriptParserPlugin for CompatibilityPlugin {
if name != RuntimeGlobals::REQUIRE.name() {
return None;
}
let Some(variable_info) = parser.get_mut_variable_info(name) else {
return None;
};
let tag_info = parser
.definitions_db
.expect_get_mut_tag_info(&parser.current_tag_info?);

// FIXME: should find the `tag_info` which tag equal `NESTED_WEBPACK_IDENTIFIER_TAG`;
let Some(tag_info) = &mut variable_info.tag_info else {
unreachable!();
};
if tag_info.tag != NESTED_WEBPACK_IDENTIFIER_TAG {
return None;
}
let Some(data) = tag_info.data.as_mut().map(std::mem::take) else {
unreachable!();
};
let mut nested_require_data = NestedRequireData::deserialize(data);
let mut nested_require_data = NestedRequireData::downcast(tag_info.data.take()?);
let mut deps = Vec::with_capacity(2);
let name = nested_require_data.name.clone();
if !nested_require_data.update {
deps.push(ConstDependency::new(
nested_require_data.loc.start(),
nested_require_data.loc.end(),
nested_require_data.name.clone().into(),
name.clone().into(),
None,
));
nested_require_data.update = true;
}
tag_info.data = Some(NestedRequireData::serialize(&nested_require_data));
tag_info.data = Some(NestedRequireData::into_any(nested_require_data));

deps.push(ConstDependency::new(
ident.span.real_lo(),
ident.span.real_hi(),
nested_require_data.name.into(),
name.into(),
None,
));
for dep in deps {
Expand Down
48 changes: 41 additions & 7 deletions crates/rspack_plugin_javascript/src/parser_plugin/drive.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use swc_core::atoms::Atom;
use swc_core::common::Span;
use swc_core::ecma::ast::{
BinExpr, CallExpr, Callee, CondExpr, ExportDecl, ExportDefaultDecl, Expr, OptChainExpr,
Expand Down Expand Up @@ -110,18 +111,51 @@ impl JavascriptParserPlugin for JavaScriptParserPluginDrive {
None
}

fn member_chain(
&self,
parser: &mut JavascriptParser,
expr: &swc_core::ecma::ast::MemberExpr,
for_name: &str,
members: &[Atom],
members_optionals: &[bool],
member_ranges: &[Span],
) -> Option<bool> {
for plugin in &self.plugins {
let res = plugin.member_chain(
parser,
expr,
for_name,
members,
members_optionals,
member_ranges,
);
// `SyncBailHook`
if res.is_some() {
return res;
}
}
None
}

fn call_member_chain(
&self,
parser: &mut JavascriptParser,
root_info: &ExportedVariableInfo,
expr: &CallExpr,
// TODO: members: &Vec<String>,
// TODO: members_optionals: Vec<bool>,
// TODO: members_ranges: Vec<DependencyLoc>
for_name: &str,
members: &[Atom],
members_optionals: &[bool],
member_ranges: &[Span],
) -> Option<bool> {
assert!(matches!(expr.callee, Callee::Expr(_)));
for plugin in &self.plugins {
let res = plugin.call_member_chain(parser, root_info, expr);
let res = plugin.call_member_chain(
parser,
expr,
for_name,
members,
members_optionals,
member_ranges,
);
// `SyncBailHook`
if res.is_some() {
return res;
Expand Down Expand Up @@ -444,8 +478,8 @@ impl JavascriptParserPlugin for JavaScriptParserPluginDrive {
parser: &mut JavascriptParser,
statement: &swc_core::ecma::ast::ImportDecl,
source: &swc_core::atoms::Atom,
export_name: Option<&str>,
identifier_name: &str,
export_name: Option<&Atom>,
identifier_name: &Atom,
) -> Option<bool> {
for plugin in &self.plugins {
let res = plugin.import_specifier(parser, statement, source, export_name, identifier_name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,7 @@ fn handle_esm_export_harmony_import_side_effects_dep(
});
}

handle_harmony_import_side_effects_dep(
parser,
request,
span,
source_span,
specifiers,
dep_type,
export_all,
)
handle_harmony_import_side_effects_dep(parser, request, span, source_span, dep_type, export_all)
}
pub struct HarmonyExportDependencyParserPlugin;

Expand Down
Loading

2 comments on commit 0d00059

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-06-07 c0c0fd3) Current Change
10000_development-mode + exec 2.24 s ± 15 ms 2.23 s ± 16 ms -0.31 %
10000_development-mode_hmr + exec 739 ms ± 5.1 ms 735 ms ± 14 ms -0.49 %
10000_production-mode + exec 2.58 s ± 28 ms 2.58 s ± 17 ms +0.20 %
arco-pro_development-mode + exec 1.94 s ± 84 ms 1.94 s ± 72 ms -0.22 %
arco-pro_development-mode_hmr + exec 441 ms ± 2 ms 442 ms ± 3.2 ms +0.20 %
arco-pro_production-mode + exec 3.51 s ± 72 ms 3.55 s ± 55 ms +1.13 %
threejs_development-mode_10x + exec 1.4 s ± 8.4 ms 1.41 s ± 15 ms +0.34 %
threejs_development-mode_10x_hmr + exec 808 ms ± 7.4 ms 807 ms ± 6.6 ms -0.09 %
threejs_production-mode_10x + exec 4.71 s ± 35 ms 4.72 s ± 25 ms +0.37 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ✅ success
_selftest ✅ success
nx ✅ success
rspress ❌ failure
rsbuild ❌ failure
compat ✅ success
examples ✅ success

Please sign in to comment.