Skip to content

Latest commit

 

History

History
104 lines (69 loc) · 1.68 KB

README-zh_CN.md

File metadata and controls

104 lines (69 loc) · 1.68 KB

babel-plugin-enum-to-object

NPM version NPM downloads

Test codecov

中文 | 英文

关于

一个用来将 ts enum 转 js 对象的 babel 插件,使没使用到的 enum 能全部shaking 掉

eg:

没加插件前:

enum Status {
  PAID,
  UN_PAID
}

// =>

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

加插件后:

enum Status {
  PAID,
  UN_PAID
}

// =>

// 默认 reflect 是 true
const Status = {
  PAID: 0,
  0: 'PAID',
  UN_PAID: 1,
  1: 'UN_PAID'
}
//  设置 reflect 为false
const Status = {
  PAID: 0,
  UN_PAID: 1,
}

📦 安装

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

🔨 使用

// babel.config.js

module.exports = {

  plugins: [
    // 如果是生产环境,添加这一行
    ['enum-to-object', { reflect: true or false }]
  ],
}

📄 License

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