Skip to content

Commit cc8c155

Browse files
authoredMay 30, 2024··
feat(es/minifier): Detect TypeScript enum initialization pattern (#8986)
**Description:** We can optimize ```js var Foo; Foo || Foo = {}; ``` This code looks strange, but ```ts enum Foo { a = 1, b = 2, } ``` transpiles to ```js var Foo; (function(Foo) { Foo[Foo["a"] = 1] = "a"; Foo[Foo["b"] = 2] = "b"; })(Foo || (Foo = {})); ``` and after minification it becomes ```js var Foo, Foo1; (Foo1 = Foo || (Foo = {}))[Foo1.a = 1] = "a", Foo1[Foo1.b = 2] = "b"; ```
1 parent ffc74b8 commit cc8c155

File tree

384 files changed

+885
-1049
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

384 files changed

+885
-1049
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
//// [module.d.ts]
22
//// [classPoint.ts]
3-
var A;
43
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
5-
(A || (A = {})).Point = function Point(x, y) {
6-
_class_call_check(this, Point), this.x = x, this.y = y;
7-
};
84
//// [test.ts]
95
A.Point.Origin, new A.Point(0, 0);

‎crates/swc/tests/tsc-references/ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.2.minified.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//// [ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts]
22
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
3-
var A, Point, A1, Point1 = function() {
3+
var A, Point, Point1 = function() {
44
function Point(x, y) {
55
_class_call_check(this, Point), this.x = x, this.y = y;
66
}
@@ -13,7 +13,7 @@ var A, Point, A1, Point1 = function() {
1313
}();
1414
(Point1 || (Point1 = {})).Origin = function() {
1515
return null;
16-
}, A = A1 || (A1 = {}), Point = function() {
16+
}, A = {}, Point = function() {
1717
function Point(x, y) {
1818
_class_call_check(this, Point), this.x = x, this.y = y;
1919
}

0 commit comments

Comments
 (0)
Please sign in to comment.