English | 中文
A babel Plugin to transform ts enum to object,for better shaking
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,
}
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: [
// if isProduction add this
['enum-to-object', { reflect: true or false }]
],
}
babel-plugin-enum-to-object is MIT licensed.