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

fix(commonjs): correctly wrap a default class export from cjs module #1350

Merged
merged 2 commits into from Nov 27, 2022
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
Jump to file
Failed to load files.
Diff view
Diff view
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