Skip to content

Commit

Permalink
feat: support wrappedContextRegExp (#8149)
Browse files Browse the repository at this point in the history
  • Loading branch information
inottn authored Oct 25, 2024
1 parent 1b3ad8c commit e9ba1ba
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 7 deletions.
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,7 @@ export interface RawJavascriptParserOptions {
url?: string
exprContextCritical?: boolean
wrappedContextCritical?: boolean
wrappedContextRegExp?: RegExp
exportsPresence?: string
importExportsPresence?: string
reexportExportsPresence?: string
Expand Down
8 changes: 7 additions & 1 deletion crates/rspack_binding_options/src/options/raw_module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use rspack_core::{
use rspack_error::error;
use rspack_napi::regexp::{JsRegExp, JsRegExpExt};
use rspack_napi::threadsafe_function::ThreadsafeFunction;
use rspack_regex::RspackRegex;
use tokio::runtime::Handle;

use crate::RawResolveOptions;
Expand Down Expand Up @@ -110,7 +111,7 @@ impl TryFrom<RawRuleSetCondition> for rspack_core::RuleSetCondition {
let result = match x {
RawRuleSetCondition::string(s) => Self::String(s),
RawRuleSetCondition::regexp(r) => {
let reg = rspack_regex::RspackRegex::with_flags(&r.source, &r.flags)?;
let reg = RspackRegex::with_flags(&r.source, &r.flags)?;
Self::Regexp(reg)
}
RawRuleSetCondition::logical(mut l) => {
Expand Down Expand Up @@ -265,6 +266,8 @@ pub struct RawJavascriptParserOptions {
pub url: Option<String>,
pub expr_context_critical: Option<bool>,
pub wrapped_context_critical: Option<bool>,
#[napi(ts_type = "RegExp")]
pub wrapped_context_reg_exp: Option<JsRegExp>,
pub exports_presence: Option<String>,
pub import_exports_presence: Option<String>,
pub reexport_exports_presence: Option<String>,
Expand Down Expand Up @@ -303,6 +306,9 @@ 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_critical: value.wrapped_context_critical,
exports_presence: value
.exports_presence
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/options/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ pub struct JavascriptParserOptions {
pub url: Option<JavascriptParserUrl>,
pub expr_context_critical: Option<bool>,
pub wrapped_context_critical: Option<bool>,
pub wrapped_context_reg_exp: Option<RspackRegex>,
pub exports_presence: Option<ExportPresenceMode>,
pub import_exports_presence: Option<ExportPresenceMode>,
pub reexport_exports_presence: Option<ExportPresenceMode>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ use swc_core::ecma::ast::Expr;
use super::create_traceable_error;
use crate::utils::eval::{BasicEvaluatedExpression, TemplateStringKind};

// FIXME: delete this after `parserOptions.wrappedContextRegExp.source`
const DEFAULT_WRAPPED_CONTEXT_REGEXP: &str = ".*";

pub fn create_context_dependency(
param: &BasicEvaluatedExpression,
expr: &Expr,
parser: &mut crate::visitors::JavascriptParser,
) -> ContextModuleScanResult {
let mut critical = None;
let wrapped_context_reg_exp = parser
.javascript_options
.wrapped_context_reg_exp
.as_ref()
.expect("should have wrapped_context_reg_exp")
.source();

if param.is_template_string() {
let quasis = param.quasis();
Expand Down Expand Up @@ -47,10 +50,10 @@ pub fn create_context_dependency(
let reg = format!(
"^{}{}{}{}$",
quote_meta(&prefix),
DEFAULT_WRAPPED_CONTEXT_REGEXP,
wrapped_context_reg_exp,
quasis[1..quasis.len() - 1]
.iter()
.map(|q| quote_meta(q.string().as_str()) + DEFAULT_WRAPPED_CONTEXT_REGEXP)
.map(|q| quote_meta(q.string().as_str()) + wrapped_context_reg_exp)
.join(""),
quote_meta(&postfix)
);
Expand Down Expand Up @@ -148,7 +151,7 @@ pub fn create_context_dependency(
};

let reg = format!(
"^{}{DEFAULT_WRAPPED_CONTEXT_REGEXP}{}$",
"^{}{wrapped_context_reg_exp}{}$",
quote_meta(&prefix),
quote_meta(&postfix)
);
Expand Down
3 changes: 3 additions & 0 deletions crates/rspack_util/src/merge.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use rspack_regex::RspackRegex;

use crate::atom::Atom;

pub trait MergeFrom: Clone {
Expand Down Expand Up @@ -42,6 +44,7 @@ impl_merge_from!(u8, u16, u32, u64, u128);
impl_merge_from!(bool);
impl_merge_from!(String);
impl_merge_from!(Atom);
impl_merge_from!(RspackRegex);

pub fn merge_from_optional_with<T: MergeFrom>(
base: Option<T>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ Object {
...,
],
wrappedContextCritical: false,
wrappedContextRegExp: /\\.\\*/,
},
},
rules: Array [],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
it("should not include foo.js", async () => {
let a1 = 'a1';
let a2 = 'a2';
expect(require('./sub/' + a1)).toBe("a1");
expect(() => require('./sub/' + a2)).toThrow();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @type {import("@rspack/core").Configuration} */
module.exports = {
module: {
parser: {
javascript: {
wrappedContextRegExp: /.*1/,
}
}
},
}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "a1";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "a2";
Loading

2 comments on commit e9ba1ba

@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-10-25 562662e) Current Change
10000_development-mode + exec 2.11 s ± 25 ms 2.12 s ± 16 ms +0.46 %
10000_development-mode_hmr + exec 674 ms ± 13 ms 667 ms ± 13 ms -0.94 %
10000_production-mode + exec 2.7 s ± 38 ms 2.71 s ± 35 ms +0.19 %
arco-pro_development-mode + exec 1.77 s ± 92 ms 1.8 s ± 83 ms +1.34 %
arco-pro_development-mode_hmr + exec 426 ms ± 1.4 ms 427 ms ± 3.2 ms +0.10 %
arco-pro_production-mode + exec 3.19 s ± 61 ms 3.21 s ± 84 ms +0.57 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.26 s ± 74 ms 3.26 s ± 73 ms +0.11 %
threejs_development-mode_10x + exec 1.61 s ± 18 ms 1.62 s ± 18 ms +0.58 %
threejs_development-mode_10x_hmr + exec 761 ms ± 8.2 ms 760 ms ± 7.7 ms -0.10 %
threejs_production-mode_10x + exec 5.01 s ± 23 ms 5 s ± 31 ms -0.32 %

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

Please sign in to comment.