rollup-plugin-const-enum
v1.1.4
Published
A simple plugin to inline your const enums in typescript projects.
Readme
rollup-plugin-const-enum
A tiny Rollup plugin that inlines TypeScript const enum members by simple RegExp-based text replacement. (for example Colors.Red -> 0 or Consts.Key -> "val").
This plugin will replace all occurrences of
EnumName.Memberin the source files (be aware of the name collision!), then the import statements will be removed by Rollup's tree-shaking.
More Rollup Plugins you might be interested in:
- rollup-plugin-conditional-compilation: Use directives like
// #if,// #elseto do the conditional compilation like C++. - rollup-plugin-const-enum: inline your
const enum XXX { ... }definitions at compile time. - rollup-plugin-func-macro: replace
__func__by function name of current block, and__file__by file name at compile time.
For more awesome packages, check out my homepage💛
Install
Use pnpm to install as a devDependency:
pnpm add -D rollup-plugin-const-enumQuick usage
Place the plugin in your Rollup config (near the front of the plugin list is recommended):
import { constEnum } from 'rollup-plugin-const-enum';
export default {
// ...
plugins: [
constEnum(), // place it near the front
...
],
};Options
The plugin accepts an optional options object. All options are optional and have sensible defaults.
suffixes: string[]— File suffixes to include when scanning the project for const enums. Default:['.ts', '.tsx', '.mts', '.cts'].files: string[]— Explicit list of file paths (relative toprocess.cwd()) to scan forconst enumdeclarations. When this array is non-empty the plugin will only use these files (each path is resolved withpath.join(process.cwd(), file)) and will ignore recursive directory collection. Default:[](scan the project tree).excludedDirectories: string[]— Directory names (relative to the project root) to exclude from recursive scanning. Each name is resolved againstprocess.cwd(). Default:['.git', 'test', 'tests', 'dist', 'node_modules'].skipDts: boolean— Whentrue, files ending with.d.tsare ignored. Default:true.
Validation: suffixes, files, and excludedDirectories must be arrays of strings. Passing invalid types will throw a TypeError during plugin initialization.
Example
import constEnum from 'rollup-plugin-const-enum';
export default {
plugins: [
constEnum({
suffixes: ['.ts', '.tsx'],
files: ['src/index.ts'],
excludedDirectories: ['.git', 'node_modules'],
skipDts: true,
}),
],
};Important notes
- Scans and collects all const enums and applies them to every included file.
- Cannot scan a specific file and apply it to another specific file.
- This plugin does not use any AST transformer.
- Replacements are based on the textual key
EnumName.Memberusing RegExp. - Only supports simple cases. Ambiguous or complex expressions are not supported.
Advanced Usage
You can access the internal replacement list (for advanced use cases) via the plugin instance:
const plugin = constEnum();
const list = plugin.__kskb_replacement_list; // [ [RegExp, [ [key, value], ... ] ], ... ]License
MIT
