From 8c26740bdc3aef068e8c3005bbd0fda64f2a66b0 Mon Sep 17 00:00:00 2001 From: CPunisher <1343316114@qq.com> Date: Mon, 23 Dec 2024 14:14:47 +0800 Subject: [PATCH] fix --- .../src/raw_options/raw_module/mod.rs | 2 +- crates/rspack_core/src/options/module.rs | 42 ++++++++----------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/crates/rspack_binding_values/src/raw_options/raw_module/mod.rs b/crates/rspack_binding_values/src/raw_options/raw_module/mod.rs index 92f2baea68f1..bd9d0e46a958 100644 --- a/crates/rspack_binding_values/src/raw_options/raw_module/mod.rs +++ b/crates/rspack_binding_values/src/raw_options/raw_module/mod.rs @@ -138,7 +138,7 @@ impl TryFrom for rspack_core::RuleSetCondition { } } -impl TryFrom for rspack_core::RuleSetConditionMatch { +impl TryFrom for rspack_core::RuleSetConditionWithEmpty { type Error = rspack_error::Error; fn try_from(x: RawRuleSetCondition) -> rspack_error::Result { diff --git a/crates/rspack_core/src/options/module.rs b/crates/rspack_core/src/options/module.rs index f5919f23457e..9ff790972011 100644 --- a/crates/rspack_core/src/options/module.rs +++ b/crates/rspack_core/src/options/module.rs @@ -12,7 +12,7 @@ use rspack_macros::MergeFrom; use rspack_regex::RspackRegex; use rspack_util::{try_all, try_any, MergeFrom}; use rustc_hash::FxHashMap as HashMap; -use tokio::sync::RwLock; +use tokio::sync::OnceCell; use crate::{Compilation, Filename, Module, ModuleType, PublicPath, Resolve}; @@ -561,8 +561,8 @@ impl Default for CssExportsConvention { } } -pub type DescriptionData = HashMap; -pub type With = HashMap; +pub type DescriptionData = HashMap; +pub type With = HashMap; pub type RuleSetConditionFnMatcher = Box BoxFuture<'static, Result> + Sync + Send>; @@ -654,16 +654,16 @@ impl RuleSetCondition { } #[derive(Debug)] -pub struct RuleSetConditionMatch { +pub struct RuleSetConditionWithEmpty { condition: RuleSetCondition, - match_when_empty: RwLock>, + match_when_empty: OnceCell, } -impl RuleSetConditionMatch { +impl RuleSetConditionWithEmpty { pub fn new(condition: RuleSetCondition) -> Self { Self { condition, - match_when_empty: RwLock::new(None), + match_when_empty: OnceCell::new(), } } @@ -672,17 +672,11 @@ impl RuleSetConditionMatch { } pub async fn match_when_empty(&self) -> Result { - if let Some(match_when_empty) = self.match_when_empty.read().await.as_ref() { - return Ok(*match_when_empty); - } - - Ok( - *self - .match_when_empty - .write() - .await - .insert(self.condition.match_when_empty().await?), - ) + self + .match_when_empty + .get_or_try_init(|| async { self.condition.match_when_empty().await }) + .await + .copied() } } @@ -767,13 +761,13 @@ pub struct ModuleRule { /// A condition matcher matching an absolute path. pub resource: Option, /// A condition matcher against the resource query. - pub resource_query: Option, - pub resource_fragment: Option, + pub resource_query: Option, + pub resource_fragment: Option, pub dependency: Option, - pub issuer: Option, - pub issuer_layer: Option, - pub scheme: Option, - pub mimetype: Option, + pub issuer: Option, + pub issuer_layer: Option, + pub scheme: Option, + pub mimetype: Option, pub description_data: Option, pub with: Option, pub one_of: Option>,