GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — babel-plugin-typescript-hardcoded-enum/REMADE_en.md at main · xiaopingbuxiao/babel-plugin-typescript-hardcoded-enum

Expand file tree

/

REMADE_en.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
93 lines (78 loc) · 2.17 KB
/

REMADE_en.md

Copy path

File metadata and controls

93 lines (78 loc) · 2.17 KB
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions

babel-plugin-typescript-hardcoded-enum

中文 | English

Description

This plugin can resolve typescript enum to hard code.

ts-loader 和 @babel/preset-typescript

when you use enum like this。

// 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;
  }
  // ....
}
// 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);

In projects compiled by ts-loader(transpileOnly : false) everything is fine and working. But in projects compiled by @babel/preset-typescript or ts-loader(transpileOnly : true) , this results in a runtime error, ecom is undefined.

babel-plugin-typescript-hardcoded-enum plugin can resolve enum to hard code like ts-loader(transpileOnly : false).

how to use

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

typingsRoot

typingsRoot is the path to the typings folder of the current project (all d.ts files under the current path will be looked up), or it can be the path to a d.ts. With babel-plugin-typescript-hardcoded-enum, the above code will compiled like this

/* eslint-disable @typescript-eslint/no-unused-vars */
const Test1 = {
  id: "111",
  eventType: 4,
};
const EventTypeMap = {
  1: "在线",
  2: "离线",
  3: "接口",
  4: "消息",
};
console.log(7);
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.