Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: keep unresolved nodejs require #1689

Merged
merged 6 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 keep_unresolved_node_require: bool,
xusd320 marked this conversation as resolved.
Show resolved Hide resolved
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,
"keepUnresolvedNodeRequire": 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 @@ -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);

Expand Down
15 changes: 15 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,15 @@
}

fn visit_mut_ident(&mut self, ident: &mut Ident) {
if self
.context
.config
.experimental
.keep_unresolved_node_require
&& let Platform::Node = self.context.config.platform

Check warning on line 57 in crates/mako/src/visitors/mako_require.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/visitors/mako_require.rs#L57

Added line #L57 was not covered by tests
{
return;
}
self.replace_require(ident);
}
}
Expand Down Expand Up @@ -105,6 +119,7 @@
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
Loading