Skip to content

Commit 48885c7

Browse files
authored
fix: require.resolve() replaced as require() (#12773)
* fix issue 10479 * fix issue 10479 * fix issue 10479 * revert incorrect changes * fix `require.resolve()`` replaced as `require()` * check if `require` is a local variable * fix test * fix test
1 parent d11b7e1 commit 48885c7

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

crates/rspack_plugin_javascript/src/parser_plugin/common_js_imports_parse_plugin.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use rspack_util::SpanExt;
77
use swc_core::{
88
atoms::Atom,
99
common::{Span, Spanned},
10-
ecma::ast::{AssignExpr, CallExpr, Expr, ExprOrSpread, Ident, MemberExpr, NewExpr, UnaryExpr},
10+
ecma::ast::{
11+
AssignExpr, CallExpr, Callee, Expr, ExprOrSpread, Ident, MemberExpr, NewExpr, UnaryExpr,
12+
},
1113
};
1214

1315
use super::JavascriptParserPlugin;
@@ -143,6 +145,26 @@ impl CommonJsImportsParserPlugin {
143145
.unwrap_or_default()
144146
}
145147

148+
fn should_process_resolve(parser: &mut JavascriptParser, call_expr: &CallExpr) -> bool {
149+
let Callee::Expr(expr) = &call_expr.callee else {
150+
return false;
151+
};
152+
153+
let Expr::Member(member_expr) = expr.as_ref() else {
154+
return false;
155+
};
156+
157+
let Expr::Ident(ident) = member_expr.obj.as_ref() else {
158+
return false;
159+
};
160+
161+
if parser.get_variable_info(&ident.sym).is_some() {
162+
return false;
163+
}
164+
165+
true
166+
}
167+
146168
fn process_resolve(&self, parser: &mut JavascriptParser, call_expr: &CallExpr, weak: bool) {
147169
if call_expr.args.len() != 1 {
148170
return;
@@ -458,14 +480,14 @@ impl JavascriptParserPlugin for CommonJsImportsParserPlugin {
458480
)),
459481
expr_name::REQUIRE_RESOLVE => Some(eval::evaluate_to_identifier(
460482
expr_name::REQUIRE_RESOLVE.into(),
461-
expr_name::REQUIRE.into(),
483+
expr_name::REQUIRE_RESOLVE.into(),
462484
Some(true),
463485
start,
464486
end,
465487
)),
466488
expr_name::REQUIRE_RESOLVE_WEAK => Some(eval::evaluate_to_identifier(
467489
expr_name::REQUIRE_RESOLVE_WEAK.into(),
468-
expr_name::REQUIRE.into(),
490+
expr_name::REQUIRE_RESOLVE_WEAK.into(),
469491
Some(true),
470492
start,
471493
end,
@@ -505,13 +527,19 @@ impl JavascriptParserPlugin for CommonJsImportsParserPlugin {
505527
if for_name == expr_name::REQUIRE || for_name == expr_name::MODULE_REQUIRE {
506528
self.require_handler(parser, CallOrNewExpr::Call(call_expr))
507529
} else if for_name == expr_name::REQUIRE_RESOLVE {
508-
if matches!(parser.javascript_options.require_resolve, Some(false)) {
530+
if matches!(parser.javascript_options.require_resolve, Some(false))
531+
|| !Self::should_process_resolve(parser, call_expr)
532+
{
509533
return None;
510534
}
511535

512536
self.process_resolve(parser, call_expr, false);
513537
Some(true)
514538
} else if for_name == expr_name::REQUIRE_RESOLVE_WEAK {
539+
if !Self::should_process_resolve(parser, call_expr) {
540+
return None;
541+
}
542+
515543
self.process_resolve(parser, call_expr, true);
516544
Some(true)
517545
} else {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { require } from './require'
2+
3+
function run() {
4+
return require.resolve('module')
5+
}
6+
7+
it('should not evaluate `require.resolve()` as `require()`', () => {
8+
expect(run.toString()).toContain(`.resolve('module')`)
9+
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { createRequire } from 'module';
2+
3+
export const require = createRequire(import.meta.url);

0 commit comments

Comments
 (0)