Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ts-transformer-optimize-const-enum

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-transformer-optimize-const-enum - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

2

dist/cjs/transform.d.ts
import ts from 'typescript';
export default function (program: ts.Program, pluginOptions: unknown): (ctx: ts.TransformationContext) => (sourceFile: ts.SourceFile) => ts.SourceFile;
export default function (program: ts.Program, pluginOptions?: unknown): (ctx: ts.TransformationContext) => (sourceFile: ts.SourceFile) => ts.SourceFile;
{
"name": "ts-transformer-optimize-const-enum",
"version": "0.0.1",
"version": "0.0.2",
"description": "A TypeScript transformer that optimize exported const enum into object literal",

@@ -24,6 +24,9 @@ "main": "dist/cjs/transform.js",

"build:ejs": "tsc --outDir dist/ejs --module es6",
"test": "jest --coverage",
"lint": "dprint check && eslint .",
"format": "dprint fmt"
},
"files": ["dist"],
"devDependencies": {
"@types/jest": "^27.4.0",
"@typescript-eslint/eslint-plugin": "^5.10.2",

@@ -33,2 +36,4 @@ "@typescript-eslint/parser": "^5.10.2",

"eslint": "^8.8.0",
"jest": "^27.4.7",
"ts-jest": "^27.1.3",
"ttypescript": "^1.5.13",

@@ -35,0 +40,0 @@ "typescript": "^4.5.5"

# ts-transformer-optimize-const-enum
A typescript transpiler that transform exported const enum into object literal.
[![](https://img.shields.io/npm/v/ts-transformer-optimize-const-enum.svg)](https://www.npmjs.com/package/ts-transformer-optimize-const-enum) ![CI Status](https://github.com/Fonger/ts-transformer-optimize-const-enum/actions/workflows/test.yml/badge.svg) [![codecov](https://codecov.io/gh/Fonger/ts-transformer-optimize-const-enum/branch/main/graph/badge.svg?token=CHDVP7EMNA)](https://codecov.io/gh/Fonger/ts-transformer-optimize-const-enum)
A typescript transformer that convert exported const enum into object literal.
This is just like the one from [@babel/preset-typescript with optimizeConstEnums: true](https://babeljs.io/docs/en/babel-preset-typescript#optimizeconstenums) but it works for typescript compiler.

@@ -15,3 +17,3 @@

D = 10,
E = C * 200
E = C * 200,
}

@@ -42,6 +44,17 @@ ```

Const enum can only works in the same file. It works by inlining the exact value into code.
With [isolateModules](https://www.typescriptlang.org/tsconfig#isolatedModules), you can't use the exported const enum. The solution is to enable [preserveConstEnums](https://www.typescriptlang.org/tsconfig#preserveConstEnums) option to convert const enum to regular enum.
However, the regular enum compiles to
```ts
if (cond === MyEnum.A) { /*...*/ }
```
will compile to the following code. That's a great inline optimization.
```ts
if (cond === 0 /* A */) { /*...*/ }
```
However, const enums only work in the same file with [isolateModules](https://www.typescriptlang.org/tsconfig#isolatedModules). Therefore, you can't use the exported const enum. The solution is to enable [preserveConstEnums](https://www.typescriptlang.org/tsconfig#preserveConstEnums) option to convert const enums to regular enums.
And the regular enum compiles to
```js

@@ -58,10 +71,16 @@ export var MyEnum;

which is ugly and waste a lot of bytes. Not only can't you take advantage of enum inlining, but it also wastes a lot of bytes. That's why this transform existed.
which is verbose. Not only can't you take advantage of enum inlining, but it also wastes a lot of bytes. That's the reason why this transform is made.
# Installation
```sh
npm install ts-transformer-optimize-const-enum --save-dev
```
# Usage
If you use vanilla TypeScript compiler, you can use this with [ttypescript](https://github.com/cevek/ttypescript) and compile with `ttsc` instead of `tsc`
## ttypescript
If you use vanilla TypeScript compiler, you can use this with [ttypescript](https://github.com/cevek/ttypescript) and compile with `ttsc` instead of `tsc`
```js

@@ -139,3 +158,2 @@ // tsconfig.json

Currently, only immediate export const enum works. For example:
This will be fixed in later version.

@@ -150,1 +168,3 @@ ```ts

```
This may be fixed in future release.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc