{"id":10538,"library":"babel-plugin-const-enum","title":"Babel Plugin for TypeScript Const Enums","description":"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.","status":"active","version":"1.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/dosentmatter/babel-plugin-const-enum","tags":["javascript","babel-plugin","typescript","const","enum","terser","uglify","minify","compress"],"install":[{"cmd":"npm install babel-plugin-const-enum","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-const-enum","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-const-enum","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Babel plugins require Babel's core runtime.","package":"@babel/core","optional":false}],"imports":[{"note":"Referenced as a string in Babel config. It must be listed before `@babel/plugin-transform-typescript` if using plugins directly.","wrong":"{ \"plugins\": [\"@babel/plugin-transform-typescript\", \"const-enum\"] }","symbol":"const-enum","correct":"{ \"plugins\": [\"const-enum\"] }"},{"note":"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.","wrong":"{ \"plugins\": [\"const-enum\": {\"transform\": \"constObject\"}] }","symbol":"const-enum with options","correct":"{ \"plugins\": [[\"const-enum\", {\"transform\": \"constObject\"}]] }"},{"note":"If configuring Babel dynamically in `babel.config.js`, plugins are typically `require`d, not `import`ed directly as a module for application code.","wrong":"import constEnumPlugin from 'babel-plugin-const-enum'","symbol":"babel-plugin-const-enum (require)","correct":"require('babel-plugin-const-enum')"}],"quickstart":{"code":"{\n  \"presets\": [\n    [\n      \"@babel/preset-typescript\",\n      {\n        \"is  TSX\": true, // Example option for preset-typescript\n        \"allExtensions\": true\n      }\n    ]\n  ],\n  \"plugins\": [\n    // Must run BEFORE @babel/preset-typescript if plugin-transform-typescript is implicitly used.\n    // Or explicitly before @babel/plugin-transform-typescript if listed directly.\n    [\"const-enum\", {\"transform\": \"constObject\"}]\n  ]\n}","lang":"json","description":"Configures Babel to use `babel-plugin-const-enum` with `@babel/preset-typescript`, transforming `const enum`s into constant object literals for better minification."},"warnings":[{"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.","message":"Incorrect plugin order will cause `const enum`s to not be transformed correctly or result in syntax errors.","severity":"breaking","affected_versions":">=1.0.0"},{"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)/`).","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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).","cause":"The `babel-plugin-const-enum` is being applied to non-TypeScript files that might contain different syntax, such as Flow annotations.","error":"SyntaxError: Unexpected token, expected \"(\""},{"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).","cause":"`const enum` was stripped by Babel without transformation, likely due to incorrect plugin order.","error":"ReferenceError: MyEnum is not defined"}],"ecosystem":"npm"}