From a6107a84b7139cfa4be7976cb3010ee482612b73 Mon Sep 17 00:00:00 2001 From: xusd320 Date: Fri, 8 Nov 2024 16:24:43 +0800 Subject: [PATCH 1/5] fix: regexpr for nodejs intenal modules igonre --- crates/mako/src/features/node.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/mako/src/features/node.rs b/crates/mako/src/features/node.rs index 50356e822..9387f0884 100644 --- a/crates/mako/src/features/node.rs +++ b/crates/mako/src/features/node.rs @@ -21,7 +21,7 @@ impl Node { config.targets = HashMap::from([("node".into(), *target)]); // ignore all built-in node modules config.ignores.push(format!( - "^(node:)?({})(/|$)", + "^(node:)?({})(/.+|$)", Self::get_all_node_modules().join("|") )); // polifyll __dirname & __filename is supported with MockFilenameAndDirname Visitor From 5107f57cd6d677c96adb943e70e00d9058220715 Mon Sep 17 00:00:00 2001 From: xusd320 Date: Fri, 8 Nov 2024 16:27:26 +0800 Subject: [PATCH 2/5] feat: not replace unresolved nodejs require to __mako_require__ --- crates/mako/src/generate/transform.rs | 1 + crates/mako/src/visitors/mako_require.rs | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/crates/mako/src/generate/transform.rs b/crates/mako/src/generate/transform.rs index 972740311..807a0a966 100644 --- a/crates/mako/src/generate/transform.rs +++ b/crates/mako/src/generate/transform.rs @@ -247,6 +247,7 @@ pub fn transform_js_generate(transform_js_param: TransformJsParam) -> Result<()> let mut mako_require = MakoRequire { ignores, unresolved_mark, + context: context.clone(), }; ast.ast.visit_mut_with(&mut mako_require); diff --git a/crates/mako/src/visitors/mako_require.rs b/crates/mako/src/visitors/mako_require.rs index 02da722cf..bcb9bc601 100644 --- a/crates/mako/src/visitors/mako_require.rs +++ b/crates/mako/src/visitors/mako_require.rs @@ -1,15 +1,20 @@ +use std::sync::Arc; + use regex::Regex; use swc_core::common::Mark; use swc_core::ecma::ast::{CallExpr, Callee, Expr, ExprOrSpread, Ident, Lit, Str}; use swc_core::ecma::visit::{VisitMut, VisitMutWith}; use crate::ast::utils::is_ident_undefined; +use crate::compiler::Context; +use crate::config::Platform; const MAKO_REQUIRE: &str = "__mako_require__"; pub struct MakoRequire { pub unresolved_mark: Mark, pub ignores: Vec, + pub context: Arc, } impl MakoRequire { @@ -44,6 +49,9 @@ impl VisitMut for MakoRequire { } fn visit_mut_ident(&mut self, ident: &mut Ident) { + if let Platform::Node = self.context.config.platform { + return; + } self.replace_require(ident); } } @@ -105,6 +113,7 @@ require("foo"); let mut visitor = MakoRequire { ignores, unresolved_mark: ast.unresolved_mark, + context: test_utils.context.clone(), }; ast.ast.visit_mut_with(&mut visitor); }); From 4a4ff7ea04d30be160d76fbbbf3ee5113b03f19f Mon Sep 17 00:00:00 2001 From: xusd320 Date: Mon, 11 Nov 2024 17:56:13 +0800 Subject: [PATCH 3/5] fix: define util require should be __mako_require__ --- crates/mako/src/visitors/optimize_define_utils.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/mako/src/visitors/optimize_define_utils.rs b/crates/mako/src/visitors/optimize_define_utils.rs index e0ac7c83a..9c3a18694 100644 --- a/crates/mako/src/visitors/optimize_define_utils.rs +++ b/crates/mako/src/visitors/optimize_define_utils.rs @@ -39,7 +39,7 @@ impl VisitMut for OptimizeDefineUtils { call_expr.callee = member_expr!( DUMMY_CTXT.apply_mark(self.unresolved_mark), DUMMY_SP, - require.d + __mako_require__.d ) .as_callee(); } else { @@ -64,7 +64,7 @@ impl VisitMut for OptimizeDefineUtils { call_expr.callee = member_expr!( DUMMY_CTXT.apply_mark(self.unresolved_mark), DUMMY_SP, - require.d + __mako_require__.d ) .as_callee(); return; @@ -86,7 +86,7 @@ impl VisitMut for OptimizeDefineUtils { call_expr.callee = member_expr!( DUMMY_CTXT.apply_mark(self.unresolved_mark), DUMMY_SP, - require.e + __mako_require__.e ) .as_callee(); return; From 2a9383ab47ccb2dee9caa2518ebfafdd7158e3bd Mon Sep 17 00:00:00 2001 From: xusd320 Date: Tue, 12 Nov 2024 10:59:50 +0800 Subject: [PATCH 4/5] feat: add expreriental.keep_unresolved_node_require --- crates/mako/src/config/experimental.rs | 2 ++ crates/mako/src/config/mako.config.default.json | 1 + crates/mako/src/visitors/mako_require.rs | 8 +++++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/mako/src/config/experimental.rs b/crates/mako/src/config/experimental.rs index 7b65f40fa..af1a57e06 100644 --- a/crates/mako/src/config/experimental.rs +++ b/crates/mako/src/config/experimental.rs @@ -7,6 +7,8 @@ use crate::create_deserialize_fn; pub struct ExperimentalConfig { pub webpack_syntax_validate: Vec, pub require_context: bool, + // this feature is conflicting with require_context + pub keep_unresolved_node_require: bool, pub magic_comment: bool, #[serde(deserialize_with = "deserialize_detect_loop")] pub detect_circular_dependence: Option, diff --git a/crates/mako/src/config/mako.config.default.json b/crates/mako/src/config/mako.config.default.json index d7c50c566..e9073dc1c 100644 --- a/crates/mako/src/config/mako.config.default.json +++ b/crates/mako/src/config/mako.config.default.json @@ -67,6 +67,7 @@ "experimental": { "webpackSyntaxValidate": [], "requireContext": true, + "keepUnresolvedNodeRequire": false, "magicComment": true, "detectCircularDependence": { "ignores": ["node_modules"], diff --git a/crates/mako/src/visitors/mako_require.rs b/crates/mako/src/visitors/mako_require.rs index bcb9bc601..0953866f5 100644 --- a/crates/mako/src/visitors/mako_require.rs +++ b/crates/mako/src/visitors/mako_require.rs @@ -49,7 +49,13 @@ impl VisitMut for MakoRequire { } fn visit_mut_ident(&mut self, ident: &mut Ident) { - if let Platform::Node = self.context.config.platform { + if self + .context + .config + .experimental + .keep_unresolved_node_require + && let Platform::Node = self.context.config.platform + { return; } self.replace_require(ident); From 262a0eeb6ef4564d1c7ae40980e0c9ad4a781a80 Mon Sep 17 00:00:00 2001 From: xusd320 Date: Wed, 13 Nov 2024 17:33:10 +0800 Subject: [PATCH 5/5] feat: add expreriental.ignore_non_literal_require --- crates/mako/src/config/experimental.rs | 2 +- crates/mako/src/config/mako.config.default.json | 2 +- crates/mako/src/visitors/mako_require.rs | 6 +----- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/crates/mako/src/config/experimental.rs b/crates/mako/src/config/experimental.rs index 59b17b71d..1c86ec1cc 100644 --- a/crates/mako/src/config/experimental.rs +++ b/crates/mako/src/config/experimental.rs @@ -8,7 +8,7 @@ pub struct ExperimentalConfig { pub webpack_syntax_validate: Vec, pub require_context: bool, // this feature is conflicting with require_context - pub keep_unresolved_node_require: bool, + pub ignore_non_literal_require: bool, pub magic_comment: bool, #[serde(deserialize_with = "deserialize_detect_loop")] pub detect_circular_dependence: Option, diff --git a/crates/mako/src/config/mako.config.default.json b/crates/mako/src/config/mako.config.default.json index dcdf3d72c..be971fdbb 100644 --- a/crates/mako/src/config/mako.config.default.json +++ b/crates/mako/src/config/mako.config.default.json @@ -67,7 +67,7 @@ "experimental": { "webpackSyntaxValidate": [], "requireContext": true, - "keepUnresolvedNodeRequire": false, + "ignoreNonLiteralRequire": false, "magicComment": true, "detectCircularDependence": { "ignores": ["node_modules"], diff --git a/crates/mako/src/visitors/mako_require.rs b/crates/mako/src/visitors/mako_require.rs index 0953866f5..4929c9cde 100644 --- a/crates/mako/src/visitors/mako_require.rs +++ b/crates/mako/src/visitors/mako_require.rs @@ -49,11 +49,7 @@ impl VisitMut for MakoRequire { } fn visit_mut_ident(&mut self, ident: &mut Ident) { - if self - .context - .config - .experimental - .keep_unresolved_node_require + if self.context.config.experimental.ignore_non_literal_require && let Platform::Node = self.context.config.platform { return;