esbuild icon indicating copy to clipboard operation
esbuild copied to clipboard

Const enum prevents inlining when discriminant is derived from another imported const enum

Open pushkine opened this issue 3 years ago • 1 comments

🕗 Version

latest 0.14.48

💻 Code

// bar.ts
export const enum A {
	a = 1
}

// foo.ts
import { A } from "./bar";

const enum B {
	b = A.a,
}

console.log(B.b);

esbuild foo.ts --bundle --outfile=foo.js

🙁 Actual behavior

"use strict";
(() => {
  // foo.ts
  var B = ((B2) => {
    B2[B2["b"] = 1 /* a */] = "b";
    return B2;
  })(B || {});
  console.log(B.b);
})();

🙂 Expected behavior

"use strict";
(() => {
  // foo.ts
  console.log(1 /* b */);
})();

pushkine avatar Jul 03 '22 08:07 pushkine

This is not a bug, but rather an expected outcome of the way esbuild's enum inlining works. Files in esbuild are compiled in parallel and then one round of inlining is performed during linking. Things that aren't inlined during that step are left un-inlined. The minifier deliberately doesn't attempt to be optimal in all cases like this.

evanw avatar Jul 06 '22 05:07 evanw