Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(es/minifier): Respect toplevel and module options #7671

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 7 additions & 0 deletions crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,13 @@ impl Options {
c.toplevel = Some(TerserTopLevelOptions::Bool(true));
}

if matches!(
cfg.module,
None | Some(ModuleConfig::Es6 | ModuleConfig::NodeNext)
) {
c.module = true;
}

c
})
.map(BoolOrDataConfig::from_obj)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
var t;
module.exports = ((t = function() {
"use strict";
function t() {}
return t.prototype.it = function() {
this.bb = new t.MyA();
}, t;
}()).MyA = function() {}, t);
var n;
module.exports = ((n = function() {
function n() {}
return n.prototype.it = function() {
this.bb = new n.MyA();
}, n;
}()).MyA = function() {}, n);
15 changes: 7 additions & 8 deletions crates/swc/tests/fixture/sourcemap/004/output/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,28 @@
158
],
{
2943: function(n, t, u) {
"use strict";
2943: function(n, u, t) {
var r = function(n) {
var t = n.data;
var u = n.data;
return (0, _.jsx)("div", {
children: t.foo
children: u.foo
});
};
u.r(t), u.d(t, {
t.r(u), t.d(u, {
__N_SSG: function() {
return i;
},
default: function() {
return r;
}
});
var _ = u(4512), i = !0;
var _ = t(4512), i = !0;
},
7139: function(n, t, u) {
7139: function(n, u, t) {
(window.__NEXT_P = window.__NEXT_P || []).push([
"/static",
function() {
return u(2943);
return t(2943);
}
]);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/sourcemap/004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BACpB,OAAqB,AAAC,CAAA,GAAGC,EAA+CC,GAAG,AAAHA,EAAK,OAAO;oBAChFC,UAAUH,EAAKI,GAAG;gBACtB;YACJ;YACAP,EAAoBQ,CAAC,CAACT,IACDC,EAAoBS,CAAC,CAACV,GAAqB;gBACvCW,SAAS;oBAC1B,OAAqBA;gBACzB;gBACqBC,SAAS;oBAC1B,OAAqBV;gBACzB;YACJ;YACqB,IAAIG,IAAiDJ,EAAoB,OAC1FU,IAAU,CAAA;QAE1B;QACc,MAAY,SAAUZ,CAAuB,EAAEc,CAAwB,EAAEZ,CAAmB;YAC7Fa,CAAAA,OAAOC,QAAQ,GAAGD,OAAOC,QAAQ,IAAI,EAAE,AAAF,EAAIjB,IAAI,CAAC;gBAC3C;gBACA;oBACI,OAAOG,EAAoB;gBAC/B;aACH;QAGb;IACI;IACS,SAAUA,CAAmB;QAKzBA,EAAoBe,CAAC,CAAC,GAAG;YAC9B;YACA;YACA;SACH,EAAE;YACC,OAPOf,EAAoBA,EAAoBgB,CAAC,GAOxB;QAC5B,IAESC,OAD0BjB,EAAoBe,CAAC;IAGhE;CACC",
"names": [
"self",
"webpackChunk_N_E",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
var A;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
(A || (A = {})).Point = function Point(x, y) {
"use strict";
_class_call_check(this, Point), this.x = x, this.y = y;
};
//// [test.ts]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
//// [ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.ts]
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
var clodule1 = function clodule1() {
"use strict";
_class_call_check(this, clodule1);
};
clodule1 || (clodule1 = {});
var clodule2 = function clodule2() {
"use strict";
_class_call_check(this, clodule2);
};
clodule2 || (clodule2 = {});
var clodule3 = function clodule3() {
"use strict";
_class_call_check(this, clodule3);
};
(clodule3 || (clodule3 = {})).y = {
id: T
};
var clodule4 = function clodule4() {
"use strict";
_class_call_check(this, clodule4);
};
clodule4 || (clodule4 = {});
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//// [ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndGenericClassStaticFunctionOfTheSameName.ts]
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
var clodule = function() {
"use strict";
function clodule() {
_class_call_check(this, clodule);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//// [ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndNonGenericClassStaticFunctionOfTheSameName.ts]
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
var clodule = function() {
"use strict";
function clodule() {
_class_call_check(this, clodule);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//// [ClassAndModuleThatMergeWithModulesExportedStaticFunctionUsingClassPrivateStatics.ts]
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
var clodule = function() {
"use strict";
function clodule() {
_class_call_check(this, clodule);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//// [ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts]
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
var A, Point = function() {
"use strict";
function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
}
Expand All @@ -16,7 +15,6 @@ var A, Point = function() {
return null;
}, function(A) {
var Point = function() {
"use strict";
function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//// [ClassAndModuleThatMergeWithStaticFunctionAndNonExportedFunctionThatShareAName.ts]
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
var A, Point = function() {
"use strict";
function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
}
Expand All @@ -14,7 +13,6 @@ var A, Point = function() {
}();
Point || (Point = {}), function(A) {
var Point = function() {
"use strict";
function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
//// [ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts]
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
var A, Point = function Point(x, y) {
"use strict";
_class_call_check(this, Point), this.x = x, this.y = y;
};
Point.Origin = {
x: 0,
y: 0
}, (Point || (Point = {})).Origin = "", function(A) {
var Point = function Point(x, y) {
"use strict";
_class_call_check(this, Point), this.x = x, this.y = y;
};
Point.Origin = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
//// [ClassAndModuleThatMergeWithStaticVariableAndNonExportedVarThatShareAName.ts]
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
var A, Point = function Point(x, y) {
"use strict";
_class_call_check(this, Point), this.x = x, this.y = y;
};
Point.Origin = {
x: 0,
y: 0
}, Point || (Point = {}), function(A) {
var Point = function Point(x, y) {
"use strict";
_class_call_check(this, Point), this.x = x, this.y = y;
};
Point.Origin = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
//// [class.ts]
var X;
var X, X1;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
!function(X) {
(X.Y || (X.Y = {})).Point = function Point(x, y) {
"use strict";
_class_call_check(this, Point), this.x = x, this.y = y;
};
}(X || (X = {}));
((X1 = X || (X = {})).Y || (X1.Y = {})).Point = function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
};
//// [module.ts]
var X;
!function(X) {
Expand All @@ -18,7 +15,6 @@ new X.Y.Point(1, 1), X.Y.Point.Origin;
//// [simple.ts]
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
var A = function A() {
"use strict";
_class_call_check(this, A);
};
(A || (A = {})).Instance = new A(), A.Instance, new A();
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//// [class.ts]
var X;
!function(X) {
(X.Y || (X.Y = {})).Point = class {
constructor(x, y){
this.x = x, this.y = y;
}
};
}(X || (X = {}));
var X, X1;
((X1 = X || (X = {})).Y || (X1.Y = {})).Point = class {
constructor(x, y){
this.x = x, this.y = y;
}
};
//// [module.ts]
var X;
!function(X) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//// [ES5For-ofTypeCheck10.ts]
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
var StringIterator = function() {
"use strict";
function StringIterator() {
_class_call_check(this, StringIterator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ var M;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
!function(M) {
var _$Symbol, C = function() {
"use strict";
function C() {
_class_call_check(this, C);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
var Symbol;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
(new (function() {
"use strict";
function C() {
_class_call_check(this, C);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
var Symbol;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
(new (function() {
"use strict";
function C() {
_class_call_check(this, C);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
var Symbol;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
(new (function() {
"use strict";
function C() {
_class_call_check(this, C);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//// [ES5SymbolProperty6.ts]
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
(new (function() {
"use strict";
function C() {
_class_call_check(this, C);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
var Symbol;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
(new (function() {
"use strict";
function C() {
_class_call_check(this, C);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//// [EnumAndModuleWithSameNameAndCommonRoot.ts]
var enumdule;
var enumdule, enumdule1;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
!function(enumdule) {
enumdule[enumdule.Red = 0] = "Red", enumdule[enumdule.Blue = 1] = "Blue";
}(enumdule || (enumdule = {})), (enumdule || (enumdule = {})).Point = function Point(x, y) {
"use strict";
(enumdule1 = enumdule || (enumdule = {}))[enumdule1.Red = 0] = "Red", enumdule1[enumdule1.Blue = 1] = "Blue", (enumdule || (enumdule = {})).Point = function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
}, enumdule.Red, new enumdule.Point(0, 0);
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ var A;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
!function(A) {
var Point2d = function() {
"use strict";
function Point2d(x, y) {
_class_call_check(this, Point2d), this.x = x, this.y = y;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import { _ as _inherits } from "@swc/helpers/_/_inherits";
import { _ as _create_super } from "@swc/helpers/_/_create_super";
!function(A) {
var Point = function Point() {
"use strict";
_class_call_check(this, Point);
};
A.Point = Point, A.Origin = {
x: 0,
y: 0
};
var Point3d = function(Point) {
"use strict";
_inherits(Point3d, Point);
var _super = _create_super(Point3d);
function Point3d() {
Expand All @@ -26,7 +24,6 @@ import { _ as _create_super } from "@swc/helpers/_/_create_super";
y: 0,
z: 0
}, A.Line = function Line(start, end) {
"use strict";
_class_call_check(this, Line), this.start = start, this.end = end;
};
}(A || (A = {}));
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
var A;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
(A || (A = {})).points = function points() {
"use strict";
_class_call_check(this, points);
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ import { _ as _create_super } from "@swc/helpers/_/_create_super";
y: 0
};
var Point3d = function(Point) {
"use strict";
_inherits(Point3d, Point);
var _super = _create_super(Point3d);
function Point3d() {
return _class_call_check(this, Point3d), _super.apply(this, arguments);
}
return Point3d;
}(function Point() {
"use strict";
_class_call_check(this, Point);
});
A.Point3d = Point3d, A.Origin3d = {
Expand All @@ -26,7 +24,6 @@ import { _ as _create_super } from "@swc/helpers/_/_create_super";
z: 0
};
var Line = function() {
"use strict";
function Line(start, end) {
_class_call_check(this, Line), this.start = start, this.end = end;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ var A;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
!function(A) {
A.Point = function Point() {
"use strict";
_class_call_check(this, Point);
};
var Line = function Line(start, end) {
"use strict";
_class_call_check(this, Line), this.start = start, this.end = end;
};
A.Line = Line, A.fromOrigin = function(p) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ var A;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
!function(A) {
var Line = function Line(start, end) {
"use strict";
_class_call_check(this, Line), this.start = start, this.end = end;
};
A.Line = Line, A.fromOrigin = function(p) {
Expand Down