npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-typescript-hardcoded-enum

v0.0.2

Published

A babel plugin can resolve typescript enum to hard code

Downloads

6

Readme

babel-plugin-typescript-hardcoded-enum

中文 | English

Description

让你在 babel 编译的 typescript 项目中,轻松使用 enum

ts-loader 和 @babel/preset-typescript

当你像下面这样组织项目的 typings 并使用 enum 时,在 ts-loader 编译的项目中一切都是正常且可行的。但是在 @babel/preset-typescript 编译的项目,这会导致一个运行时的错误, ecom is undefined

并且 babeltypescript 的枚举编译为对象,这将会增加项目的体积,具体点击这里

babel-plugin-typescript-hardcoded-enum 就是为了解决这个问题,让你在 babel 编译的项目中轻松使用 enum。提取枚举值进行硬编码

// xx.ts
import openmind = ecom.governance.openmind;
const Test1: ecom.governance.openmind.EventTypeRequest = {
  id: "111",
  eventType: ecom.governance.openmind.EventType.MQ,
};
const EventTypeMap: Record<ecom.governance.openmind.EventType, string> = {
  [ecom.governance.openmind.EventType.Online]: "在线",
  [ecom.governance.openmind.EventType.Offline]: "离线",
  [openmind.EventType.RPC]: "接口",
  [openmind.EventType.MQ]: "消息",
};
console.log(ecom.governance.openmind.DecisionTreeStatus.Assessing);
// xx.d.ts
declare namespace ecom.governance.openmind {
  export const enum DecisionTreeStatus {
    Draft = 1,
    Waiting = 2,
    Pending = 3,
    Releasing = 4,
    Running = 5,
    Disabled = 6,
    Assessing = 7,
  }

  export const enum EventType {
    Online = 1,
    Offline = 2,
    RPC = 3,
    MQ = 4,
  }
  export interface EventTypeRequest {
    eventType: EventType;
    id: string;
  }
  // ....
}

使用

// babel.config.json
{
  "plugins": [
    [
        "babel-plugin-typescript-hardcoded-enum",
        {
            "typingsRoot":"" //
        }
    ]
  ];
}

typingsRoot

typingsRoot 为当前项目的 typings 文件夹路径(会查到当前路径下的所有 d.ts 文件),也可以是一个 d.ts 的路径。设置为即可开始轻松使用 const enum。使用 babel-plugin-typescript-hardcoded-enum 后,上面的代码将编译

/* eslint-disable @typescript-eslint/no-unused-vars */
const Test1 = {
  id: '111',
  eventType: 4
};
const EventTypeMap = {
  1: '在线',
  2: '离线',
  3: '接口',
  4: '消息'
};
console.log(7)

说明

本项目 fork 自 babel-plugin-typescript-const-enum 开发。