Skip to content

Commit

Permalink
Fix CJS optimizer (vercel#50376)
Browse files Browse the repository at this point in the history
Avoid the new `VarDecl` being inserted multiple times.
  • Loading branch information
shuding authored and hydRAnger committed Jun 12, 2023
1 parent caa96a2 commit 2d4330c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/next-swc/crates/core/src/cjs_optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,22 @@ impl VisitMut for CjsOptimizer {
definite: false,
};

self.data.extra_stmts.push(Stmt::Decl(Decl::Var(Box::new(
VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Const,
declare: false,
decls: vec![var],
},
))));
if !self.data.extra_stmts.clone().into_iter().any(|s| {
if let Stmt::Decl(Decl::Var(v)) = &s {
v.decls.iter().any(|d| d.name == var.name)
} else {
false
}
}) {
self.data.extra_stmts.push(Stmt::Decl(Decl::Var(
Box::new(VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Const,
declare: false,
decls: vec![var],
}),
)));
}

*e = Expr::Ident(new_id.into());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const server_1 = require('next/server')

server_1.Response()
server_1.Response.rewrite()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const Response = require("next/server/response").Response;
;
Response();
Response.rewrite();

0 comments on commit 2d4330c

Please sign in to comment.