Skip to content

Compress: Dead code elimination doesn't work with const value return functions #10448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
schardev opened this issue May 9, 2025 · 2 comments
Assignees
Labels
Milestone

Comments

@schardev
Copy link

schardev commented May 9, 2025

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:

const isDev = () => false;

function Foo() {
    let value: string = 'default';

    // This conditional check will NOT be removed by SWC, even when isDev is set to false.
    if (isDev()) value = "dev value"

    return value;
}

let x = <Foo />

console.log(x)

Output:

"use strict";
const isDev = ()=>!1;
console.log(React.createElement(function() {
    let value = 'default';
    return isDev() && (value = "dev value"), value;
}, null));

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

const isDev = () => false;

function Foo() {
    let value: string = 'default';
    if (isDev()) value = "dev value"
    return value;
}

let x = <Foo />

console.log(x)

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

@schardev schardev added the C-bug label May 9, 2025
@kdy1 kdy1 added this to the Planned milestone May 12, 2025
@CPunisher CPunisher self-assigned this May 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

5 participants
@CPunisher @kdy1 @schardev and others