You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed that dead code elimination doesn’t behave as expected when using functions that return constant values, for example, when checking process.env.NODE_ENV.
Here’s a simple example:
constisDev=()=>false;functionFoo(){letvalue: string='default';// This conditional check will NOT be removed by SWC, even when isDev is set to false.if(isDev())value="dev value"returnvalue;}letx=<Foo/>console.log(x)
Uh oh!
There was an error while loading. Please reload this page.
Describe the bug
Ref: vercel/next.js#78955
I noticed that dead code elimination doesn’t behave as expected when using functions that return constant values, for example, when checking
process.env.NODE_ENV
.Here’s a simple example:
Output:
The above might work if you move the checks outside of the component (e.g., at module scope), the DCE kicks in properly.
Input code
Config
Link to the code that reproduces this issue
https://play.swc.rs/?version=1.11.25-nightly-20250505.1&code=H4sIAAAAAAAAAzWPMW7DMAxFd53iI0tsoLD3pMlUdEyXXMCW6FitIhai5LgIevfSNsqBAMnPz0fLUTK8vNGEE6oapzOGLggdjRlKtNlzxDuzTp4GGoEypi4UOkBy8vGma3tHQ1dC3uvSomlbXEcvsBydXxy6ADuS%2FcLDh4DLxxU9IdGdJ3Lof3ChOTefAldWxzwS%2BuKDw3diSyIv%2F7Y0UcRj1LQh6xFRoMwbdLPq%2FIBqHVd1vbEq486pfC12G2SiXFLcWkfza8zy2azKV30X7dkYxRcO1AS%2BVXP9B9j6spIqAQAA&config=H4sIAAAAAAAAA32UzW7bMAzH732KwOcdimDoYQ%2Bw255BUCzKUSeLhkilMYq8%2B2hZSbOGzs3mjx8SSf0%2FX3a77p367tfuUz7lZ7KZIN%2F%2BxUJzYnsWS8fzBNTnMHH340qZFsS5QLVcVtCxzQPwEgS0f93%2FbAFdRCQQs7eRoNnGkIKf70v2OE4ZiO5sYpWUZYTE9H98Yxk%2FqJ3kzn5AjGDTE2IsmZAYBsha4h5jtBOBOdmsZFlOanMg1EossDA4M2WcVJ5c4IBJaj5SB9aZHh0oKGToOZxAC5NaEpZIrqfcp2IHhzIMdczfouFkY7Gs1IRzHYmcVsl6xEBsfElaC1e40YMVtuZ%2BjwzeZOCS02PcO4a0MZO%2FANKBaImSHUHLWz287NNWtH8aGZKXleVZ4bLf2i0TDNJUE4JXOrt0BjIHbZoZXOlh6WyvHafhjfZRcGDAe9kVJTV9BO6PWtHlnaNXgMzXem2rVmBur3CDLw%2FiCf4tt2R9wZrHaPm4TWkeDxifFBiBj%2BieOMgoGLdxFpU4T9u8JAeyGuBUl0IVPIqAPABGE6tePuyGPA%2FJaIaIB1UmJtnzOsT9azNebuI82jR8qcAq0C%2FNoRvRlQqb8i9TXwX7rftyumrz7TpdoD%2FXyCr7l3%2FcAhU5RQYAAA%3D%3D
SWC Info output
No response
Expected behavior
SWC should be able to remove code blocks/function with constant conditions/return values.
Actual behavior
No response
Version
1.11.25-nightly-20250505.1
Additional context
No response
The text was updated successfully, but these errors were encountered: