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

ESBuild doesn't support constant folding expressions with enums imported from external files #3425

Closed
esqu1 opened this issue Oct 5, 2023 · 1 comment

Comments

@esqu1
Copy link

esqu1 commented Oct 5, 2023

Repro:

// in test_enum.ts
export enum MyEnum {
  FIRST,
  SECOND,
  THIRD,
}

// in main.ts
import { MyEnum } from "./test_enum";

export function main() {
  if (0 == MyEnum.FIRST) {
    console.log("building the first");
  } else if (0 == MyEnum.SECOND) {
    console.log("building the second");
  } else {
    console.log("building something else");
  }
}

Running the command for bundling:

esbuild main.ts --bundle --outfile=out.js --minify --tree-shaking=true

should result in:

❯ cat out.js
(()=>{function e(){console.log("building the first")}})();

but instead gives

(()=>{function l(){0==0?console.log("building the first"):1==0?console.log("building the second"):console.log("building something else")}})();

which shows that ESBuild is not constant folding the enum in! It doesn't work either if the enum is marked as a const enum. For comparison, this DOES give the expected result if the enum is placed in the same file as where it's being used, main.ts.

Is this an issue where enum resolution comes after parsing/constant folding?

@evanw
Copy link
Owner

evanw commented Oct 6, 2023

Is this an issue where enum resolution comes after parsing/constant folding?

Yes that’s correct, at least for cross-file enum inlining.

@evanw evanw closed this as completed in 9e2f304 May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants