Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: harmony import dependency parser plugin #6752

Merged
merged 4 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -303,7 +303,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 @@ -320,6 +319,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
Loading