Skip to content

Commit

Permalink
feat: support wrappedContextRegExp
Browse files Browse the repository at this point in the history
  • Loading branch information
inottn committed Oct 17, 2024
1 parent fc0751c commit 7886b89
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 @@ -1436,6 +1436,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 @@ -181,6 +181,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

0 comments on commit 7886b89

Please sign in to comment.