Babel Plugin for TypeScript Const Enums

1.2.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

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"}]
  ]
}

view raw JSON →