{"id":16299497,"url":"https://github.com/fonger/ts-transformer-optimize-const-enum","last_synced_at":"2025-08-21T22:07:26.142Z","repository":{"id":42511229,"uuid":"454520370","full_name":"Fonger/ts-transformer-optimize-const-enum","owner":"Fonger","description":"A typescript  transform that converts exported const enums into object literal.","archived":false,"fork":false,"pushed_at":"2024-08-30T20:20:01.000Z","size":836,"stargazers_count":25,"open_issues_count":3,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-11T20:48:11.686Z","etag":null,"topics":["const-enum","custom-transformer","object-literal","optimization","optimizeconstenums","transformer","ts-patch","ttypescript","typescript","typescript-plugin"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Fonger.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2022-02-01T19:21:16.000Z","updated_at":"2024-08-21T06:46:41.000Z","dependencies_parsed_at":"2023-02-08T01:15:55.049Z","dependency_job_id":"5507fa76-59e1-414b-b4e5-dac984973998","html_url":"https://github.com/Fonger/ts-transformer-optimize-const-enum","commit_stats":{"total_commits":38,"total_committers":2,"mean_commits":19.0,"dds":0.07894736842105265,"last_synced_commit":"baf2537ac02e4379cd626b5c42bed8a6f0838982"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fonger%2Fts-transformer-optimize-const-enum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fonger%2Fts-transformer-optimize-const-enum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fonger%2Fts-transformer-optimize-const-enum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fonger%2Fts-transformer-optimize-const-enum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Fonger","download_url":"https://codeload.github.com/Fonger/ts-transformer-optimize-const-enum/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221572768,"owners_count":16845714,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["const-enum","custom-transformer","object-literal","optimization","optimizeconstenums","transformer","ts-patch","ttypescript","typescript","typescript-plugin"],"created_at":"2024-10-10T20:48:12.854Z","updated_at":"2024-10-26T19:37:54.826Z","avatar_url":"https://github.com/Fonger.png","language":"TypeScript","readme":"# ts-transformer-optimize-const-enum\n\n[![](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)\n\nA typescript transformer that convert exported const enum into object literal.\n\nThis 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.\n\nThis will transform exported const enum from\n\n```ts\nexport const enum MyEnum {\n  A,\n  B,\n  C,\n  D = 10,\n  E = C * 200,\n}\n```\n\ninto object literal like this\n\n```ts\nexport const MyEnum = {\n  A: 0,\n  B: 1,\n  C: 2,\n  D: 10,\n  E: 400,\n} as const;\n```\n\nand it also strips `const` in declaration file, to make your code compatible with [`--isolatedModules`](https://www.typescriptlang.org/tsconfig#isolatedModules)\n\n```ts\n// my-enum.d.ts\ndeclare enum MyEnum { A: 0, ... }\n```\n\n## Why?\n\nConst enum can only works in the same file. It works by inlining the exact value into code.\n\n```ts\nif (cond === MyEnum.A) { /*...*/ }\n```\n\nwill compile to the following code. That's a great inline optimization.\n\n```ts\nif (cond === 0 /* A */) { /*...*/ }\n```\n\nHowever, 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.\n\nAnd the regular enum compiles to\n\n```js\nexport var MyEnum;\n(function(MyEnum) {\n  MyEnum[MyEnum['A'] = 0] = 'A';\n  MyEnum[MyEnum['B'] = 1] = 'B';\n  MyEnum[MyEnum['C'] = 2] = 'C';\n  MyEnum[MyEnum['D'] = 10] = 'D';\n  MyEnum[MyEnum['E'] = 400] = 'E';\n})(MyEnum || (MyEnum = {}));\n```\n\nwhich 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.\n\nAlthough keys of object literals can't be tree-shaken by webpack, however, the exported object literals have no side effects like enums do. If one of your code-splitting chunks does not use it, it will be completely erased.\n\n# Installation\n\n```sh\nnpm install ts-transformer-optimize-const-enum --save-dev\n```\n\n# Usage\n\nIf you use vanilla TypeScript compiler, you can use this with [ttypescript](https://github.com/cevek/ttypescript) and compile with `ttsc` instead of `tsc`\n\n## ttypescript\n\n```js\n// tsconfig.json\n{\n  \"compilerOptions\": {\n    // ...\n    \"plugins\": [\n      { \"transform\": \"ts-transformer-optimize-const-enum\" },\n      { \"transform\": \"ts-transformer-optimize-const-enum\", \"afterDeclarations\": true },\n    ]\n  },\n  // ...\n}\n```\n\nThe afterDeclarations part is to strip out const keyword from declaration file.\n\n## webpack (with ts-loader or awesome-typescript-loader)\n\n```js\n// webpack.config.js\nconst optimizeConstEnum = require('ts-transformer-optimize-const-enum').default;\n\nmodule.exports = {\n  // ...\n  module: {\n    rules: [\n      {\n        test: /\\.ts$/,\n        loader: 'ts-loader', // or 'awesome-typescript-loader'\n        options: {\n          getCustomTransformers: program =\u003e ({\n            before: [\n              optimizeConstEnum(program),\n            ],\n            afterDeclarations: [\n              optimizeConstEnum(program),\n            ],\n          }),\n        },\n      },\n    ],\n  },\n};\n```\n\n## Rollup (with @rollup/plugin-typescript or rollup-plugin-typescript2)\n\n```js\n// rollup.config.js\nimport typescript from '@rollup/plugin-typescript';\nimport optimizeConstEnum from 'ts-transformer-optimize-const-enum';\n\nexport default {\n  // ...\n  plugins: [\n    typescript({\n      transformers: [service =\u003e ({\n        before: [\n          optimizeConstEnum(service.getProgram()),\n        ],\n        afterDeclarations: [\n          optimizeConstEnum(service.getProgram()),\n        ],\n      })],\n    }),\n  ],\n};\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffonger%2Fts-transformer-optimize-const-enum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffonger%2Fts-transformer-optimize-const-enum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffonger%2Fts-transformer-optimize-const-enum/lists"}