Skip to content

Commit

Permalink
Merge branch 'main' into perf-ensure-conditions-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Jun 11, 2024
2 parents 6466809 + 0b71ac1 commit d17299d
Show file tree
Hide file tree
Showing 76 changed files with 1,477 additions and 632 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ 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" }
bitflags = { version = "2.5.0" }
concat-string = "1.0.1"
css-module-lexer = "0.0.11"
css-module-lexer = "0.0.12"
dashmap = { version = "5.5.3" }
derivative = { version = "2.2.0" }
futures = { version = "0.3.30" }
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_binding_options/src/options/raw_module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub struct RawRuleSetCondition {
pub logical_matcher: Option<Vec<RawRuleSetLogicalConditions>>,
pub array_matcher: Option<Vec<RawRuleSetCondition>>,
#[napi(ts_type = r#"(value: string) => boolean"#)]
pub func_matcher: Option<ThreadsafeFunction<String, bool>>,
pub func_matcher: Option<ThreadsafeFunction<serde_json::Value, bool>>,
}

impl Debug for RawRuleSetCondition {
Expand Down Expand Up @@ -200,8 +200,8 @@ impl TryFrom<RawRuleSetCondition> for rspack_core::RuleSetCondition {
"should have a func_matcher when RawRuleSetCondition.type is \"function\""
)
})?;
Self::Func(Box::new(move|data: &str| {
let data = data.to_string();
Self::Func(Box::new(move|data: &serde_json::Value| {
let data = data.clone();
let func_matcher = func_matcher.clone();
Box::pin(async move { func_matcher.call(data).await })
}))
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);
}
}
15 changes: 10 additions & 5 deletions crates/rspack_core/src/options/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ impl Default for CssExportsConvention {
pub type DescriptionData = HashMap<String, RuleSetCondition>;

pub type RuleSetConditionFnMatcher =
Box<dyn Fn(&str) -> BoxFuture<'static, Result<bool>> + Sync + Send>;
Box<dyn Fn(&serde_json::Value) -> BoxFuture<'static, Result<bool>> + Sync + Send>;

pub enum RuleSetCondition {
String(String),
Expand All @@ -482,10 +482,15 @@ impl fmt::Debug for RuleSetCondition {

impl RuleSetCondition {
#[async_recursion]
pub async fn try_match(&self, data: &str) -> Result<bool> {
pub async fn try_match(&self, data: &serde_json::Value) -> Result<bool> {
match self {
Self::String(s) => Ok(data.starts_with(s)),
Self::Regexp(r) => Ok(r.test(data)),
Self::String(s) => Ok(
data
.as_str()
.map(|data| data.starts_with(s))
.unwrap_or_default(),
),
Self::Regexp(r) => Ok(data.as_str().map(|data| r.test(data)).unwrap_or_default()),
Self::Logical(g) => g.try_match(data).await,
Self::Array(l) => try_any(l, |i| async { i.try_match(data).await }).await,
Self::Func(f) => f(data).await,
Expand All @@ -502,7 +507,7 @@ pub struct RuleSetLogicalConditions {

impl RuleSetLogicalConditions {
#[async_recursion]
pub async fn try_match(&self, data: &str) -> Result<bool> {
pub async fn try_match(&self, data: &serde_json::Value) -> Result<bool> {
if let Some(and) = &self.and
&& try_any(and, |i| async { i.try_match(data).await.map(|i| !i) }).await?
{
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/raw_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ impl Module for RawModule {
build_info: BuildInfo {
hash: Some(hasher.digest(&build_context.compiler_options.output.hash_digest)),
cacheable: true,
strict: true,
..Default::default()
},
dependencies: vec![],
Expand Down
46 changes: 34 additions & 12 deletions crates/rspack_core/src/utils/module_rules.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use async_recursion::async_recursion;
use rspack_error::Result;
use rspack_loader_runner::ResourceData;
use serde_json::Value;

use crate::{DependencyCategory, ModuleRule};

Expand All @@ -27,7 +28,9 @@ pub async fn module_rule_matcher<'a>(
matched_rules: &mut Vec<&'a ModuleRule>,
) -> Result<bool> {
if let Some(test_rule) = &module_rule.rspack_resource
&& !test_rule.try_match(&resource_data.resource).await?
&& !test_rule
.try_match(&Value::String(resource_data.resource.to_string()))
.await?
{
return Ok(false);
}
Expand All @@ -36,37 +39,46 @@ pub async fn module_rule_matcher<'a>(
// See: https://webpack.js.org/configuration/module/#ruletest
if let Some(test_rule) = &module_rule.test
&& !test_rule
.try_match(&resource_data.resource_path.to_string_lossy())
.try_match(&Value::String(
resource_data.resource_path.to_string_lossy().to_string(),
))
.await?
{
return Ok(false);
} else if let Some(resource_rule) = &module_rule.resource
&& !resource_rule
.try_match(&resource_data.resource_path.to_string_lossy())
.try_match(&Value::String(
resource_data.resource_path.to_string_lossy().to_string(),
))
.await?
{
return Ok(false);
}

if let Some(include_rule) = &module_rule.include
&& !include_rule
.try_match(&resource_data.resource_path.to_string_lossy())
.try_match(&Value::String(
resource_data.resource_path.to_string_lossy().to_string(),
))
.await?
{
return Ok(false);
}

if let Some(exclude_rule) = &module_rule.exclude
&& exclude_rule
.try_match(&resource_data.resource_path.to_string_lossy())
.try_match(&Value::String(
resource_data.resource_path.to_string_lossy().to_string(),
))
.await?
{
return Ok(false);
}

if let Some(resource_query_rule) = &module_rule.resource_query {
if let Some(resource_query) = &resource_data.resource_query {
if !resource_query_rule.try_match(resource_query).await? {
let resource_query = Value::String(resource_query.to_string());
if !resource_query_rule.try_match(&resource_query).await? {
return Ok(false);
}
} else {
Expand All @@ -76,8 +88,9 @@ pub async fn module_rule_matcher<'a>(

if let Some(resource_fragment_condition) = &module_rule.resource_fragment {
if let Some(resource_fragment) = &resource_data.resource_fragment {
let resource_fragment = Value::String(resource_fragment.to_string());
if !resource_fragment_condition
.try_match(resource_fragment)
.try_match(&resource_fragment)
.await?
{
return Ok(false);
Expand All @@ -89,7 +102,8 @@ pub async fn module_rule_matcher<'a>(

if let Some(mimetype_condition) = &module_rule.mimetype {
if let Some(mimetype) = &resource_data.mimetype {
if !mimetype_condition.try_match(mimetype).await? {
let mimetype = Value::String(mimetype.to_string());
if !mimetype_condition.try_match(&mimetype).await? {
return Ok(false);
}
} else {
Expand All @@ -102,28 +116,36 @@ pub async fn module_rule_matcher<'a>(
if scheme.is_none() {
return Ok(false);
}
if !scheme_condition.try_match(&scheme.to_string()).await? {
let scheme = Value::String(scheme.to_string());
if !scheme_condition.try_match(&scheme).await? {
return Ok(false);
}
}

if let Some(issuer_rule) = &module_rule.issuer
&& let Some(issuer) = issuer
&& !issuer_rule.try_match(issuer).await?
&& !issuer_rule
.try_match(&Value::String(issuer.to_string()))
.await?
{
return Ok(false);
}

if let Some(dependency_rule) = &module_rule.dependency
&& !dependency_rule.try_match(&dependency.to_string()).await?
&& !dependency_rule
.try_match(&Value::String(dependency.to_string()))
.await?
{
return Ok(false);
}

if let Some(description_data) = &module_rule.description_data {
if let Some(resource_description) = &resource_data.resource_description {
for (k, matcher) in description_data {
if let Some(v) = resource_description.json().get(k).and_then(|v| v.as_str()) {
if let Some(v) = k
.split('.')
.try_fold(resource_description.json(), |acc, key| acc.get(key))
{
if !matcher.try_match(v).await? {
return Ok(false);
}
Expand Down
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
Loading

0 comments on commit d17299d

Please sign in to comment.