中文 | 英文
一个用来将 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 }]
],
}
babel-plugin-enum-to-object is MIT licensed.