# babel-plugin-const-enum

> Transform TypeScript `const` enums

Latest version **1.2.0** (published 2021-11-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install babel-plugin-const-enum
pnpm add babel-plugin-const-enum
yarn add babel-plugin-const-enum
bun add babel-plugin-const-enum
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: no vulnerabilities; high maintenance score.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2021-11-06 |
| First published | 2019-07-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 14.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 51 |
| Author | Kevin Lau |
| Maintainers | dosentmatter |
| Keywords | babel-plugin, typescript, const, enum, terser, uglify, minify, compress |

## Links

- npm: https://www.npmjs.com/package/babel-plugin-const-enum
- Repository: https://github.com/dosentmatter/babel-plugin-const-enum
- npm.io page: https://npm.io/package/babel-plugin-const-enum

## Dependencies (3)

- [@babel/traverse](https://npm.io/package/@babel/traverse.md) ^7.16.0
- [@babel/helper-plugin-utils](https://npm.io/package/@babel/helper-plugin-utils.md) ^7.0.0
- [@babel/plugin-syntax-typescript](https://npm.io/package/@babel/plugin-syntax-typescript.md) ^7.3.3

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.2.0 (latest) — 2021-11-06
- 1.1.0 — 2021-07-03
- 1.0.1 — 2020-04-15
- 1.0.0 — 2020-04-12
- 0.0.5 — 2019-12-09
- 0.0.4 — 2019-11-10
- 0.0.3 — 2019-10-23
- 0.0.2 — 2019-07-08
- 0.0.1 — 2019-07-07
- 0.0.0 — 2019-07-07

## README

# babel-plugin-const-enum &middot; [![npm version](https://img.shields.io/npm/v/babel-plugin-const-enum.svg?style=flat)](https://www.npmjs.com/package/babel-plugin-const-enum) [![npm downloads](https://img.shields.io/npm/dm/babel-plugin-const-enum.svg?style=flat)](https://www.npmjs.com/package/babel-plugin-const-enum)

> Transform TypeScript `const` enums

## Install

Using npm:

```sh
npm install --save-dev babel-plugin-const-enum
```

or using yarn:

```sh
yarn add babel-plugin-const-enum --dev
```

## Usage

You are most likely using
[`@babel/preset-typescript`](https://babeljs.io/docs/en/babel-preset-typescript)
or
[`@babel/plugin-transform-typescript`](https://babeljs.io/docs/en/babel-plugin-transform-typescript)
along with this plugin.

If you are using `@babel/preset-typescript`, then nothing special needs to be
done since
[plugins run before presets](https://babeljs.io/docs/en/plugins/#plugin-ordering).

If you are using `@babel/plugin-transform-typescript`, then make sure that
`babel-plugin-const-enum` comes before
`@babel/plugin-transform-typescript` in the plugin array so that
`babel-plugin-const-enum` [runs first](https://babeljs.io/docs/en/plugins/#plugin-ordering).
This plugin needs to run first to transform the `const enum`s into code that
`@babel/plugin-transform-typescript` allows.

`.babelrc`

```json
{
  "plugins": ["const-enum", "@babel/transform-typescript"]
}
```

### `transform: removeConst` (default)

Removes the `const` keyword to use regular `enum`.
Can be used in a slower dev build to allow `const`, while prod still uses `tsc`.
See [babel#6476](https://github.com/babel/babel/issues/6476).

```ts
// Before:
const enum MyEnum {
  A = 1,
  B = A,
  C,
  D = C,
  E = 1,
  F,
  G = A * E,
  H = A ** B ** C,
  I = A << 20
}

// After:
enum MyEnum {
  A = 1,
  B = A,
  C,
  D = C,
  E = 1,
  F,
  G = A * E,
  H = A ** B ** C,
  I = A << 20
}
```

`.babelrc`
```json
{
  "plugins": [
    "const-enum"
  ]
}
```

Or Explicitly:

`.babelrc`
```json
{
  "plugins": [
    [
      "const-enum",
      {
        "transform": "removeConst"
      }
    ]
  ]
}
```

### `transform: constObject`

Transforms into a `const` object literal.
Can be further compressed using Uglify/Terser to inline `enum` access.
See [babel#8741](https://github.com/babel/babel/issues/8741).

```ts
// Before:
const enum MyEnum {
  A = 1,
  B = A,
  C,
  D = C,
  E = 1,
  F,
  G = A * E,
  H = A ** B ** C,
  I = A << 20
}

// After:
const MyEnum = {
  A: 1,
  B: 1,
  C: 2,
  D: 2,
  E: 1,
  F: 2,
  G: 1,
  H: 1,
  I: 1048576
};
```

`.babelrc`
```json
{
  "plugins": [
    [
      "const-enum",
      {
        "transform": "constObject"
      }
    ]
  ]
}
```

## Troubleshooting

### `SyntaxError`

You may be getting a `SyntaxError` because you are running this plugin on
non-TypeScript source. You might have run into this problem in `react-native`,
see:<br>
[babel-plugin-const-enum#2](https://github.com/dosentmatter/babel-plugin-const-enum/issues/2)<br>
[babel-plugin-const-enum#3](https://github.com/dosentmatter/babel-plugin-const-enum/issues/3)

This seems to be caused by `react-native` transpiling
[`flow`](https://flow.org/) code in `node_modules`.
To fix this issue, please use
[`babel-preset-const-enum`](https://github.com/dosentmatter/babel-preset-const-enum)
to only run `babel-plugin-const-enum` on TypeScript files.
If you wish to fix the issue manually, check out the
[solution in babel-plugin-const-enum#2](https://github.com/dosentmatter/babel-plugin-const-enum/issues/2#issuecomment-542859348).

---
_Source: https://npm.io/package/babel-plugin-const-enum · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
