@eliasku/ts-transformers 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -13
- package/package.json +1 -1
- package/src/index.ts +16 -1
package/README.md
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
[](https://www.npmjs.com/package/@eliasku/ts-transformers)
|
|
2
|
+

|
|
3
|
+
|
|
1
4
|
# @eliasku/ts-transformers
|
|
2
5
|
|
|
3
6
|
TypeScript transformer for aggressive code minification through type-aware property renaming and const enum inlining.
|
|
@@ -25,6 +28,7 @@ Based on type analysis, properties are categorized as:
|
|
|
25
28
|
- **Private**: Everything else → prefixed with `$_` (mangled by minifier)
|
|
26
29
|
|
|
27
30
|
**Example:**
|
|
31
|
+
|
|
28
32
|
```typescript
|
|
29
33
|
// Before
|
|
30
34
|
class MyClass {
|
|
@@ -52,7 +56,10 @@ Replaces const enum accesses with literal values and removes declarations.
|
|
|
52
56
|
|
|
53
57
|
```typescript
|
|
54
58
|
// Before
|
|
55
|
-
const enum Status {
|
|
59
|
+
const enum Status {
|
|
60
|
+
Active = 1,
|
|
61
|
+
Inactive = 0,
|
|
62
|
+
}
|
|
56
63
|
const status = Status.Active;
|
|
57
64
|
|
|
58
65
|
// After transformer + minifier
|
|
@@ -94,7 +101,7 @@ await build({
|
|
|
94
101
|
entryPoints: ["./dist/bundle.js"],
|
|
95
102
|
outfile: "./dist/bundle.min.js",
|
|
96
103
|
minify: true,
|
|
97
|
-
mangleProps: /^\$_/,
|
|
104
|
+
mangleProps: /^\$_/, // Match your privatePrefix
|
|
98
105
|
mangleQuoted: false,
|
|
99
106
|
keepNames: false,
|
|
100
107
|
});
|
|
@@ -107,15 +114,15 @@ await build({
|
|
|
107
114
|
Entry points defining your public API surface.
|
|
108
115
|
|
|
109
116
|
```typescript
|
|
110
|
-
entrySourceFiles: ["./src/index.ts"]
|
|
117
|
+
entrySourceFiles: ["./src/index.ts"];
|
|
111
118
|
```
|
|
112
119
|
|
|
113
|
-
### privatePrefix (optional, default: "
|
|
120
|
+
### privatePrefix (optional, default: "$\_")
|
|
114
121
|
|
|
115
122
|
Prefix for private properties that will be mangled by esbuild.
|
|
116
123
|
|
|
117
124
|
```typescript
|
|
118
|
-
privatePrefix: "$_"
|
|
125
|
+
privatePrefix: "$_"; // myFunction → $_myFunction
|
|
119
126
|
```
|
|
120
127
|
|
|
121
128
|
### publicJSDocTag (optional, default: "public")
|
|
@@ -123,13 +130,13 @@ privatePrefix: "$_" // myFunction → $_myFunction
|
|
|
123
130
|
JSDoc tag marking types/properties as public. Set to empty string to disable.
|
|
124
131
|
|
|
125
132
|
```typescript
|
|
126
|
-
publicJSDocTag: "public"
|
|
133
|
+
publicJSDocTag: "public";
|
|
127
134
|
|
|
128
135
|
class MyClass {
|
|
129
136
|
/** @public */
|
|
130
|
-
apiMethod() {}
|
|
137
|
+
apiMethod() {} // Public, no prefix
|
|
131
138
|
|
|
132
|
-
internalHelper() {}
|
|
139
|
+
internalHelper() {} // Private, gets $_ prefix
|
|
133
140
|
}
|
|
134
141
|
```
|
|
135
142
|
|
|
@@ -138,12 +145,12 @@ class MyClass {
|
|
|
138
145
|
Skip renaming decorated fields.
|
|
139
146
|
|
|
140
147
|
```typescript
|
|
141
|
-
ignoreDecorated: true
|
|
148
|
+
ignoreDecorated: true;
|
|
142
149
|
|
|
143
150
|
@Component({ selector: "app-root" })
|
|
144
151
|
class AppComponent {
|
|
145
|
-
@Input() data: any;
|
|
146
|
-
private internal = 1;
|
|
152
|
+
@Input() data: any; // Not renamed
|
|
153
|
+
private internal = 1; // Renamed to $_internal
|
|
147
154
|
}
|
|
148
155
|
```
|
|
149
156
|
|
|
@@ -157,7 +164,7 @@ Inline const enum values and remove declarations.
|
|
|
157
164
|
// src/index.ts (before)
|
|
158
165
|
class API {
|
|
159
166
|
private baseUrl = "https://api.example.com";
|
|
160
|
-
|
|
167
|
+
|
|
161
168
|
/** @public */
|
|
162
169
|
async get(path: string): Promise<Response> {
|
|
163
170
|
const url = `${this.baseUrl}${path}`;
|
|
@@ -187,5 +194,16 @@ class API {
|
|
|
187
194
|
}
|
|
188
195
|
|
|
189
196
|
// After esbuild minifier
|
|
190
|
-
class A
|
|
197
|
+
class A {
|
|
198
|
+
a = "https://api.example.com";
|
|
199
|
+
async get(t) {
|
|
200
|
+
const n = `${this.a}${t}`;
|
|
201
|
+
return await fetch(n);
|
|
202
|
+
}
|
|
203
|
+
b(t) {
|
|
204
|
+
return t;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
const s = new A();
|
|
208
|
+
export { s };
|
|
191
209
|
```
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -53,7 +53,14 @@ function createTransformerFactory(
|
|
|
53
53
|
|
|
54
54
|
if (ts.isImportSpecifier(node)) {
|
|
55
55
|
const removed = tryRemoveConstEnumImport(node);
|
|
56
|
-
if (removed === undefined) {
|
|
56
|
+
if (removed === undefined || node.isTypeOnly) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (ts.isExportSpecifier(node)) {
|
|
62
|
+
const removed = tryRemoveConstEnumExport(node);
|
|
63
|
+
if (removed === undefined || node.isTypeOnly) {
|
|
57
64
|
return undefined;
|
|
58
65
|
}
|
|
59
66
|
}
|
|
@@ -672,6 +679,14 @@ function createTransformerFactory(
|
|
|
672
679
|
return node;
|
|
673
680
|
}
|
|
674
681
|
|
|
682
|
+
function tryRemoveConstEnumExport(node: ts.ExportSpecifier): ts.ExportSpecifier | undefined {
|
|
683
|
+
const importedType = typeChecker.getTypeAtLocation(node);
|
|
684
|
+
if (isConstEnumType(importedType)) {
|
|
685
|
+
return undefined;
|
|
686
|
+
}
|
|
687
|
+
return node;
|
|
688
|
+
}
|
|
689
|
+
|
|
675
690
|
function tryRemoveConstEnumImportClause(node: ts.ImportClause): ts.ImportClause | undefined {
|
|
676
691
|
if (!node.name) return node;
|
|
677
692
|
const type = typeChecker.getTypeAtLocation(node.name);
|