Skip to content

Commit ab05216

Browse files
authored
fix(commonjs): correctly wrap a default class export from cjs module (#1350)
* fix: correctly wraps the default export from a CommonJS module when it is a class Fixes https://github.com/vitejs/vite/issues/10853 Since 3c00191 the namespace became callable when requiring ESM with function default, but it isn't newable, leading to errors when the function is actually a class. * test: update snapshots as the helper code has changed
1 parent 19ad1b6 commit ab05216

File tree

8 files changed

+138
-17
lines changed

8 files changed

+138
-17
lines changed

β€Žpackages/commonjs/src/helpers.jsβ€Ž

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ export function getDefaultExportFromNamespaceIfNotNamed (n) {
3737
export function getAugmentedNamespace(n) {
3838
var f = n.default;
3939
if (typeof f == "function") {
40-
var a = function () {
40+
var a = function a () {
41+
if (this instanceof a) {
42+
var args = [null];
43+
args.push.apply(args, arguments);
44+
var Ctor = Function.bind.apply(f, args);
45+
return new Ctor();
46+
}
4147
return f.apply(this, arguments);
4248
};
4349
a.prototype = f.prototype;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const Foo = require('./other.js');
2+
3+
new Foo(1, 2)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default class Foo {
2+
constructor(...args) {
3+
if (args.length !== 2) {
4+
throw new Error(`new Foo(...) requires exactly 2 arguments, received ${args.length}`);
5+
}
6+
}
7+
}

β€Žpackages/commonjs/test/snapshots/function.js.mdβ€Ž

Lines changed: 91 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3469,7 +3469,13 @@ Generated by [AVA](https://avajs.dev).
34693469
function getAugmentedNamespace(n) {␊
34703470
var f = n.default;␊
34713471
if (typeof f == "function") {␊
3472-
var a = function () {␊
3472+
var a = function a () {␊
3473+
if (this instanceof a) {␊
3474+
var args = [null];␊
3475+
args.push.apply(args, arguments);␊
3476+
var Ctor = Function.bind.apply(f, args);␊
3477+
return new Ctor();␊
3478+
}␊
34733479
return f.apply(this, arguments);␊
34743480
};␊
34753481
a.prototype = f.prototype;␊
@@ -3549,7 +3555,13 @@ Generated by [AVA](https://avajs.dev).
35493555
function getAugmentedNamespace(n) {␊
35503556
var f = n.default;␊
35513557
if (typeof f == "function") {␊
3552-
var a = function () {␊
3558+
var a = function a () {␊
3559+
if (this instanceof a) {␊
3560+
var args = [null];␊
3561+
args.push.apply(args, arguments);␊
3562+
var Ctor = Function.bind.apply(f, args);␊
3563+
return new Ctor();␊
3564+
}␊
35533565
return f.apply(this, arguments);␊
35543566
};␊
35553567
a.prototype = f.prototype;␊
@@ -3623,7 +3635,13 @@ Generated by [AVA](https://avajs.dev).
36233635
function getAugmentedNamespace(n) {␊
36243636
var f = n.default;␊
36253637
if (typeof f == "function") {␊
3626-
var a = function () {␊
3638+
var a = function a () {␊
3639+
if (this instanceof a) {␊
3640+
var args = [null];␊
3641+
args.push.apply(args, arguments);␊
3642+
var Ctor = Function.bind.apply(f, args);␊
3643+
return new Ctor();␊
3644+
}␊
36273645
return f.apply(this, arguments);␊
36283646
};␊
36293647
a.prototype = f.prototype;␊
@@ -3710,7 +3728,13 @@ Generated by [AVA](https://avajs.dev).
37103728
function getAugmentedNamespace(n) {␊
37113729
var f = n.default;␊
37123730
if (typeof f == "function") {␊
3713-
var a = function () {␊
3731+
var a = function a () {␊
3732+
if (this instanceof a) {␊
3733+
var args = [null];␊
3734+
args.push.apply(args, arguments);␊
3735+
var Ctor = Function.bind.apply(f, args);␊
3736+
return new Ctor();␊
3737+
}␊
37143738
return f.apply(this, arguments);␊
37153739
};␊
37163740
a.prototype = f.prototype;␊
@@ -3907,7 +3931,13 @@ Generated by [AVA](https://avajs.dev).
39073931
function getAugmentedNamespace(n) {␊
39083932
var f = n.default;␊
39093933
if (typeof f == "function") {␊
3910-
var a = function () {␊
3934+
var a = function a () {␊
3935+
if (this instanceof a) {␊
3936+
var args = [null];␊
3937+
args.push.apply(args, arguments);␊
3938+
var Ctor = Function.bind.apply(f, args);␊
3939+
return new Ctor();␊
3940+
}␊
39113941
return f.apply(this, arguments);␊
39123942
};␊
39133943
a.prototype = f.prototype;␊
@@ -4131,7 +4161,13 @@ Generated by [AVA](https://avajs.dev).
41314161
function getAugmentedNamespace(n) {␊
41324162
var f = n.default;␊
41334163
if (typeof f == "function") {␊
4134-
var a = function () {␊
4164+
var a = function a () {␊
4165+
if (this instanceof a) {␊
4166+
var args = [null];␊
4167+
args.push.apply(args, arguments);␊
4168+
var Ctor = Function.bind.apply(f, args);␊
4169+
return new Ctor();␊
4170+
}␊
41354171
return f.apply(this, arguments);␊
41364172
};␊
41374173
a.prototype = f.prototype;␊
@@ -4227,7 +4263,13 @@ Generated by [AVA](https://avajs.dev).
42274263
function getAugmentedNamespace(n) {␊
42284264
var f = n.default;␊
42294265
if (typeof f == "function") {␊
4230-
var a = function () {␊
4266+
var a = function a () {␊
4267+
if (this instanceof a) {␊
4268+
var args = [null];␊
4269+
args.push.apply(args, arguments);␊
4270+
var Ctor = Function.bind.apply(f, args);␊
4271+
return new Ctor();␊
4272+
}␊
42314273
return f.apply(this, arguments);␊
42324274
};␊
42334275
a.prototype = f.prototype;␊
@@ -4414,7 +4456,13 @@ Generated by [AVA](https://avajs.dev).
44144456
function getAugmentedNamespace(n) {␊
44154457
var f = n.default;␊
44164458
if (typeof f == "function") {␊
4417-
var a = function () {␊
4459+
var a = function a () {␊
4460+
if (this instanceof a) {␊
4461+
var args = [null];␊
4462+
args.push.apply(args, arguments);␊
4463+
var Ctor = Function.bind.apply(f, args);␊
4464+
return new Ctor();␊
4465+
}␊
44184466
return f.apply(this, arguments);␊
44194467
};␊
44204468
a.prototype = f.prototype;␊
@@ -4686,7 +4734,13 @@ Generated by [AVA](https://avajs.dev).
46864734
function getAugmentedNamespace(n) {␊
46874735
var f = n.default;␊
46884736
if (typeof f == "function") {␊
4689-
var a = function () {␊
4737+
var a = function a () {␊
4738+
if (this instanceof a) {␊
4739+
var args = [null];␊
4740+
args.push.apply(args, arguments);␊
4741+
var Ctor = Function.bind.apply(f, args);␊
4742+
return new Ctor();␊
4743+
}␊
46904744
return f.apply(this, arguments);␊
46914745
};␊
46924746
a.prototype = f.prototype;␊
@@ -4774,7 +4828,13 @@ Generated by [AVA](https://avajs.dev).
47744828
function getAugmentedNamespace(n) {␊
47754829
var f = n.default;␊
47764830
if (typeof f == "function") {␊
4777-
var a = function () {␊
4831+
var a = function a () {␊
4832+
if (this instanceof a) {␊
4833+
var args = [null];␊
4834+
args.push.apply(args, arguments);␊
4835+
var Ctor = Function.bind.apply(f, args);␊
4836+
return new Ctor();␊
4837+
}␊
47784838
return f.apply(this, arguments);␊
47794839
};␊
47804840
a.prototype = f.prototype;␊
@@ -6017,7 +6077,13 @@ Generated by [AVA](https://avajs.dev).
60176077
function getAugmentedNamespace(n) {␊
60186078
var f = n.default;␊
60196079
if (typeof f == "function") {␊
6020-
var a = function () {␊
6080+
var a = function a () {␊
6081+
if (this instanceof a) {␊
6082+
var args = [null];␊
6083+
args.push.apply(args, arguments);␊
6084+
var Ctor = Function.bind.apply(f, args);␊
6085+
return new Ctor();␊
6086+
}␊
60216087
return f.apply(this, arguments);␊
60226088
};␊
60236089
a.prototype = f.prototype;␊
@@ -6062,7 +6128,13 @@ Generated by [AVA](https://avajs.dev).
60626128
function getAugmentedNamespace(n) {␊
60636129
var f = n.default;␊
60646130
if (typeof f == "function") {␊
6065-
var a = function () {␊
6131+
var a = function a () {␊
6132+
if (this instanceof a) {␊
6133+
var args = [null];␊
6134+
args.push.apply(args, arguments);␊
6135+
var Ctor = Function.bind.apply(f, args);␊
6136+
return new Ctor();␊
6137+
}␊
60666138
return f.apply(this, arguments);␊
60676139
};␊
60686140
a.prototype = f.prototype;␊
@@ -6993,7 +7065,13 @@ Generated by [AVA](https://avajs.dev).
69937065
function getAugmentedNamespace(n) {␊
69947066
var f = n.default;␊
69957067
if (typeof f == "function") {␊
6996-
var a = function () {␊
7068+
var a = function a () {␊
7069+
if (this instanceof a) {␊
7070+
var args = [null];␊
7071+
args.push.apply(args, arguments);␊
7072+
var Ctor = Function.bind.apply(f, args);␊
7073+
return new Ctor();␊
7074+
}␊
69977075
return f.apply(this, arguments);␊
69987076
};␊
69997077
a.prototype = f.prototype;␊
94 Bytes
Binary file not shown.

β€Žpackages/commonjs/test/snapshots/test.js.mdβ€Ž

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ Generated by [AVA](https://avajs.dev).
4040
function getAugmentedNamespace(n) {␊
4141
var f = n.default;␊
4242
if (typeof f == "function") {␊
43-
var a = function () {␊
43+
var a = function a () {␊
44+
if (this instanceof a) {␊
45+
var args = [null];␊
46+
args.push.apply(args, arguments);␊
47+
var Ctor = Function.bind.apply(f, args);␊
48+
return new Ctor();␊
49+
}␊
4450
return f.apply(this, arguments);␊
4551
};␊
4652
a.prototype = f.prototype;␊
@@ -187,7 +193,13 @@ Generated by [AVA](https://avajs.dev).
187193
function getAugmentedNamespace(n) {␊
188194
var f = n.default;␊
189195
if (typeof f == "function") {␊
190-
var a = function () {␊
196+
var a = function a () {␊
197+
if (this instanceof a) {␊
198+
var args = [null];␊
199+
args.push.apply(args, arguments);␊
200+
var Ctor = Function.bind.apply(f, args);␊
201+
return new Ctor();␊
202+
}␊
191203
return f.apply(this, arguments);␊
192204
};␊
193205
a.prototype = f.prototype;␊
@@ -261,7 +273,13 @@ Generated by [AVA](https://avajs.dev).
261273
function getAugmentedNamespace(n) {␊
262274
var f = n.default;␊
263275
if (typeof f == "function") {␊
264-
var a = function () {␊
276+
var a = function a () {␊
277+
if (this instanceof a) {␊
278+
var args = [null];␊
279+
args.push.apply(args, arguments);␊
280+
var Ctor = Function.bind.apply(f, args);␊
281+
return new Ctor();␊
282+
}␊
265283
return f.apply(this, arguments);␊
266284
};␊
267285
a.prototype = f.prototype;␊
58 Bytes
Binary file not shown.

β€Žpackages/commonjs/test/test.jsβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,15 @@ test('prefers to set name using directory for index files', async (t) => {
445445
t.not(code.indexOf('var nonIndex'), -1, 'contains nonIndex');
446446
});
447447

448+
test('correctly wraps the default export from a CommonJS module when it is a class', async (t) => {
449+
const bundle = await rollup({
450+
input: 'fixtures/samples/es-module-with-class-as-default-export/main.js',
451+
plugins: [commonjs()]
452+
});
453+
const result = await executeBundle(bundle, t);
454+
t.is(result.error, undefined);
455+
});
456+
448457
test('does not warn even if the ES module does not export "default"', async (t) => {
449458
const warns = [];
450459
await rollup({

0 commit comments

Comments
Β (0)