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-enumVerified import paths — ran on the pinned version, not inferred.
Configures Babel to use `babel-plugin-const-enum` with `@babel/preset-typescript`, transforming `const enum`s into constant object literals for better minification.
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.
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)/`).
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.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).
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).