Skip to content

Commit

Permalink
fix(commonjs): correctly wrap a default class export from cjs module (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
sodatea committed Nov 27, 2022
1 parent 19ad1b6 commit ab05216
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 17 deletions.
8 changes: 7 additions & 1 deletion packages/commonjs/src/helpers.js
Expand Up @@ -37,7 +37,13 @@ export function getDefaultExportFromNamespaceIfNotNamed (n) {
export function getAugmentedNamespace(n) {
var f = n.default;
if (typeof f == "function") {
var a = function () {
var a = function a () {
if (this instanceof a) {
var args = [null];
args.push.apply(args, arguments);
var Ctor = Function.bind.apply(f, args);
return new Ctor();
}
return f.apply(this, arguments);
};
a.prototype = f.prototype;
Expand Down
@@ -0,0 +1,3 @@
const Foo = require('./other.js');

new Foo(1, 2)
@@ -0,0 +1,7 @@
export default class Foo {
constructor(...args) {
if (args.length !== 2) {
throw new Error(`new Foo(...) requires exactly 2 arguments, received ${args.length}`);
}
}
}
104 changes: 91 additions & 13 deletions packages/commonjs/test/snapshots/function.js.md
Expand Up @@ -3469,7 +3469,13 @@ Generated by [AVA](https://avajs.dev).
function getAugmentedNamespace(n) {␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function () {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
}␊
return f.apply(this, arguments);␊
};␊
a.prototype = f.prototype;␊
Expand Down Expand Up @@ -3549,7 +3555,13 @@ Generated by [AVA](https://avajs.dev).
function getAugmentedNamespace(n) {␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function () {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
}␊
return f.apply(this, arguments);␊
};␊
a.prototype = f.prototype;␊
Expand Down Expand Up @@ -3623,7 +3635,13 @@ Generated by [AVA](https://avajs.dev).
function getAugmentedNamespace(n) {␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function () {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
}␊
return f.apply(this, arguments);␊
};␊
a.prototype = f.prototype;␊
Expand Down Expand Up @@ -3710,7 +3728,13 @@ Generated by [AVA](https://avajs.dev).
function getAugmentedNamespace(n) {␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function () {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
}␊
return f.apply(this, arguments);␊
};␊
a.prototype = f.prototype;␊
Expand Down Expand Up @@ -3907,7 +3931,13 @@ Generated by [AVA](https://avajs.dev).
function getAugmentedNamespace(n) {␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function () {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
}␊
return f.apply(this, arguments);␊
};␊
a.prototype = f.prototype;␊
Expand Down Expand Up @@ -4131,7 +4161,13 @@ Generated by [AVA](https://avajs.dev).
function getAugmentedNamespace(n) {␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function () {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
}␊
return f.apply(this, arguments);␊
};␊
a.prototype = f.prototype;␊
Expand Down Expand Up @@ -4227,7 +4263,13 @@ Generated by [AVA](https://avajs.dev).
function getAugmentedNamespace(n) {␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function () {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
}␊
return f.apply(this, arguments);␊
};␊
a.prototype = f.prototype;␊
Expand Down Expand Up @@ -4414,7 +4456,13 @@ Generated by [AVA](https://avajs.dev).
function getAugmentedNamespace(n) {␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function () {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
}␊
return f.apply(this, arguments);␊
};␊
a.prototype = f.prototype;␊
Expand Down Expand Up @@ -4686,7 +4734,13 @@ Generated by [AVA](https://avajs.dev).
function getAugmentedNamespace(n) {␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function () {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
}␊
return f.apply(this, arguments);␊
};␊
a.prototype = f.prototype;␊
Expand Down Expand Up @@ -4774,7 +4828,13 @@ Generated by [AVA](https://avajs.dev).
function getAugmentedNamespace(n) {␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function () {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
}␊
return f.apply(this, arguments);␊
};␊
a.prototype = f.prototype;␊
Expand Down Expand Up @@ -6017,7 +6077,13 @@ Generated by [AVA](https://avajs.dev).
function getAugmentedNamespace(n) {␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function () {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
}␊
return f.apply(this, arguments);␊
};␊
a.prototype = f.prototype;␊
Expand Down Expand Up @@ -6062,7 +6128,13 @@ Generated by [AVA](https://avajs.dev).
function getAugmentedNamespace(n) {␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function () {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
}␊
return f.apply(this, arguments);␊
};␊
a.prototype = f.prototype;␊
Expand Down Expand Up @@ -6993,7 +7065,13 @@ Generated by [AVA](https://avajs.dev).
function getAugmentedNamespace(n) {␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function () {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
}␊
return f.apply(this, arguments);␊
};␊
a.prototype = f.prototype;␊
Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.
24 changes: 21 additions & 3 deletions packages/commonjs/test/snapshots/test.js.md
Expand Up @@ -40,7 +40,13 @@ Generated by [AVA](https://avajs.dev).
function getAugmentedNamespace(n) {␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function () {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
}␊
return f.apply(this, arguments);␊
};␊
a.prototype = f.prototype;␊
Expand Down Expand Up @@ -187,7 +193,13 @@ Generated by [AVA](https://avajs.dev).
function getAugmentedNamespace(n) {␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function () {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
}␊
return f.apply(this, arguments);␊
};␊
a.prototype = f.prototype;␊
Expand Down Expand Up @@ -261,7 +273,13 @@ Generated by [AVA](https://avajs.dev).
function getAugmentedNamespace(n) {␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function () {␊
var a = function a () {␊
if (this instanceof a) {␊
var args = [null];␊
args.push.apply(args, arguments);␊
var Ctor = Function.bind.apply(f, args);␊
return new Ctor();␊
}␊
return f.apply(this, arguments);␊
};␊
a.prototype = f.prototype;␊
Expand Down
Binary file modified packages/commonjs/test/snapshots/test.js.snap
Binary file not shown.
9 changes: 9 additions & 0 deletions packages/commonjs/test/test.js
Expand Up @@ -445,6 +445,15 @@ test('prefers to set name using directory for index files', async (t) => {
t.not(code.indexOf('var nonIndex'), -1, 'contains nonIndex');
});

test('correctly wraps the default export from a CommonJS module when it is a class', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/es-module-with-class-as-default-export/main.js',
plugins: [commonjs()]
});
const result = await executeBundle(bundle, t);
t.is(result.error, undefined);
});

test('does not warn even if the ES module does not export "default"', async (t) => {
const warns = [];
await rollup({
Expand Down

0 comments on commit ab05216

Please sign in to comment.