Skip to content

Commit b36e9d7

Browse files
committed
docs(README): add usage and reason
1 parent 203058b commit b36e9d7

File tree

1 file changed

+99
-9
lines changed

1 file changed

+99
-9
lines changed

README.md

Lines changed: 99 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
# ts-transformer-optimize-const-enum
22

3-
A typescript transpiler that transform exported const enum into object literal! This is just like the one from [@babel/preset-typescript or @babel/plugin-transform-typescript with optimizeConstEnums: true](https://babeljs.io/docs/en/babel-preset-typescript#optimizeconstenums) but it works for typescript compiler.
3+
A typescript transpiler that transform exported const enum into object literal.
4+
5+
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.
46

57
This will transform exported const enum from
68

79
```ts
810
export const enum MyEnum {
911
A,
1012
B,
11-
C
13+
C,
1214
D = 10,
13-
D = C * 200
15+
E = C * 200
1416
}
1517
```
1618

@@ -23,21 +25,110 @@ export const MyEnum {
2325
C: 2,
2426
D: 10,
2527
E: 400
26-
}
28+
} as const
2729
```
2830

29-
while stripping const in declaration file, to make your code compatible with `--isolatedModules`
31+
and it also strips `const` in declaration file, to make your code compatible with [`--isolatedModules`](https://www.typescriptlang.org/tsconfig#isolatedModules)
3032

3133
```ts
32-
declare enum MyEnum { ... }
34+
// my-enum.d.ts
35+
declare enum MyEnum { A: 0, ... }
3336
```
3437

3538
## Why?
36-
WIP
39+
40+
Const enum can only works in the same file. It works by inlining the exact value into code.
41+
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.
42+
43+
However, the regular enum compiles to
44+
45+
```js
46+
export var MyEnum;
47+
(function(MyEnum) {
48+
MyEnum[MyEnum['A'] = 0] = 'A';
49+
MyEnum[MyEnum['B'] = 1] = 'B';
50+
MyEnum[MyEnum['C'] = 2] = 'C';
51+
MyEnum[MyEnum['D'] = 10] = 'D';
52+
MyEnum[MyEnum['E'] = 400] = 'E';
53+
})(MyEnum || (MyEnum = {}));
54+
```
55+
56+
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.
3757

3858
# Usage
3959

40-
WIP
60+
## ttypescript
61+
62+
If you use vanilla TypeScript compiler, you can use this with [ttypescript](https://github.com/cevek/ttypescript) and compile with `ttsc` instead of `tsc`
63+
64+
```js
65+
// tsconfig.json
66+
{
67+
"compilerOptions": {
68+
// ...
69+
"plugins": [
70+
{ "transform": "ts-transformer-optimize-const-enum" },
71+
{ "transform": "ts-transformer-optimize-const-enum", "afterDeclarations": true },
72+
]
73+
},
74+
// ...
75+
}
76+
```
77+
78+
The afterDeclarations part is to strip out const keyword from declaration file.
79+
80+
## webpack (with ts-loader or awesome-typescript-loader)
81+
82+
```js
83+
// webpack.config.js
84+
const optimizeConstEnum = require('ts-transformer-optimize-const-enum').default;
85+
86+
module.exports = {
87+
// ...
88+
module: {
89+
rules: [
90+
{
91+
test: /\.ts$/,
92+
loader: 'ts-loader', // or 'awesome-typescript-loader'
93+
options: {
94+
getCustomTransformers: program => ({
95+
before: [
96+
optimizeConstEnum(program),
97+
],
98+
afterDeclarations: [
99+
optimizeConstEnum(program),
100+
],
101+
}),
102+
},
103+
},
104+
],
105+
},
106+
};
107+
```
108+
109+
## Rollup (with rollup-plugin-typescript2)
110+
111+
```js
112+
// rollup.config.js
113+
import typescript from 'rollup-plugin-typescript2';
114+
import optimizeConstEnum from 'ts-transformer-optimize-const-enum';
115+
116+
export default {
117+
// ...
118+
plugins: [
119+
typescript({
120+
transformers: [service => ({
121+
before: [
122+
optimizeConstEnum(service.getProgram()),
123+
],
124+
afterDeclarations: [
125+
optimizeConstEnum(service.getProgram()),
126+
],
127+
})],
128+
}),
129+
],
130+
};
131+
```
41132

42133
# Caveats
43134

@@ -52,4 +143,3 @@ export const enum WorkingEnum {}
52143
const enum FailingEnum {}
53144
export FailEnum;
54145
```
55-

0 commit comments

Comments
 (0)