Registry / devops / babel-plugin-const-enum

babel-plugin-const-enum

JSON →
library1.2.0jsnpmunverified

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.

npm install babel-plugin-const-enum
INSTALL
IMPORT
SIG · BABEL-PLUGIN-CONST
B
babel-plugin-const-enum
devopsjavascriptv1.2.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

const-enum
{ "plugins": ["const-enum"] }
{ "plugins": ["@babel/plugin-transform-typescript", "const-enum"] }
Referenced as a string in Babel config. It must be listed before `@babel/plugin-transform-typescript` if using plugins directly.
const-enum with options
{ "plugins": [["const-enum", {"transform": "constObject"}]] }
{ "plugins": ["const-enum": {"transform": "constObject"}] }
When passing options, the plugin entry must be an array where the first element is the plugin name and the second is an object of options.
babel-plugin-const-enum (require)
require('babel-plugin-const-enum')
import constEnumPlugin from 'babel-plugin-const-enum'
If configuring Babel dynamically in `babel.config.js`, plugins are typically `require`d, not `import`ed directly as a module for application code.

Configures Babel to use `babel-plugin-const-enum` with `@babel/preset-typescript`, transforming `const enum`s into constant object literals for better minification.

{ "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"}] ] }
Debug
Known issues
breakingIncorrect plugin order will cause `const enum`s to not be transformed correctly or result in syntax errors.
fix
Ensure `babel-plugin-const-enum` is listed BEFORE `@babel/plugin-transform-typescript` in your Babel configuration's plugin array. If using `@babel/preset-typescript`, no explicit ordering is usually needed as plugins run before presets.
affects: >=1.0.0
gotchaRunning `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.
fix
Use `babel-preset-const-enum` to apply the plugin only to TypeScript files, or manually configure your Babel loader/plugin to exclude non-TypeScript files (e.g., `exclude: /node_modules/(?!my-typescript-lib)/`).
affects: >=1.0.0
gotchaThe 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.
fix
If minification and bundle size are critical, configure the plugin with `["const-enum", {"transform": "constObject"}]` to convert enums into constant object literals that optimizers can inline.
affects: >=1.0.0
Errors
Common errors & fixes
SyntaxError: Unexpected token, expected "("
The `babel-plugin-const-enum` is being applied to non-TypeScript files that might contain different syntax, such as Flow annotations.
fix
Ensure 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
`const enum` was stripped by Babel without transformation, likely due to incorrect plugin order.
fix
Verify 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).
Upgrade
Version history
1.2.0latest on npm
Audit
Dependencies
@babel/corerequiredBabel plugins require Babel's core runtime.
Agent activity
4 hits · last 30 days
node
4
Resources
babel-plugin-const-enum — npm install babel-plugin-const-enum · libregistry