Skip to content

Commit

Permalink
refactor(rspack): remove RawRegex and JsRegExp (#8298)
Browse files Browse the repository at this point in the history
* feat: impl trait From

* feat: impl napi trait for RspackRegex

* fix: remove error code

* refactor: remove JsRegExp

* chore: remove unused dependency

* refactor: remove RawRegex

* chore: update snapshots

* chore: remove unnecessary code
  • Loading branch information
shulaoda authored Nov 4, 2024
1 parent 2e89bca commit 4d1c348
Show file tree
Hide file tree
Showing 30 changed files with 194 additions and 307 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.

19 changes: 7 additions & 12 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ export class JsContextModuleFactoryAfterResolveData {
set context(context: string)
get request(): string
set request(request: string)
get regExp(): RawRegex | undefined
set regExp(rawRegExp: RawRegex | undefined)
get regExp(): RegExp | undefined
set regExp(rawRegExp: RegExp | undefined)
get recursive(): boolean
set recursive(recursive: boolean)
get dependencies(): Array<JsDependencyMut>
Expand All @@ -111,8 +111,8 @@ export class JsContextModuleFactoryBeforeResolveData {
set context(context: string)
get request(): string
set request(request: string)
get regExp(): RawRegex | undefined
set regExp(rawRegExp: RawRegex | undefined)
get regExp(): RegExp | undefined
set regExp(rawRegExp: RegExp | undefined)
get recursive(): boolean
set recursive(recursive: boolean)
}
Expand Down Expand Up @@ -1189,10 +1189,10 @@ export interface RawContainerReferencePluginOptions {
}

export interface RawContextReplacementPluginOptions {
resourceRegExp: RawRegex
resourceRegExp: RegExp
newContentResource?: string
newContentRecursive?: boolean
newContentRegExp?: RawRegex
newContentRegExp?: RegExp
newContentCreateContextMap?: Record<string, string>
}

Expand Down Expand Up @@ -1723,11 +1723,6 @@ export interface RawProvideOptions {
strictVersion?: boolean
}

export interface RawRegex {
source: string
flags: string
}

export interface RawRelated {
sourceMap?: string
}
Expand Down Expand Up @@ -1801,7 +1796,7 @@ export interface RawRspackFuture {
export interface RawRuleSetCondition {
type: RawRuleSetConditionType
string?: string
regexp?: RawRegex
regexp?: RegExp
logical?: Array<RawRuleSetLogicalConditions>
array?: Array<RawRuleSetCondition>
func?: (value: string) => boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ use napi::Either;
use napi_derive::napi;
use rspack_binding_values::JsChunk;
use rspack_error::Result;
use rspack_napi::{
regexp::{JsRegExp, JsRegExpExt},
threadsafe_function::ThreadsafeFunction,
};
use rspack_napi::threadsafe_function::ThreadsafeFunction;
use rspack_plugin_banner::{
BannerContent, BannerContentFnCtx, BannerPluginOptions, BannerRule, BannerRules,
};
use rspack_regex::RspackRegex;

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

type RawBannerRule = Either<String, JsRegExp>;
type RawBannerRule = Either<String, RspackRegex>;
type RawBannerRules = Either<RawBannerRule, Vec<RawBannerRule>>;
struct RawBannerRuleWrapper(RawBannerRule);
struct RawBannerRulesWrapper(RawBannerRules);
Expand Down Expand Up @@ -75,7 +73,7 @@ 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.to_rspack_regex()),
Either::B(r) => BannerRule::Regexp(r),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
use napi_derive::napi;
use rspack_napi::{
regexp::{JsRegExp, JsRegExpExt},
threadsafe_function::ThreadsafeFunction,
};
use rspack_napi::threadsafe_function::ThreadsafeFunction;
use rspack_plugin_ignore::{CheckResourceContent, IgnorePluginOptions};
use rspack_regex::RspackRegex;

type RawCheckResource = ThreadsafeFunction<(String, String), bool>;

#[derive(Debug)]
#[napi(object, object_to_js = false)]
pub struct RawIgnorePluginOptions {
#[napi(ts_type = "RegExp")]
pub resource_reg_exp: Option<JsRegExp>,
pub resource_reg_exp: Option<RspackRegex>,
#[napi(ts_type = "RegExp")]
pub context_reg_exp: Option<JsRegExp>,
pub context_reg_exp: Option<RspackRegex>,
#[napi(ts_type = "(resource: string, context: string) => boolean")]
pub check_resource: Option<RawCheckResource>,
}

impl From<RawIgnorePluginOptions> for IgnorePluginOptions {
fn from(value: RawIgnorePluginOptions) -> Self {
Self {
resource_reg_exp: value
.resource_reg_exp
.map(|resource_reg_exp| resource_reg_exp.to_rspack_regex()),
context_reg_exp: value
.context_reg_exp
.map(|context_reg_exp| context_reg_exp.to_rspack_regex()),
resource_reg_exp: value.resource_reg_exp,
context_reg_exp: value.context_reg_exp,

check_resource: value.check_resource.map(|check_resource| {
CheckResourceContent::Fn(Box::new(move |resource, context| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use napi::{
Either,
};
use napi_derive::napi;
use rspack_binding_values::{JsModuleWrapper, RawRegex};
use rspack_binding_values::JsModuleWrapper;
use rspack_core::ModuleIdentifier;
use rspack_napi::threadsafe_function::ThreadsafeFunction;
use rspack_plugin_lazy_compilation::{
Expand All @@ -14,7 +14,7 @@ use rspack_regex::RspackRegex;

#[derive(Debug)]
pub struct RawLazyCompilationTest<F = ThreadsafeFunction<JsModuleWrapper, Option<bool>>>(
pub Either<RawRegex, F>,
pub Either<RspackRegex, F>,
);

impl<F: FromNapiValue + ValidateNapiValue> FromNapiValue for RawLazyCompilationTest<F> {
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_binding_options/src/options/raw_external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use napi::bindgen_prelude::Either4;
use napi_derive::napi;
use rspack_core::ExternalItemFnCtx;
use rspack_core::{ExternalItem, ExternalItemFnResult, ExternalItemValue};
use rspack_napi::regexp::{JsRegExp, JsRegExpExt};
use rspack_napi::threadsafe_function::ThreadsafeFunction;
use rspack_regex::RspackRegex;

#[napi(object)]
pub struct RawHttpExternalsRspackPluginOptions {
Expand All @@ -25,7 +25,7 @@ pub struct RawExternalsPluginOptions {

type RawExternalItem = Either4<
String,
JsRegExp,
RspackRegex,
HashMap<String, RawExternalItemValue>,
ThreadsafeFunction<RawExternalItemFnCtx, RawExternalItemFnResult>,
>;
Expand Down Expand Up @@ -97,7 +97,7 @@ impl TryFrom<RawExternalItemWrapper> for ExternalItem {
fn try_from(value: RawExternalItemWrapper) -> rspack_error::Result<Self> {
match value.0 {
Either4::A(v) => Ok(Self::String(v)),
Either4::B(v) => Ok(Self::RegExp(v.to_rspack_regex())),
Either4::B(v) => Ok(Self::RegExp(v)),
Either4::C(v) => Ok(Self::Object(
v.into_iter()
.map(|(k, v)| (k, RawExternalItemValueWrapper(v).into()))
Expand Down
16 changes: 7 additions & 9 deletions crates/rspack_binding_options/src/options/raw_module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use derivative::Derivative;
use napi::bindgen_prelude::Either3;
use napi::Either;
use napi_derive::napi;
use rspack_binding_values::{JsFilename, RawRegex};
use rspack_binding_values::JsFilename;
use rspack_core::{
AssetGeneratorDataUrl, AssetGeneratorDataUrlFnArgs, AssetGeneratorDataUrlOptions,
AssetGeneratorOptions, AssetInlineGeneratorOptions, AssetParserDataUrl,
Expand All @@ -19,7 +19,6 @@ use rspack_core::{
ParserOptionsMap,
};
use rspack_error::error;
use rspack_napi::regexp::{JsRegExp, JsRegExpExt};
use rspack_napi::threadsafe_function::ThreadsafeFunction;
use rspack_regex::RspackRegex;

Expand Down Expand Up @@ -50,7 +49,8 @@ impl Debug for RawModuleRuleUse {
#[rspack_napi_macros::tagged_union]
pub enum RawRuleSetCondition {
string(String),
regexp(RawRegex),
#[napi(ts_type = "RegExp")]
regexp(RspackRegex),
logical(Vec<RawRuleSetLogicalConditions>),
array(Vec<RawRuleSetCondition>),
#[napi(ts_type = r#"(value: string) => boolean"#)]
Expand Down Expand Up @@ -265,7 +265,7 @@ pub struct RawJavascriptParserOptions {
pub expr_context_critical: Option<bool>,
pub wrapped_context_critical: Option<bool>,
#[napi(ts_type = "RegExp")]
pub wrapped_context_reg_exp: Option<JsRegExp>,
pub wrapped_context_reg_exp: Option<RspackRegex>,
pub exports_presence: Option<String>,
pub import_exports_presence: Option<String>,
pub reexport_exports_presence: Option<String>,
Expand Down Expand Up @@ -304,9 +304,7 @@ impl From<RawJavascriptParserOptions> for JavascriptParserOptions {
.map(|x| DynamicImportFetchPriority::from(x.as_str())),
url: value.url.map(|v| JavascriptParserUrl::from(v.as_str())),
expr_context_critical: value.expr_context_critical,
wrapped_context_reg_exp: value
.wrapped_context_reg_exp
.map(|context_reg_exp| context_reg_exp.to_rspack_regex()),
wrapped_context_reg_exp: value.wrapped_context_reg_exp,
wrapped_context_critical: value.wrapped_context_critical,
exports_presence: value
.exports_presence
Expand Down Expand Up @@ -868,7 +866,7 @@ impl TryFrom<RawModuleOptions> for ModuleOptions {
}
}

type RawModuleNoParseRule = Either3<String, JsRegExp, ThreadsafeFunction<String, Option<bool>>>;
type RawModuleNoParseRule = Either3<String, RspackRegex, ThreadsafeFunction<String, Option<bool>>>;
type RawModuleNoParseRules = Either<RawModuleNoParseRule, Vec<RawModuleNoParseRule>>;

struct RawModuleNoParseRuleWrapper(RawModuleNoParseRule);
Expand All @@ -888,7 +886,7 @@ impl From<RawModuleNoParseRuleWrapper> for ModuleNoParseRule {
fn from(x: RawModuleNoParseRuleWrapper) -> Self {
match x.0 {
Either3::A(v) => Self::AbsPathPrefix(v),
Either3::B(v) => Self::Regexp(v.to_rspack_regex()),
Either3::B(v) => Self::Regexp(v),
Either3::C(v) => Self::TestFn(js_func_to_no_parse_test_func(v)),
}
}
Expand Down
28 changes: 11 additions & 17 deletions crates/rspack_binding_options/src/options/raw_split_chunks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use raw_split_chunk_name::RawChunkOptionName;
use rspack_core::Filename;
use rspack_core::SourceType;
use rspack_core::DEFAULT_DELIMITER;
use rspack_napi::regexp::{JsRegExp, JsRegExpExt};
use rspack_napi::string::JsStringExt;
use rspack_plugin_split_chunks::ChunkNameGetter;
use rspack_regex::RspackRegex;

use self::raw_split_chunk_cache_group_test::default_cache_group_test;
use self::raw_split_chunk_cache_group_test::normalize_raw_cache_group_test;
Expand Down Expand Up @@ -74,10 +74,10 @@ pub struct RawCacheGroupOptions {
pub chunks: Option<Chunks>,
#[napi(ts_type = "RegExp | string")]
#[derivative(Debug = "ignore")]
pub r#type: Option<Either<JsRegExp, JsString>>,
pub r#type: Option<Either<RspackRegex, JsString>>,
#[napi(ts_type = "RegExp | string")]
#[derivative(Debug = "ignore")]
pub layer: Option<Either<JsRegExp, JsString>>,
pub layer: Option<Either<RspackRegex, JsString>>,
pub automatic_name_delimiter: Option<String>,
// pub max_async_requests: usize,
// pub max_initial_requests: usize,
Expand Down Expand Up @@ -283,13 +283,10 @@ pub struct RawFallbackCacheGroupOptions {
}

fn create_module_type_filter(
raw: Either<JsRegExp, JsString>,
raw: Either<RspackRegex, JsString>,
) -> rspack_plugin_split_chunks::ModuleTypeFilter {
match raw {
Either::A(js_reg) => {
let regex = js_reg.to_rspack_regex();
Arc::new(move |m| regex.test(m.module_type().as_str()))
}
Either::A(regex) => Arc::new(move |m| regex.test(m.module_type().as_str())),
Either::B(js_str) => {
let type_str = js_str.into_string();
Arc::new(move |m| m.module_type().as_str() == type_str.as_str())
Expand All @@ -298,17 +295,14 @@ fn create_module_type_filter(
}

fn create_module_layer_filter(
raw: Either<JsRegExp, JsString>,
raw: Either<RspackRegex, JsString>,
) -> rspack_plugin_split_chunks::ModuleLayerFilter {
match raw {
Either::A(js_reg) => {
let regex = js_reg.to_rspack_regex();
Arc::new(move |m| {
m.get_layer()
.map(|layer| regex.test(layer))
.unwrap_or_default()
})
}
Either::A(regex) => Arc::new(move |m| {
m.get_layer()
.map(|layer| regex.test(layer))
.unwrap_or_default()
}),
Either::B(js_str) => {
let test = js_str.into_string();
Arc::new(move |m| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use std::sync::Arc;
use napi::bindgen_prelude::{Either3, FromNapiValue};
use napi_derive::napi;
use rspack_binding_values::JsModuleWrapper;
use rspack_napi::regexp::{JsRegExp, JsRegExpExt};
use rspack_napi::threadsafe_function::ThreadsafeFunction;
use rspack_plugin_split_chunks::{CacheGroupTest, CacheGroupTestFnCtx};
use rspack_regex::RspackRegex;

pub(super) type RawCacheGroupTest =
Either3<String, JsRegExp, ThreadsafeFunction<JsCacheGroupTestCtx, Option<bool>>>;
Either3<String, RspackRegex, ThreadsafeFunction<JsCacheGroupTestCtx, Option<bool>>>;

#[napi(object, object_from_js = false)]
pub struct JsCacheGroupTestCtx {
Expand Down Expand Up @@ -37,7 +37,7 @@ pub(super) fn normalize_raw_cache_group_test(raw: RawCacheGroupTest) -> CacheGro
use pollster::block_on;
match raw {
Either3::A(str) => CacheGroupTest::String(str),
Either3::B(regexp) => CacheGroupTest::RegExp(regexp.to_rspack_regex()),
Either3::B(regexp) => CacheGroupTest::RegExp(regexp),
Either3::C(v) => CacheGroupTest::Fn(Arc::new(move |ctx| block_on(v.call(ctx.into())))),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ use std::sync::Arc;

use napi::{bindgen_prelude::Either3, JsString};
use rspack_binding_values::JsChunk;
use rspack_napi::regexp::{JsRegExp, JsRegExpExt};
use rspack_napi::string::JsStringExt;
use rspack_napi::threadsafe_function::ThreadsafeFunction;
use rspack_regex::RspackRegex;

pub type Chunks = Either3<JsRegExp, JsString, ThreadsafeFunction<JsChunk, bool>>;
pub type Chunks = Either3<RspackRegex, JsString, ThreadsafeFunction<JsChunk, bool>>;

pub fn create_chunks_filter(raw: Chunks) -> rspack_plugin_split_chunks::ChunkFilter {
use pollster::block_on;
match raw {
Either3::A(reg) => {
rspack_plugin_split_chunks::create_regex_chunk_filter_from_str(reg.to_rspack_regex())
}
Either3::A(regex) => rspack_plugin_split_chunks::create_regex_chunk_filter_from_str(regex),
Either3::B(js_str) => {
let js_str = js_str.into_string();
rspack_plugin_split_chunks::create_chunk_filter_from_str(&js_str)
Expand Down
Loading

2 comments on commit 4d1c348

@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 ❌ failure
_selftest ✅ success
rspress ✅ success
rslib ✅ success
rsbuild ❌ failure
examples ❌ failure
devserver ✅ success

@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-11-04 a987332) Current Change
10000_big_production-mode + exec 45.9 s ± 613 ms 43.9 s ± 877 ms -4.35 %
10000_development-mode + exec 1.84 s ± 17 ms 1.82 s ± 17 ms -1.17 %
10000_development-mode_hmr + exec 645 ms ± 12 ms 644 ms ± 6.8 ms -0.20 %
10000_production-mode + exec 2.4 s ± 19 ms 2.41 s ± 29 ms +0.58 %
arco-pro_development-mode + exec 1.79 s ± 63 ms 1.76 s ± 60 ms -1.68 %
arco-pro_development-mode_hmr + exec 429 ms ± 1.2 ms 430 ms ± 1.7 ms +0.33 %
arco-pro_production-mode + exec 3.19 s ± 87 ms 3.2 s ± 87 ms +0.46 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.27 s ± 50 ms 3.23 s ± 86 ms -0.95 %
threejs_development-mode_10x + exec 1.58 s ± 7.7 ms 1.59 s ± 22 ms +0.32 %
threejs_development-mode_10x_hmr + exec 784 ms ± 4.2 ms 780 ms ± 9.9 ms -0.46 %
threejs_production-mode_10x + exec 4.93 s ± 40 ms 4.96 s ± 33 ms +0.63 %

Please sign in to comment.