https://github.com/baozouai/babel-plugin-enum-to-object
transform enum to const object
https://github.com/baozouai/babel-plugin-enum-to-object
babel-plugin enum object optimize treeshaking
Last synced: 9 months ago
JSON representation
transform enum to const object
- Host: GitHub
- URL: https://github.com/baozouai/babel-plugin-enum-to-object
- Owner: baozouai
- License: mit
- Created: 2023-04-17T12:43:42.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-04-22T14:24:51.000Z (almost 3 years ago)
- Last Synced: 2025-04-07T15:54:35.390Z (10 months ago)
- Topics: babel-plugin, enum, object, optimize, treeshaking
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/babel-plugin-enum-to-object
- Size: 39.1 KB
- Stars: 7
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README-zh_CN.md
- License: LICENSE
Awesome Lists containing this project
README
babel-plugin-enum-to-object
[![NPM version][npm-image]][npm-url] ![NPM downloads][download-image]
![Test][test-badge] ![codecov][codecov-badge]
[npm-image]: https://img.shields.io/npm/v/babel-plugin-enum-to-object.svg?style=flat-square
[npm-url]: http://npmjs.org/package/babel-plugin-enum-to-object
[download-image]: https://img.shields.io/npm/dm/babel-plugin-enum-to-object.svg?style=flat-square
[test-badge]: https://github.com/baozouai/babel-plugin-enum-to-object/actions/workflows/ci.yml/badge.svg
[codecov-badge]: https://codecov.io/github/baozouai/babel-plugin-enum-to-object/branch/master/graph/badge.svg
中文 | [英文](./README.md)
## 关于
一个用来将 ts enum 转 js 对象的 babel 插件,使没使用到的 enum 能全部shaking 掉
eg:
没加插件前:
```ts
enum Status {
PAID,
UN_PAID
}
// =>
var Status;
(function (Status) {
Status[Status.PAID = 0] = 'PAID'
Status[Status.UN_PAID = 1] = 'UN_PAID'
})(Status || (Status = {}))
```
加插件后:
```ts
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,
}
```
## 📦 安装
```sh
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
```
## 🔨 使用
```js
// babel.config.js
module.exports = {
plugins: [
// 如果是生产环境,添加这一行
['enum-to-object', { reflect: true or false }]
],
}
```
## 📄 License
babel-plugin-enum-to-object is [MIT licensed](./LICENSE).