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 removed. #1317

Closed
underfin opened this issue May 26, 2021 · 3 comments
Closed

Dead code not removed. #1317

underfin opened this issue May 26, 2021 · 3 comments

Comments

@underfin
Copy link

Description

The dead code not removed. I found similar issue #598, but it not same case.

Input

var flag_A = 1 << 0
var flag_B = 1 << 1
var flag_C = 1 << 2

const used = flag_A | flag_B

if(used & flag_A) {
    console.log('flagA')
    a()
}
if(used & flag_B) {
    console.log('flagB')
}
if(used & flag_C) {
    console.log('flagC')
    a()
}

function a () {
 console.log('2322')
}

Output

(() => {
  // input.js
  var flag_A = 1 << 0;
  var flag_B = 1 << 1;
  var flag_C = 1 << 2;
  var used = flag_A | flag_B;
  if (used & flag_A) {
    console.log("flagA");
    a();
  }
  if (used & flag_B) {
    console.log("flagB");
  }
  if (used & flag_C) {
    console.log("flagC");
    a();
  }
  function a() {
    console.log("2322");
  }
})();

Expected Output

This is output emited by terser.

function o(){console.log("2322")}console.log("flagA"),o(),console.log("flagB");
@evanw
Copy link
Owner

evanw commented May 26, 2021

This isn't currently supported. If you want to do constant propagation like this, you can run Terser on the output of esbuild.

@underfin
Copy link
Author

Thanks for your reply, i will use terser before esbuild support it.

@evanw
Copy link
Owner

evanw commented Feb 4, 2022

Closing this issue since your example should work now that #1981 has been fixed, at least as long as you use const instead of var. That code now minifies to this when tree-shaking is active:

console.log("flagA"),l();console.log("flagB");function l(){console.log("2322")}

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

No branches or pull requests

2 participants