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 🙏

© 2026 – Pkg Stats / Ryan Hefner

enum-loader

v1.0.0

Published

enum loader for webpack

Readme

enum-loader

enum-loader 将对 typescript 的枚举(enum)进行处理

开始

你需要先下载enum-loader

npm install --save-dev enum-loader 或 yarn add --save-dev enum-loader

配置

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.(ts|tsx)$/i,
        use: ["enum-loader"],
      },
    ],
  },
};

在 umi3 项目中

.umirc.ts

chainWebpack(config: any) {
    config.module
    .rule('enum-loader')
    .test(/.(ts|tsx)$/)
    .pre()
    .include.add([
      path.resolve(process.cwd(), 'src'),
      path.resolve(process.cwd(), 'config'),
      ])
      .end()
      .use('enum-loader')
      .loader(require.resolve('enum-loader'));
}

功能

enum-loader 会将在typescript中加了@name和@description注解的枚举进行反射处理,并向该枚举添加toArraytoDictionary静态方法。

toArray

将添加了注解@name 和 @description的枚举转换为数组,数组格式为{label: string, value: string}[],其中label属性使用的是注解@description的值,value属性使用的是枚举的 .

toDictionary

将添加了注解@name 和 @description的枚举转换为数组,数组格式为{[key: string]: {label: string, value: string}},以枚举的为key,字典的值为toArray方法的{label: string, value: string}.

示例

定义枚举

enum ENUM {
  /**
   * @name TEST
   * @description 测试
  */
 TEST = 'TEST',
 /**
  * @name STATUS
  * @description 状态
 */
 STATUS = 0,
  /**
  * @name ALL
  * @description 全部
 */
 ALL = 'ALL',
}

经过enum-loader处理之后,

// 调用枚举的toArray方法,目前需要忽略tslint的检测

// @ts-ignore
ENUM.toArray();

//输出
[
  {
    "label": "测试",
    "value": "TEST"
  },
  {
    "label": "状态",
    "value": "0"
  },
  {
    "label": "全部",
    "value": "ALL"
  }
]

//@ts-ignore
ENUM.toDictionary();

// 输出
{
  1: {label: "状态", value: "0"}
  ALL: {label: "全部", value: "ALL"}
  TEST: {label: "测试", value: "TEST"}
}