Unverified Commit 18ba2085 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix edge case in the CJS optimizer (#50258)

Previously we are ignoring idents in all var declarators in the check,
so it's still possible to fail in some edge cases. CC @kdy1
parent fcfd6306
...@@ -255,9 +255,28 @@ impl Visit for Analyzer<'_> { ...@@ -255,9 +255,28 @@ impl Visit for Analyzer<'_> {
noop_visit_type!(); noop_visit_type!();
fn visit_var_declarator(&mut self, n: &VarDeclarator) { fn visit_var_declarator(&mut self, n: &VarDeclarator) {
self.in_member_or_var = true; let mut safe_to_ignore = false;
n.visit_children_with(self);
self.in_member_or_var = false; // Ignore the require itself (foo = require('foo'))
if let Some(Expr::Call(CallExpr {
callee: Callee::Expr(callee),
..
})) = n.init.as_deref()
{
if let Expr::Ident(ident) = &**callee {
if ident.sym == *"require" {
safe_to_ignore = true;
}
}
}
if safe_to_ignore {
self.in_member_or_var = true;
n.visit_children_with(self);
self.in_member_or_var = false;
} else {
n.visit_children_with(self);
}
} }
fn visit_member_expr(&mut self, e: &MemberExpr) { fn visit_member_expr(&mut self, e: &MemberExpr) {
......
const foo = require('next/server')
const bar = foo
console.log(bar)
const foo = require('next/server');
const bar = foo;
console.log(bar);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment