evanw / evanw/esbuild

Replicate Babel's `optimizeConstEnums` transformation

Open
#3,697 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
40.1k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

Unlike regular enums, const-based enums do not have reverse mapping, and can't be referenced and indexed the same way.

Given that we're already ignoring `isolatedModules` behavior of not inlining const-based enums at all even within the same file, why not go a step further and transform `const enum` into a plain object literal? [Babel's `optimizeConstEnums`](https://babeljs.io/docs/babel-preset-typescript#optimizeconstenums) does the following:

```ts
export const enum LabelPreference {
Ignore = 1,
Warn = 2,
Hide = 3,
};

// transformed to...

export var LabelPreference = {
Ignore: 1,
Warn: 2,
Hide: 3,
};

// duplicate enum declarations gets transformed to:
Object.assign(LabelPreference, {
Blur: 3,
});
```

The size reduction from this transformation seems worthwhile for libraries trying to bundle with esbuild.

Contributor guide

No contributing guide indexed for this repository

Research direction

The issue names no source files or tests. Start by locating esbuild's TypeScript const-enum lowering and compare its output with Babel's optimizeConstEnums examples. Done means const enums produce plain objects, including the demonstrated handling of duplicate declarations.

Written by the indexing model from the issue text.

Assessment

Tech stack
babel, go, typescript
Domain
build-system, compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.