Babel Plugin for TypeScript Const Enums
babel-plugin-const-enum is a Babel plugin designed to transform TypeScript `const enum` declarations. Babel's standard TypeScript presets/plugins (`@babel/preset-typescript` or `@babel/plugin-transform-typescript`) do not inherently handle `const enum`s, which are erased during TypeScript's compilation if not explicitly preserved or transformed. This plugin, currently at version 1.2.0, provides a solution by enabling two transformation strategies: `removeConst` (the default, converting `const enum`s into regular `enum`s) or `constObject` (transforming them into constant object literals). The `constObject` strategy is particularly useful for environments where minifiers like Terser or UglifyJS can then effectively inline these values, leading to smaller bundle sizes. It acts as a critical intermediary step for projects using TypeScript with Babel that rely on `const enum`s, ensuring their correct processing and optimization. The release cadence is driven by community needs and Babel ecosystem changes.
Common errors
-
SyntaxError: Unexpected token, expected "("cause The `babel-plugin-const-enum` is being applied to non-TypeScript files that might contain different syntax, such as Flow annotations.fixEnsure the plugin only runs on TypeScript files. Use `babel-preset-const-enum` or configure your Babel setup to explicitly include/exclude files based on their type (e.g., `test: /\.tsx?$/` for Webpack Babel loader). -
ReferenceError: MyEnum is not defined
cause `const enum` was stripped by Babel without transformation, likely due to incorrect plugin order.fixVerify that `babel-plugin-const-enum` is listed before `@babel/plugin-transform-typescript` in your Babel configuration. If using `@babel/preset-typescript`, ensure `babel-plugin-const-enum` is listed in the `plugins` array (plugins always run before presets).
Warnings
- breaking Incorrect plugin order will cause `const enum`s to not be transformed correctly or result in syntax errors.
- gotcha Running `babel-plugin-const-enum` on non-TypeScript source files (e.g., Flow-typed JavaScript in `node_modules` in React Native projects) can lead to `SyntaxError`s.
- gotcha The default `transform: removeConst` option converts `const enum`s to regular `enum`s, which may not be fully optimized by minifiers. The `transform: constObject` option provides better minification but changes the runtime shape.
Install
-
npm install babel-plugin-const-enum -
yarn add babel-plugin-const-enum -
pnpm add babel-plugin-const-enum
Imports
- const-enum
{ "plugins": ["@babel/plugin-transform-typescript", "const-enum"] }{ "plugins": ["const-enum"] } - const-enum with options
{ "plugins": ["const-enum": {"transform": "constObject"}] }{ "plugins": [["const-enum", {"transform": "constObject"}]] } - babel-plugin-const-enum (require)
import constEnumPlugin from 'babel-plugin-const-enum'
require('babel-plugin-const-enum')
Quickstart
{
"presets": [
[
"@babel/preset-typescript",
{
"is TSX": true, // Example option for preset-typescript
"allExtensions": true
}
]
],
"plugins": [
// Must run BEFORE @babel/preset-typescript if plugin-transform-typescript is implicitly used.
// Or explicitly before @babel/plugin-transform-typescript if listed directly.
["const-enum", {"transform": "constObject"}]
]
}