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: reuse AssetConditions for banner plugin #8350

Merged
merged 1 commit into from
Nov 11, 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: 0 additions & 2 deletions Cargo.lock

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

Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use derivative::Derivative;
use napi::Either;
use napi_derive::napi;
use rspack_binding_values::JsChunk;
use rspack_binding_values::{into_asset_conditions, JsChunk, RawAssetConditions};
use rspack_error::Result;
use rspack_napi::threadsafe_function::ThreadsafeFunction;
use rspack_plugin_banner::{
BannerContent, BannerContentFnCtx, BannerPluginOptions, BannerRule, BannerRules,
};
use rspack_regex::RspackRegex;
use rspack_plugin_banner::{BannerContent, BannerContentFnCtx, BannerPluginOptions};

#[napi(object)]
pub struct RawBannerContentFnCtx {
Expand Down Expand Up @@ -45,11 +42,6 @@ impl TryFrom<RawBannerContentWrapper> for BannerContent {
}
}

type RawBannerRule = Either<String, RspackRegex>;
type RawBannerRules = Either<RawBannerRule, Vec<RawBannerRule>>;
struct RawBannerRuleWrapper(RawBannerRule);
struct RawBannerRulesWrapper(RawBannerRules);

#[derive(Derivative)]
#[derivative(Debug)]
#[napi(object, object_to_js = false)]
Expand All @@ -62,32 +54,11 @@ pub struct RawBannerPluginOptions {
pub raw: Option<bool>,
pub stage: Option<i32>,
#[napi(ts_type = "string | RegExp | (string | RegExp)[]")]
pub test: Option<RawBannerRules>,
pub test: Option<RawAssetConditions>,
#[napi(ts_type = "string | RegExp | (string | RegExp)[]")]
pub include: Option<RawBannerRules>,
pub include: Option<RawAssetConditions>,
#[napi(ts_type = "string | RegExp | (string | RegExp)[]")]
pub exclude: Option<RawBannerRules>,
}

impl From<RawBannerRuleWrapper> for BannerRule {
fn from(x: RawBannerRuleWrapper) -> Self {
match x.0 {
Either::A(s) => BannerRule::String(s),
Either::B(r) => BannerRule::Regexp(r),
}
}
}

impl From<RawBannerRulesWrapper> for BannerRules {
fn from(x: RawBannerRulesWrapper) -> Self {
match x.0 {
Either::A(v) => BannerRules::Single(RawBannerRuleWrapper(v).into()),
Either::B(v) => v
.into_iter()
.map(|v| RawBannerRuleWrapper(v).into())
.collect(),
}
}
pub exclude: Option<RawAssetConditions>,
}

impl TryFrom<RawBannerPluginOptions> for BannerPluginOptions {
Expand All @@ -99,9 +70,9 @@ impl TryFrom<RawBannerPluginOptions> for BannerPluginOptions {
footer: value.footer,
raw: value.raw,
stage: value.stage,
test: value.test.map(|v| RawBannerRulesWrapper(v).into()),
include: value.include.map(|v| RawBannerRulesWrapper(v).into()),
exclude: value.exclude.map(|v| RawBannerRulesWrapper(v).into()),
test: value.test.map(into_asset_conditions),
include: value.include.map(into_asset_conditions),
exclude: value.exclude.map(into_asset_conditions),
})
}
}
18 changes: 8 additions & 10 deletions crates/rspack_plugin_banner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-recursion = { workspace = true }
cow-utils = { workspace = true }
futures = { workspace = true }
regex = { workspace = true }
rspack_core = { version = "0.1.0", path = "../rspack_core" }
rspack_error = { version = "0.1.0", path = "../rspack_error" }
rspack_hook = { version = "0.1.0", path = "../rspack_hook" }
rspack_regex = { version = "0.1.0", path = "../rspack_regex" }
rspack_util = { version = "0.1.0", path = "../rspack_util" }
tracing = { workspace = true }
cow-utils = { workspace = true }
futures = { workspace = true }
regex = { workspace = true }
rspack_core = { version = "0.1.0", path = "../rspack_core" }
rspack_error = { version = "0.1.0", path = "../rspack_error" }
rspack_hook = { version = "0.1.0", path = "../rspack_hook" }
rspack_util = { version = "0.1.0", path = "../rspack_util" }
tracing = { workspace = true }

[package.metadata.cargo-shear]
ignored = ["tracing"]
67 changes: 14 additions & 53 deletions crates/rspack_plugin_banner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::fmt::{self, Debug};
use std::sync::LazyLock;

use async_recursion::async_recursion;
use cow_utils::CowUtils;
use futures::future::BoxFuture;
use regex::Regex;
Expand All @@ -14,45 +13,8 @@ use rspack_core::{
};
use rspack_error::Result;
use rspack_hook::{plugin, plugin_hook};
use rspack_regex::RspackRegex;
use rspack_util::{infallible::ResultInfallibleExt as _, try_any_sync};

#[derive(Debug)]
pub enum BannerRule {
String(String),
Regexp(RspackRegex),
}

#[derive(Debug)]
pub enum BannerRules {
Single(BannerRule),
Array(Vec<BannerRule>),
}

impl FromIterator<BannerRule> for BannerRules {
fn from_iter<T: IntoIterator<Item = BannerRule>>(iter: T) -> Self {
Self::Array(iter.into_iter().collect())
}
}

impl BannerRule {
pub fn try_match(&self, data: &str) -> Result<bool> {
match self {
Self::String(s) => Ok(data.starts_with(s)),
Self::Regexp(r) => Ok(r.test(data)),
}
}
}

impl BannerRules {
#[async_recursion]
pub async fn try_match(&self, data: &str) -> Result<bool> {
match self {
Self::Single(s) => s.try_match(data),
Self::Array(l) => try_any_sync(l, |i| i.try_match(data)),
}
}
}
use rspack_util::asset_condition::AssetConditions;
use rspack_util::infallible::ResultInfallibleExt as _;

#[derive(Debug)]
pub struct BannerPluginOptions {
Expand All @@ -65,11 +27,11 @@ pub struct BannerPluginOptions {
// If true, banner will not be wrapped in a comment.
pub raw: Option<bool>,
// Include all modules that pass test assertion.
pub test: Option<BannerRules>,
pub test: Option<AssetConditions>,
// Include all modules matching any of these conditions.
pub include: Option<BannerRules>,
pub include: Option<AssetConditions>,
// Exclude all modules matching any of these conditions.
pub exclude: Option<BannerRules>,
pub exclude: Option<AssetConditions>,
// Specifies the stage of banner.
pub stage: Option<i32>,
}
Expand Down Expand Up @@ -97,24 +59,23 @@ impl fmt::Debug for BannerContent {
}
}

#[async_recursion]
async fn match_object(obj: &BannerPluginOptions, str: &str) -> Result<bool> {
fn match_object(obj: &BannerPluginOptions, str: &str) -> bool {
if let Some(condition) = &obj.test {
if !condition.try_match(str).await? {
return Ok(false);
if !condition.try_match(str) {
return false;
}
}
if let Some(condition) = &obj.include {
if !condition.try_match(str).await? {
return Ok(false);
if !condition.try_match(str) {
return false;
}
}
if let Some(condition) = &obj.exclude {
if condition.try_match(str).await? {
return Ok(false);
if condition.try_match(str) {
return false;
}
}
Ok(true)
true
}

static TRIALING_WHITESPACE: LazyLock<Regex> =
Expand Down Expand Up @@ -196,7 +157,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
}

for file in &chunk.files {
let is_match = match_object(&self.config, file).await.unwrap_or(false);
let is_match = match_object(&self.config, file);

if !is_match {
continue;
Expand Down
Loading