Skip to content

Commit

Permalink
feat: keep unresolved nodejs require (#1689)
Browse files Browse the repository at this point in the history
* fix: regexpr for nodejs intenal modules igonre

* feat: not replace unresolved nodejs require to __mako_require__

* fix: define util require should be __mako_require__

* feat: add expreriental.keep_unresolved_node_require

* feat: add expreriental.ignore_non_literal_require
  • Loading branch information
xusd320 authored Nov 13, 2024
1 parent 4d645ad commit 40daa0d
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions crates/mako/src/config/experimental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::create_deserialize_fn;
pub struct ExperimentalConfig {
pub webpack_syntax_validate: Vec<String>,
pub require_context: bool,
// this feature is conflicting with require_context
pub ignore_non_literal_require: bool,
pub magic_comment: bool,
#[serde(deserialize_with = "deserialize_detect_loop")]
pub detect_circular_dependence: Option<DetectCircularDependence>,
Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/config/mako.config.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"experimental": {
"webpackSyntaxValidate": [],
"requireContext": true,
"ignoreNonLiteralRequire": false,
"magicComment": true,
"detectCircularDependence": {
"ignores": ["node_modules"],
Expand Down
2 changes: 1 addition & 1 deletion crates/mako/src/features/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/generate/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,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);

Expand Down
11 changes: 11 additions & 0 deletions crates/mako/src/visitors/mako_require.rs
Original file line number Diff line number Diff line change
@@ -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<Regex>,
pub context: Arc<Context>,
}

impl MakoRequire {
Expand Down Expand Up @@ -44,6 +49,11 @@ impl VisitMut for MakoRequire {
}

fn visit_mut_ident(&mut self, ident: &mut Ident) {
if self.context.config.experimental.ignore_non_literal_require
&& let Platform::Node = self.context.config.platform
{
return;
}
self.replace_require(ident);
}
}
Expand Down Expand Up @@ -105,6 +115,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);
});
Expand Down
6 changes: 3 additions & 3 deletions crates/mako/src/visitors/optimize_define_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 40daa0d

Please sign in to comment.