babel-plugin-const-enum
Transform TypeScript const
enums
Install
Using npm:
npm install --save-dev babel-plugin-const-enum
or using yarn:
yarn add babel-plugin-const-enum --dev
Usage
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.
const enum MyEnum {
A = 1,
B = A,
C,
D = C,
E = 1,
F,
G = A * E,
H = A ** B ** C,
I = A << 20
}
enum MyEnum {
A = 1,
B = A,
C,
D = C,
E = 1,
F,
G = A * E,
H = A ** B ** C,
I = A << 20
}
.babelrc
{
"plugins": [
"const-enum"
]
}
Or Explicitly:
.babelrc
{
"plugins": [
[
"const-enum",
{
"transform": "removeConst"
}
]
]
}
constObject
Transforms into a const
object literal.
Can be further compressed using Uglify/Terser to inline enum
access.
See babel#8741.
const enum MyEnum {
A = 1,
B = A,
C,
D = C,
E = 1,
F,
G = A * E,
H = A ** B ** C,
I = A << 20
}
const MyEnum = {
A: 1,
B: 1,
C: 2,
D: 2,
E: 1,
F: 2,
G: 1,
H: 1,
I: 1048576
};
.babelrc
{
"plugins": [
[
"const-enum",
{
"transform": "constObject"
}
]
]
}