Skip to content

Commit

Permalink
fix: produce code which works when __esModule is already defined
Browse files Browse the repository at this point in the history
  • Loading branch information
bhovhannes committed Dec 16, 2022
1 parent 75e9ff4 commit e00d06e
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 0 deletions.
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,4 @@
module.exports = {
solo: true,
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);
71 changes: 71 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
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.

0 comments on commit e00d06e

Please sign in to comment.