Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
a5674d0d
Unverified
Commit
a5674d0d
authored
Aug 11, 2026
by
Sam Poder
Committed by
GitHub
Aug 11, 2026
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[turbopack] Bail CJS tree-shaking on `var x = module.exports = {}` (#97130)
parent
bfc922eb
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
65 additions
and
6 deletions
+65
-6
visitor.rs
...crates/turbopack-ecmascript/src/analyzer/graph/visitor.rs
+26
-6
alias-exports-define.js
...k/cjs-tree-shaking/bailouts/input/alias-exports-define.js
+9
-0
alias-exports-object-chain.js
...tree-shaking/bailouts/input/alias-exports-object-chain.js
+10
-0
alias-exports-object.js
...k/cjs-tree-shaking/bailouts/input/alias-exports-object.js
+8
-0
index.js
...xecution/webpack/cjs-tree-shaking/bailouts/input/index.js
+12
-0
No files found.
turbopack/crates/turbopack-ecmascript/src/analyzer/graph/visitor.rs
View file @
a5674d0d
...
@@ -818,6 +818,21 @@ pub fn as_parent_path_with_in<'a>(
...
@@ -818,6 +818,21 @@ pub fn as_parent_path_with_in<'a>(
path
.into_boxed_slice
()
path
.into_boxed_slice
()
}
}
/// Whether the node at `ast_path` is a whole statement, so its value goes nowhere:
/// true for `module.exports = {};` and `(module.exports = {});`, false for
/// `var x = module.exports = {}` or `f(module.exports = {})`.
fn
is_expression_statement
(
ast_path
:
&
AstNodePath
<
AstParentNodeRef
<
'_
>>
)
->
bool
{
for
node_ref
in
ast_path
.iter
()
.rev
()
{
match
node_ref
{
// The `Expr` wrapper of the node itself, and of a parenthesized one.
AstParentNodeRef
::
Expr
(
..
)
|
AstParentNodeRef
::
ParenExpr
(
_
,
ParenExprField
::
Expr
)
=>
{}
AstParentNodeRef
::
ExprStmt
(
_
,
ExprStmtField
::
Expr
)
=>
return
true
,
_
=>
return
false
,
}
}
false
}
/// Returns the [`MemberExpr`] where the current node is the object:
/// Returns the [`MemberExpr`] where the current node is the object:
/// `<node>.<prop>` or `<node>[<expr>]`.
/// `<node>.<prop>` or `<node>[<expr>]`.
fn
member_access_parent
<
'r
>
(
fn
member_access_parent
<
'r
>
(
...
@@ -1452,6 +1467,11 @@ impl<'a> Analyzer<'a, '_> {
...
@@ -1452,6 +1467,11 @@ impl<'a> Analyzer<'a, '_> {
self
.taint_cjs_exports
();
self
.taint_cjs_exports
();
return
;
return
;
}
}
// Catches `var Self = Object.defineProperty(exports, …)`: the call returns exports.
if
!
is_expression_statement
(
ast_path
)
{
self
.taint_cjs_exports
();
return
;
}
let
dead
=
self
.cjs_exports_object_replaced
()
let
dead
=
self
.cjs_exports_object_replaced
()
&&
!
n
.args
.first
()
.is_some_and
(|
target
|
{
&&
!
n
.args
.first
()
.is_some_and
(|
target
|
{
is_module_exports_chain
(
&
target
.expr
,
self
.eval_context.unresolved_mark
)
is_module_exports_chain
(
&
target
.expr
,
self
.eval_context.unresolved_mark
)
...
@@ -1487,13 +1507,13 @@ impl<'a> Analyzer<'a, '_> {
...
@@ -1487,13 +1507,13 @@ impl<'a> Analyzer<'a, '_> {
else
{
else
{
return
;
return
;
};
};
// Only a statement-position assignment definitely runs.
// Catches `var Self = module.exports = { … }`: the alias reads exports back.
if
matches!
(
if
!
is_expression_statement
(
ast_path
)
{
ast_path
.len
()
.checked_sub
(
2
)
.and_then
(|
i
|
ast_path
.get
(
i
)),
self
.taint_cjs_exports
();
Some
(
AstParentNodeRef
::
ExprStmt
(
_
,
ExprStmtField
::
Expr
))
return
;
)
{
self
.replace_cjs_exports_object
();
}
}
// A statement-position assignment definitely runs.
self
.replace_cjs_exports_object
();
let
mut
names
=
Vec
::
new
();
let
mut
names
=
Vec
::
new
();
for
prop
in
&
obj
.props
{
for
prop
in
&
obj
.props
{
// A spread makes the export set unknowable.
// A spread makes the export set unknowable.
...
...
turbopack/crates/turbopack-tests/tests/execution/webpack/cjs-tree-shaking/bailouts/input/alias-exports-define.js
0 → 100644
View file @
a5674d0d
var
Self
=
Object
.
defineProperty
(
exports
,
'helper'
,
{
value
:
function
()
{
return
'abc'
},
})
exports
.
abc
=
function
()
{
return
Self
.
helper
()
}
turbopack/crates/turbopack-tests/tests/execution/webpack/cjs-tree-shaking/bailouts/input/alias-exports-object-chain.js
0 → 100644
View file @
a5674d0d
var
Self
Self
=
module
.
exports
=
{
helper
:
function
()
{
return
'abc'
},
abc
:
function
()
{
return
Self
.
helper
()
},
}
turbopack/crates/turbopack-tests/tests/execution/webpack/cjs-tree-shaking/bailouts/input/alias-exports-object.js
0 → 100644
View file @
a5674d0d
var
Self
=
(
module
.
exports
=
{
helper
:
function
()
{
return
'abc'
},
abc
:
function
()
{
return
Self
.
helper
()
},
})
turbopack/crates/turbopack-tests/tests/execution/webpack/cjs-tree-shaking/bailouts/input/index.js
View file @
a5674d0d
...
@@ -54,3 +54,15 @@ it('should be able to use AMD to define exports', () => {
...
@@ -54,3 +54,15 @@ it('should be able to use AMD to define exports', () => {
expect
(
require
(
'./using-amd'
).
abc
).
toBe
(
'abc'
)
expect
(
require
(
'./using-amd'
).
abc
).
toBe
(
'abc'
)
expect
(
require
(
'./using-amd'
).
def
).
toBe
(
'def'
)
expect
(
require
(
'./using-amd'
).
def
).
toBe
(
'def'
)
})
})
it
(
'should bailout when a variable aliases a replaced exports object'
,
()
=>
{
expect
(
require
(
'./alias-exports-object'
).
abc
()).
toBe
(
'abc'
)
})
it
(
'should bailout when an assignment chain aliases a replaced exports object'
,
()
=>
{
expect
(
require
(
'./alias-exports-object-chain'
).
abc
()).
toBe
(
'abc'
)
})
it
(
'should bailout when a variable aliases the result of defineProperty'
,
()
=>
{
expect
(
require
(
'./alias-exports-define'
).
abc
()).
toBe
(
'abc'
)
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment