Registry / serialization / babel-preset-const-enum

babel-preset-const-enum

JSON →
library1.0.0jsnpmunverified

Babel preset for transforming TypeScript const enums. Version 1.0.0. It runs babel-plugin-const-enum only on .ts/.tsx files to prevent syntax errors on non-TypeScript files. Provides two transform options: removeConst (default) reverts const enum to regular enum, while constObject inlines values into a const object literal for minification. Must be placed after @babel/preset-typescript in the preset order to work correctly. Lightweight, focused solution for using const enums with Babel.

npm install babel-preset-const-enum
INSTALL
IMPORT
SIG · BABEL-PRESET-CONST
B
babel-preset-const-enum
serializationjavascriptv1.0.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.

default export (preset)
module.exports = require('babel-preset-const-enum')
import preset from 'babel-preset-const-enum'
Preset is CommonJS; use require in .babelrc or Babel config file.
Preset options
presets: [['const-enum', { transform: 'constObject' }]]
presets: [['babel-preset-const-enum', { transform: 'constObject' }]]
Short name 'const-enum' is recommended in preset array, but full name also works.
Plugin (babel-plugin-const-enum) - not typically imported directly
not applicable
import babelPluginConstEnum from 'babel-plugin-const-enum'
Use the preset instead; plugin may cause errors on non-TS files.

Shows how to configure babel-preset-const-enum with @babel/preset-typescript and constObject transform, then run Babel on a .ts file.

// .babelrc { "presets": [ "@babel/preset-typescript", ["const-enum", { "transform": "constObject" }] ] } // example.ts const enum Color { Red = 1, Green = 2, Blue = 3 } console.log(Color.Red); // Run: npx babel example.ts --out-file example.js // Output: // const Color = { // Red: 1, // Green: 2, // Blue: 3 // }; // console.log(Color.Red);
Debug
Known issues
gotchaPreset ordering: babel-preset-const-enum must come AFTER @babel/preset-typescript in the presets array.
fix
Put 'const-enum' after '@babel/preset-typescript' in the preset order.
affects: all
gotchaTransform 'removeConst' only removes const keyword, does not inline values. May not achieve full minification.
fix
Use transform: 'constObject' if you want inlined values for minification.
affects: all
deprecatedThe default transform 'removeConst' may not be optimal for production; consider using 'constObject' to fully leverage Uglify/Terser.
fix
Set transform: 'constObject' explicitly for production builds.
affects: <=1.0.0
Errors
Common errors & fixes
SyntaxError: Unexpected token, expected ";" (1:14) ... while parsing unexpected token
babel-preset-const-enum ran before TypeScript preset, causing parsing failure on const enum syntax.
fix
Reorder presets: put '@babel/preset-typescript' first, then 'const-enum'.
Error: Cannot find module 'babel-preset-const-enum'
Package not installed or not in node_modules.
fix
Install: npm install --save-dev babel-preset-const-enum
ReferenceError: MyEnum is not defined
Using transform: 'removeConst' and the enum is used before declaration (hoisting issue).
fix
Use transform: 'constObject' to create a const object that avoids hoisting issues.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
@babel/corerequiredpeer dependency required for Babel plugin functionality
Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
1
Resources
babel-preset-const-enum — npm install babel-preset-const-enum · libregistry