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

Replicate Babel's optimizeConstEnums transformation #3697

Open
mary-ext opened this issue Mar 14, 2024 · 0 comments
Open

Replicate Babel's optimizeConstEnums transformation #3697

mary-ext opened this issue Mar 14, 2024 · 0 comments

Comments

@mary-ext
Copy link

mary-ext commented Mar 14, 2024

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 does the following:

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.

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

1 participant