Skip to content

baendlorel/rollup-plugin-const-enum

Repository files navigation

rollup-plugin-const-enum

npm version npm downloads License: MIT Codacy Badge

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.Member in 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:

For more awesome packages, check out my homepage💛

ChangeLog

Install

Use pnpm to install as a devDependency:

pnpm add -D rollup-plugin-const-enum

Quick 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 to process.cwd()) to scan for const enum declarations. When this array is non-empty the plugin will only use these files (each path is resolved with path.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 against process.cwd(). Default: ['.git', 'test', 'tests', 'dist', 'node_modules'].
  • skipDts: boolean — When true, files ending with .d.ts are 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.Member using 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

About

A simple plugin to inline your const enums in typescript projects

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published