Inline enum values to optimize bundle size.
npm install @iceman8911/unplugin-inline-enum[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![jsr][jsr-src]][jsr-href]
[![Unit Test][unit-test-src]][unit-test-href]
Inline enum values to optimize bundle size.
This fork adds support for computed enum members that reference other members of the same enum or other enums.
- Inline enum values to reduce bundle size
- Simplify generated enums in JavaScript
- Support for computed enum members with complex expressions
``ts
export enum TestEnum {
a = 1,
b = 'foo',
}
console.log(TestEnum.a, TestEnum.b)
// before
export let TestEnum
;(function (TestEnum) {
TestEnum[(TestEnum.a = 1)] = 'a'
TestEnum.b = 'foo'
})(TestEnum || (TestEnum = {}))
console.log(TestEnum.a, TestEnum.b)
// after
const TestEnum = {
a: 1,
'1': 'a',
b: 'foo',
}
console.log(1, 'foo')
`
This plugin handles virtually all TypeScript enum patterns that can be statically analyzed at build time.
Standard numeric enums with auto-incrementing values:
`ts`
export enum Direction {
Up, // 0
Down, // 1
Left, // 2
Right, // 3
}
Numeric enums with a custom starting value:
`ts`
export enum StatusCode {
OK = 200,
Created, // 201
Accepted, // 202
NoContent = 204,
BadRequest = 400,
NotFound, // 401
}
String enums:
`ts`
export enum LogLevel {
Debug = "DEBUG",
Info = "INFO",
Warn = "WARN",
Error = "ERROR",
}
Mixed (heterogeneous) enums:
`ts`
export enum Mixed {
No = 0,
Yes = "YES",
Maybe = 1,
}
Members that reference other members of the same enum:
`ts`
export enum Sizes {
Base = 16,
Small = Base / 2, // 8
Large = Base * 2, // 32
ExtraLarge = Large * 2, // 64
}
Members that reference other enums:
`ts
export enum Config {
MaxItems = 100,
PageSize = 10,
}
export enum Derived {
TotalPages = Config.MaxItems / Config.PageSize, // 10
}
`
Complex nested expressions with parentheses:
`ts`
export enum RuleIds {
MaxRules = 1000,
RuleRange = MaxRules / 8, // 125
FirstGroup = 1,
SecondGroup = FirstGroup + RuleRange, // 126
ThirdGroup = SecondGroup + RuleRange, // 251
}
Bit flags using shift operators:
`ts`
export enum Permissions {
None = 0,
Read = 1 << 0, // 1
Write = 1 << 1, // 2
Execute = 1 << 2, // 4
Delete = 1 << 3, // 8
}
Combining flags with bitwise OR:
`ts`
export enum Permissions {
None = 0,
Read = 1,
Write = 2,
Execute = 4,
ReadWrite = Read | Write, // 3
ReadExecute = Read | Execute, // 5
All = Read | Write | Execute, // 7
}
Other bitwise operations:
`ts`
export enum BitwiseOps {
A = 0b1111,
B = 0b1010,
And = A & B, // 10
Or = A | B, // 15
Xor = A ^ B, // 5
NotA = ~A, // -16
}
All standard arithmetic operators are supported:
`ts`
export enum Math {
A = 100,
B = 7,
Sum = A + B, // 107
Difference = A - B, // 93
Product = A * B, // 700
Quotient = A / B, // 14.285714...
Remainder = A % B, // 2
}
Negative values and other unary operators:
`ts`
export enum Values {
Positive = 42,
Negative = -42,
BitwiseNot = ~0, // -1
}
Hexadecimal, octal, and binary literals:
`ts`
export enum Literals {
Hex = 0xff, // 255
Octal = 0o77, // 63
Binary = 0b11111111, // 255
Scientific = 1e6, // 1000000
}
Floating point numbers:
`ts`
export enum Constants {
Pi = 3.14159,
E = 2.71828,
Half = 0.5,
}
Infinity (from overflow):
`ts`
export enum Extremes {
Huge = 1e309, // Infinity
Tiny = -1e309, // -Infinity
}
Enum members with non-identifier keys:
`ts`
export enum SpecialKeys {
"kebab-case" = 1,
"with spaces" = 2,
"123numeric" = 3,
}
Simple template literals (without embedded expressions):
`tsHello, World!
export enum Templates {
Message = ,String with "quotes"
Quoted = ,`
}
The following patterns cannot be statically analyzed and will cause build errors:
Function calls:
`ts`
export enum Invalid {
Value = Math.floor(3.5), // Error: CallExpression not supported
}
External references:
`ts`
const EXTERNAL = 100;
export enum Invalid {
Value = EXTERNAL, // Error: cannot resolve external reference
}
Runtime expressions:
`ts`
export enum Invalid {
Value = Date.now(), // Error: cannot evaluate at build time
}
If you need these patterns, keep the enum as-is and the plugin will skip it.
`bashnpm
npm i -D @iceman8911/unplugin-inline-enum
Vite
`ts
// vite.config.ts
import InlineEnum from '@iceman8911/unplugin-inline-enum/vite'export default defineConfig({
plugins: [InlineEnum()],
})
`
Rollup
`ts
// rollup.config.js
import InlineEnum from '@iceman8911/unplugin-inline-enum/rollup'export default {
plugins: [InlineEnum()],
}
`
Rolldown
`ts
// rolldown.config.js
import InlineEnum from '@iceman8911/unplugin-inline-enum/rolldown'export default {
plugins: [InlineEnum()],
}
`
esbuild
`ts
// esbuild.config.js
import { build } from 'esbuild'build({
plugins: [require('@iceman8911/unplugin-inline-enum/esbuild')()],
})
`
Webpack
`ts
// webpack.config.js
module.exports = {
/ ... /
plugins: [require('@iceman8911/unplugin-inline-enum/webpack')()],
}
``Refer to docs.
Thanks to @xiaoxiangmoe and
@yangmingshan for their contributions in the
PR.
MIT License © 2024-PRESENT Kevin Deng
[npm-version-src]: https://img.shields.io/npm/v/@iceman8911/unplugin-inline-enum.svg
[npm-version-href]: https://npmjs.com/package/@iceman8911/unplugin-inline-enum
[npm-downloads-src]: https://img.shields.io/npm/dm/@iceman8911/unplugin-inline-enum
[npm-downloads-href]: https://www.npmcharts.com/compare/@iceman8911/unplugin-inline-enum?interval=30
[jsr-src]: https://jsr.io/badges/@unplugin/inline-enum
[jsr-href]: https://jsr.io/@unplugin/inline-enum
[unit-test-src]: https://github.com/unplugin/unplugin-inline-enum/actions/workflows/unit-test.yml/badge.svg
[unit-test-href]: https://github.com/unplugin/unplugin-inline-enum/actions/workflows/unit-test.yml