npm package: @iceman8911/unplugin-inline-enum
Description: Inline enum values to optimize bundle size.
Author: Kevin Deng
Latest version: 0.6.7

# 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.

## Features

- 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')
```

## Supported Enum Patterns

This plugin handles virtually all TypeScript enum patterns that can be statically analyzed at build time.

### Basic Enums

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,
}
```

### Computed Members

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
}
```

### Bitwise Operations

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
}
```

### Arithmetic Operations

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
}
```

### Unary Operations

Negative values and other unary operators:

```ts
export enum Values {
  Positive = 42,
  Negative = -42,
  BitwiseNot = ~0,  // -1
}
```

### Numeric Literals

Hexadecimal, octal, and binary literals:

```ts
export enum Literals {
  Hex = 0xff,           // 255
  Octal = 0o77,         // 63
  Binary = 0b11111111,  // 255
  Scientific = 1e6,     // 1000000
}
```

### Special Values

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
}
```

### String Keys

Enum members with non-identifier keys:

```ts
export enum SpecialKeys {
  "kebab-case" = 1,
  "with spaces" = 2,
  "123numeric" = 3,
}
```

### Template Literals

Simple template literals (without embedded expressions):

```ts
export enum Templates {
  Message = `Hello, World!`,
  Quoted = `String with "quotes"`,
}
```

## Unsupported Patterns

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.

## Installation

```bash
# npm
npm i -D @iceman8911/unplugin-inline-enum

# jsr
npx jsr add -D @iceman8911/@unplugin/inline-enum
```

<details>
<summary>Vite</summary><br>

```ts
// vite.config.ts
import InlineEnum from '@iceman8911/unplugin-inline-enum/vite'

export default defineConfig({
  plugins: [InlineEnum()],
})
```

<br></details>

<details>
<summary>Rollup</summary><br>

```ts
// rollup.config.js
import InlineEnum from '@iceman8911/unplugin-inline-enum/rollup'

export default {
  plugins: [InlineEnum()],
}
```

<br></details>

<details>
<summary>Rolldown</summary><br>

```ts
// rolldown.config.js
import InlineEnum from '@iceman8911/unplugin-inline-enum/rolldown'

export default {
  plugins: [InlineEnum()],
}
```

<br></details>

<details>
<summary>esbuild</summary><br>

```ts
// esbuild.config.js
import { build } from 'esbuild'

build({
  plugins: [require('@iceman8911/unplugin-inline-enum/esbuild')()],
})
```

<br></details>

<details>
<summary>Webpack</summary><br>

```ts
// webpack.config.js
module.exports = {
  /* ... */
  plugins: [require('@iceman8911/unplugin-inline-enum/webpack')()],
}
```

<br></details>

## Options

Refer to [docs](https://jsr.io/@unplugin/inline-enum/doc/api/~/Options).

## Credits

Thanks to [@xiaoxiangmoe](https://github.com/xiaoxiangmoe) and
[@yangmingshan](https://github.com/yangmingshan) for their contributions in the
[PR](https://github.com/vuejs/core/pull/9261).

## Sponsors

<p align="center">
  <a href="https://cdn.jsdelivr.net/gh/sxzz/sponsors/sponsors.svg">
    <img src='https://cdn.jsdelivr.net/gh/sxzz/sponsors/sponsors.svg'/>
  </a>
</p>

## License

[MIT](./LICENSE) License © 2024-PRESENT [Kevin Deng](https://github.com/sxzz)

<!-- Badges -->

[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