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): produce code which works when __esModule is already defined #1379

Merged
merged 1 commit into from
Dec 17, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/commonjs/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function getDefaultExportFromNamespaceIfNotNamed (n) {
}

export function getAugmentedNamespace(n) {
if (n.__esModule) return n;
var f = n.default;
if (typeof f == "function") {
var a = function a () {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
description: 'produces working code when esm module with __esModule property set is being bundled'
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const foo = 'foo';

export const __esModule = true;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const esm = require('./esm.js');

t.is(esm.foo, 'foo');
t.is(esm.__esModule, true);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
description: 'transforms mixed modules when CommonJS module contains __esModule property',
pluginOptions: {
transformMixedEsModules: true
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const other = require('./other.js');

export { other };
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { other } from './dep.js';

const dep = require('./dep.js');

t.is(other, 'other');
t.deepEqual(dep, { other: 'other' });
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Object.defineProperty(exports, '__esModule', {
value: true
});
module.exports = 'other';
133 changes: 133 additions & 0 deletions packages/commonjs/test/snapshots/function.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -3491,6 +3491,7 @@ Generated by [AVA](https://avajs.dev).
var externalEsmDefault__namespace = /*#__PURE__*/_interopNamespaceCompat(externalEsmDefault);␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -3577,6 +3578,7 @@ Generated by [AVA](https://avajs.dev).
var externalEsmDefault__namespace = /*#__PURE__*/_interopNamespaceCompat(externalEsmDefault);␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -3657,6 +3659,7 @@ Generated by [AVA](https://avajs.dev).
var externalEsmDefault__namespace = /*#__PURE__*/_interopNamespaceCompat(externalEsmDefault);␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -3750,6 +3753,7 @@ Generated by [AVA](https://avajs.dev).
'main.js': `'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -3834,6 +3838,64 @@ Generated by [AVA](https://avajs.dev).
`,
}

## esm-with-esmodule

> Snapshot 1

{
'main.js': `'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "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;␊
} else a = {};␊
Object.defineProperty(a, '__esModule', {value: true});␊
Object.keys(n).forEach(function (k) {␊
var d = Object.getOwnPropertyDescriptor(n, k);␊
Object.defineProperty(a, k, d.get ? d : {␊
enumerable: true,␊
get: function () {␊
return n[k];␊
}␊
});␊
});␊
return a;␊
}␊
var main = {};␊
const foo = 'foo';␊
const __esModule = true;␊
var esm$1 = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
foo: foo,␊
__esModule: __esModule␊
});␊
var require$$0 = /*@__PURE__*/getAugmentedNamespace(esm$1);␊
const esm = require$$0;␊
t.is(esm.foo, 'foo');␊
t.is(esm.__esModule, true);␊
module.exports = main;␊
`,
}

## export-default-from

> Snapshot 1
Expand Down Expand Up @@ -3957,6 +4019,7 @@ Generated by [AVA](https://avajs.dev).
'main.js': `'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -4187,6 +4250,7 @@ Generated by [AVA](https://avajs.dev).
var externalEsmDefault__namespace = /*#__PURE__*/_interopNamespaceCompat(externalEsmDefault);␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -4289,6 +4353,7 @@ Generated by [AVA](https://avajs.dev).
'main.js': `'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -4482,6 +4547,7 @@ Generated by [AVA](https://avajs.dev).
}␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -4760,6 +4826,7 @@ Generated by [AVA](https://avajs.dev).
var externalEsmDefault__namespace = /*#__PURE__*/_interopNamespaceCompat(externalEsmDefault);␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -4854,6 +4921,7 @@ Generated by [AVA](https://avajs.dev).
'main.js': `'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -6163,6 +6231,7 @@ Generated by [AVA](https://avajs.dev).
'main.js': `'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -6214,6 +6283,7 @@ Generated by [AVA](https://avajs.dev).
'main.js': `'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -7167,6 +7237,7 @@ Generated by [AVA](https://avajs.dev).
'main.js': `'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -7211,6 +7282,68 @@ Generated by [AVA](https://avajs.dev).
`,
}

## transform-mixed-modules-esmodule

> Snapshot 1

{
'main.js': `'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "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;␊
} else a = {};␊
Object.defineProperty(a, '__esModule', {value: true});␊
Object.keys(n).forEach(function (k) {␊
var d = Object.getOwnPropertyDescriptor(n, k);␊
Object.defineProperty(a, k, d.get ? d : {␊
enumerable: true,␊
get: function () {␊
return n[k];␊
}␊
});␊
});␊
return a;␊
}␊
var otherExports = {};␊
var other$1 = {␊
get exports(){ return otherExports; },␊
set exports(v){ otherExports = v; },␊
};␊
Object.defineProperty(otherExports, '__esModule', {␊
value: true␊
});␊
other$1.exports = 'other';␊
const other = otherExports;␊
var dep$1 = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
other: other␊
});␊
var require$$0 = /*@__PURE__*/getAugmentedNamespace(dep$1);␊
const dep = require$$0;␊
t.is(other, 'other');␊
t.deepEqual(dep, { other: 'other' });␊
`,
}

## transpiled-esm-default

> Snapshot 1
Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.
3 changes: 3 additions & 0 deletions packages/commonjs/test/snapshots/test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Generated by [AVA](https://avajs.dev).
}␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -199,6 +200,7 @@ Generated by [AVA](https://avajs.dev).
`'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down Expand Up @@ -279,6 +281,7 @@ Generated by [AVA](https://avajs.dev).
`'use strict';␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var f = n.default;␊
if (typeof f == "function") {␊
var a = function a () {␊
Expand Down
Binary file modified packages/commonjs/test/snapshots/test.js.snap
Binary file not shown.