Skip to content
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

Dead code not eliminated #765

Closed
jaydenseric opened this issue Feb 7, 2021 · 2 comments
Closed

Dead code not eliminated #765

jaydenseric opened this issue Feb 7, 2021 · 2 comments

Comments

@jaydenseric
Copy link

With this code in in.js:

if (0 && false) console.error('abcde');

And this use of the CLI:

esbuild in.js --bundle --minify --outfile=out.js

abcde correctly does not appear in the out.js bundle.

But, unexpectedly, it does with this:

if ((0 + 0) && false) console.error('abcde');

In real world modules, I have code like this:

if (typeof process === 'object' && process.env.NODE_ENV !== 'production') {
  Foo.displayName = 'Foo';
  Foo.propTypes = {
    className: PropTypes.string,
    children: PropTypes.node.isRequired,
  };
}

The idea is, when process.env.NODE_ENV is 'production', the React component displayName and propTypes should be dead-code eliminated from the bundle. This works with webpack but not esbuild.

With this CLI usage:

esbuild in.js --bundle --minify define:process.env.NODE_ENV=\"production\" --outfile=out.js

The process.env.NODE_ENV !== 'production' is swapped with false in the out.js bundle, but the dead code is not eliminated.

@evanw
Copy link
Owner

evanw commented Feb 7, 2021

Thanks for the report. I have a substitution rule for false && anythingfalse but it looks like I also need a rule for noSideEffects && falsefalsy.

@jaydenseric
Copy link
Author

Great, thanks for the speedy response :)

Even if the left side of the if condition has side effects, the if block could be eliminated.

For example this:

if (foo() && false) {
  console.log('abcde');
}

Could become:

foo();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants