Skip to content

baozouai/babel-plugin-enum-to-object

Repository files navigation

babel-plugin-enum-to-object

NPM version NPM downloads

Test codecov

English | 中文

About

A babel Plugin to transform ts enum to object,for better shaking

Options

  interface BabelPluginEnumToObjectOptions {
  /**
   * need reflect ? default true
   * @example
   * enum Status {
   *  PAID
   * }
   * reflect: true
   * =>
   * const Status = {
   * PAID: 0,
   * 0: 'PAID',
   * }
   * reflect: false
   * =>
   * const Status = {
   * PAID: 0,
   * }
   */
  reflect?: boolean
}

eg:

before add plugin:

enum Status {
  PAID,
  UN_PAID
}

// =>

var Status;
(function (Status) {
  Status[Status.PAID = 0] = 'PAID'
  Status[Status.UN_PAID = 1] = 'UN_PAID'
})(Status || (Status = {}))

after add plugin:

enum Status {
  PAID,
  UN_PAID
}

// =>
// default (reflect is true)
const Status = {
  PAID: 0,
  0: 'PAID',
  UN_PAID: 1,
  1: 'UN_PAID'
}
//  set reflect false
const Status = {
  PAID: 0,
  UN_PAID: 1,
}

📦 Install

pnpm add babel-plugin-enum-to-object -D
# or
yarn add babel-plugin-enum-to-object -D
# or
npm i babel-plugin-enum-to-object -D

🔨 Usage

// babel.config.js

module.exports = {

  plugins: [
    // if isProduction add this
    ['enum-to-object', { reflect: true or false }]
  ],
}

📄 License

babel-plugin-enum-to-object is MIT licensed.