Visitar URL original
GitHub - Iceman8911/unplugin-inline-enum: Inline enum values to optimize bundle size. · GitHub
Skip to content

About

Inline enum values to optimize bundle size.

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

unplugin-inline-enum

npm version npm downloads jsr Unit Test

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
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:

export enum Direction {
  Up,      // 0
  Down,    // 1
  Left,    // 2
  Right,   // 3
}

Numeric enums with a custom starting value:

export enum StatusCode {
  OK = 200,
  Created,      // 201
  Accepted,     // 202
  NoContent = 204,
  BadRequest = 400,
  NotFound,     // 401
}

String enums:

export enum LogLevel {
  Debug = "DEBUG",
  Info = "INFO",
  Warn = "WARN",
  Error = "ERROR",
}

Mixed (heterogeneous) enums:

export enum Mixed {
  No = 0,
  Yes = "YES",
  Maybe = 1,
}

Computed Members

Members that reference other members of the same enum:

export enum Sizes {
  Base = 16,
  Small = Base / 2,       // 8
  Large = Base * 2,       // 32
  ExtraLarge = Large * 2, // 64
}

Members that reference other enums:

export enum Config {
  MaxItems = 100,
  PageSize = 10,
}

export enum Derived {
  TotalPages = Config.MaxItems / Config.PageSize, // 10
}

Complex nested expressions with parentheses:

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:

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:

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:

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:

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:

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

Numeric Literals

Hexadecimal, octal, and binary literals:

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

Special Values

Floating point numbers:

export enum Constants {
  Pi = 3.14159,
  E = 2.71828,
  Half = 0.5,
}

Infinity (from overflow):

export enum Extremes {
  Huge = 1e309,   // Infinity
  Tiny = -1e309,  // -Infinity
}

String Keys

Enum members with non-identifier keys:

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

Template Literals

Simple template literals (without embedded expressions):

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:

export enum Invalid {
  Value = Math.floor(3.5),  // Error: CallExpression not supported
}

External references:

const EXTERNAL = 100;
export enum Invalid {
  Value = EXTERNAL,  // Error: cannot resolve external reference
}

Runtime expressions:

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

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

# jsr
npx jsr add -D @iceman8911/@unplugin/inline-enum
Vite
// vite.config.ts
import InlineEnum from '@iceman8911/unplugin-inline-enum/vite'

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


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

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


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

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


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

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


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


Options

Refer to docs.

Credits

Thanks to @xiaoxiangmoe and @yangmingshan for their contributions in the PR.

Sponsors

License

MIT License © 2024-PRESENT Kevin Deng

About

Inline enum values to optimize bundle size.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages