rollup-plugin-const-enum
v2.0.0
Published
A simple plugin to inline your const enums in typescript projects.
Readme
rollup-plugin-const-enum
A Rollup plugin that uses the TypeScript compiler API to inline enum member accesses such as Colors.Red -> 0 or Status.Active -> "active".
2.0.0 is a major refactor that replaces the original regex-based scanning approach with a more robust TypeScript compiler API-based implementation. This allows for more accurate and reliable enum member inlining, especially in complex codebases with various import patterns and TypeScript features.
More Rollup Plugins you might be interested in:
For more awesome packages, check out my homepage💛
Install
Use pnpm to install as a devDependency:
pnpm add -D rollup-plugin-const-enum rollup typescriptQuick usage
Place the plugin before your TypeScript transpilation step so it can still see the original enum syntax:
import { constEnum } from 'rollup-plugin-const-enum';
export default {
// ...
plugins: [
constEnum(), // place it near the front
...
],
};Behavior
- Only enum member accesses that are available in the current file are considered.
- Cross-file enums must be explicitly imported in the current module before they can be inlined.
- Same-file enum declarations can always be inlined.
- The transform returns a sourcemap.
Options
The plugin accepts an optional options object. All options are optional and have sensible defaults.
inlineNonConstEnums: boolean— Inline regularenummembers in addition toconst enum. Default:false.inlineNames?: Array<string | RegExp>— Restrict inlining to enums whose declaration names match one of these rules. Default:undefined.
Validation:
inlineNonConstEnumsmust be a boolean when provided.inlineNamesmust be an array ofstring | RegExpwhen provided.
Example
import constEnum from 'rollup-plugin-const-enum';
export default {
plugins: [
constEnum({
inlineNonConstEnums: true,
inlineNames: ['Color', /^Status$/],
}),
],
};Important notes
- It relies on TypeScript's symbol resolution, so project
tsconfig.jsonsettings can affect module resolution. - If TypeScript cannot evaluate an enum member access as a constant, the original code is kept unchanged.
- The plugin only transforms TypeScript source files.
License
MIT
