A universal plugin for inlining TypeScript const enums at build time. Powered by unplugin, it works seamlessly with Vite, Rollup, Rolldown, esbuild, and Webpack.
- ✨ Universal Support: Works with Vite, Rollup, Rolldown, esbuild, and Webpack
- 🔍 Smart Resolution: Automatically resolves const enum values across modules
- 📦 Zero Runtime Overhead: Replaces const enum references with literal values at build time
- 🌐 Import/Export Handling: Supports complex import/export scenarios including aliases
- 🔗 Dependency Tracking: Handles const enums that depend on other const enums
- 🎯 TypeScript Path Mapping: Respects
tsconfig.jsonpath aliases - 💪 Type-safe: Written in TypeScript with full type definitions
npm install -D unplugin-inline-const-enum
# or
yarn add -D unplugin-inline-const-enum
# or
pnpm add -D unplugin-inline-const-enum// vite.config.ts
import InlineConstEnumPlugin from "unplugin-inline-const-enum/vite";
export default {
plugins: [
InlineConstEnumPlugin({
sourceDir: "./src",
tsConfig: "./tsconfig.json",
}),
],
};// rollup.config.js
import InlineConstEnumPlugin from "unplugin-inline-const-enum/rollup";
export default {
plugins: [
InlineConstEnumPlugin({
sourceDir: "./src",
tsConfig: "./tsconfig.json",
}),
],
};// rolldown.config.js
import InlineConstEnumPlugin from "unplugin-inline-const-enum/rolldown";
export default {
plugins: [
InlineConstEnumPlugin({
sourceDir: "./src",
tsConfig: "./tsconfig.json",
}),
],
};// esbuild.config.js
import InlineConstEnumPlugin from "unplugin-inline-const-enum/esbuild";
require("esbuild").build({
plugins: [
InlineConstEnumPlugin({
sourceDir: "./src",
tsConfig: "./tsconfig.json",
}),
],
});// webpack.config.js
module.exports = {
plugins: [
require("unplugin-inline-const-enum/webpack")({
sourceDir: "./src",
tsConfig: "./tsconfig.json",
}),
],
};interface IInlineConstEnumOptions {
/**
* A glob or array of globs to include.
* @default /\.[cm]?[jt]sx?$/
*/
include?: FilterPattern
/**
* A glob or array of globs to exclude.
* @default [/node_modules/, /\.d\.[cm]?ts$/]
*/
exclude?: FilterPattern
/**
* The source directory for resolving modules.
* @default process.cwd()
*/
sourceDir?: string
/**
* A glob pattern to locate source files to scan for const enums.
* @default '**/*.{ts,cts,mts,tsx}'
*/
sourcePattern?: string
/**
* The path to the tsconfig.json file.
* @default 'tsconfig.json'
*/
tsConfig?: string
/**
* Whether to enable debug logging.
* @default false
*/
debug?: boolean
}// enums.ts
export const enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT",
}
export const enum Status {
Pending,
Success = 200,
Error = 500,
}
// app.ts
import { Direction, Status } from "./enums";
console.log(Direction.Up);
console.log(Status.Success);// app.ts (transformed)
console.log("UP");
console.log(200);The plugin handles const enums imported from other modules:
// module-a.ts
export const enum Colors {
Red,
Green = 5,
Blue,
}
// module-b.ts
import { Colors } from "./module-a";
export const color = Colors.Green; // → 5Handles const enums that reference other const enums:
export const enum Base {
A = 10,
B = 20,
}
export const enum Derived {
X = Base.A, // → 10
Y = Base.B, // → 20
}Works with re-exports and aliases:
// enums.ts
export { Status as HttpStatus } from "./http-status";
// app.ts
import { HttpStatus } from "./enums";
console.log(HttpStatus.OK); // → 200- Scan Phase: Parses all TypeScript files to collect const enum definitions
- Analysis Phase: Builds a dependency graph of const enums
- Transform Phase: Replaces const enum references with their literal values during the build
-
Const Variable References Not Supported: The plugin does not support const enum members whose values are expressions containing non-const-enum variables. For example:
const varX = 1; const enum MyEnum { X = varX, // ❌ Not supported }
Only literal values and references to other const enum members are supported:
const enum MyEnum { X = 1, // ✅ Supported Y = MyEnum.X + 2, // ✅ Supported }
-
Const Enum Definitions Not Removed: The plugin only inlines const enum values but does not remove the const enum definitions from the output. To eliminate unused const enum declarations, you should rely on tree-shaking tools (like Rollup or Webpack with
mode: 'production') to remove the dead code. -
Namespace Import/Export Not Supported: The plugin cannot inline const enums when they are accessed through namespace imports or re-exported through namespace exports.
Not supported - Namespace import:
// a.ts export const enum CE_Test { X = 1, Y = 2, } // b.ts import * as A from "./a"; const X = A.CE_Test.X; // ❌ Won't be inlined
Not supported - Namespace export/import:
// a.ts export const enum CE_Test { X = 1, Y = 2, } // b.ts export * from "./a"; // c.ts import { CE_Test } from "./b"; const X = CE_Test.X; // ❌ Won't be inlined, CE_Test is exported by namespace
Workaround: Use named imports instead:
// b.ts import { CE_Test } from "./a"; const X = CE_Test.X; // ✅ Will be inlined
# Run tests
npm test
# Run tests in watch mode
npm run test:watch
# Run linting
npm run lintMIT
Contributions are welcome! Please feel free to submit a Pull Request.
If you find a bug or have a feature request, please open an issue.