diff --git a/packages/commonjs/src/helpers.js b/packages/commonjs/src/helpers.js index e75841556..be25fbbd5 100644 --- a/packages/commonjs/src/helpers.js +++ b/packages/commonjs/src/helpers.js @@ -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 () { diff --git a/packages/commonjs/test/fixtures/function/esm-with-esmodule/_config.js b/packages/commonjs/test/fixtures/function/esm-with-esmodule/_config.js new file mode 100644 index 000000000..751610ae7 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/esm-with-esmodule/_config.js @@ -0,0 +1,4 @@ +module.exports = { + solo: true, + description: 'produces working code when esm module with __esModule property set is being bundled' +}; diff --git a/packages/commonjs/test/fixtures/function/esm-with-esmodule/esm.js b/packages/commonjs/test/fixtures/function/esm-with-esmodule/esm.js new file mode 100644 index 000000000..0e54e9535 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/esm-with-esmodule/esm.js @@ -0,0 +1,3 @@ +export const foo = 'foo'; + +export const __esModule = true; diff --git a/packages/commonjs/test/fixtures/function/esm-with-esmodule/main.js b/packages/commonjs/test/fixtures/function/esm-with-esmodule/main.js new file mode 100644 index 000000000..8a5c73a51 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/esm-with-esmodule/main.js @@ -0,0 +1,4 @@ +const esm = require('./esm.js'); + +t.is(esm.foo, 'foo'); +t.is(esm.__esModule, true); diff --git a/packages/commonjs/test/snapshots/function.js.md b/packages/commonjs/test/snapshots/function.js.md index e01a4cfeb..cc01161f9 100644 --- a/packages/commonjs/test/snapshots/function.js.md +++ b/packages/commonjs/test/snapshots/function.js.md @@ -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 () {␊ @@ -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 () {␊ @@ -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 () {␊ @@ -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 () {␊ @@ -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 @@ -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 () {␊ @@ -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 () {␊ @@ -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 () {␊ @@ -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 () {␊ @@ -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 () {␊ @@ -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 () {␊ @@ -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 () {␊ @@ -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 () {␊ @@ -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 () {␊ diff --git a/packages/commonjs/test/snapshots/function.js.snap b/packages/commonjs/test/snapshots/function.js.snap index c004e1f6b..00e7a556f 100644 Binary files a/packages/commonjs/test/snapshots/function.js.snap and b/packages/commonjs/test/snapshots/function.js.snap differ diff --git a/packages/commonjs/test/snapshots/test.js.md b/packages/commonjs/test/snapshots/test.js.md index 706386cd0..0dab2cd1b 100644 --- a/packages/commonjs/test/snapshots/test.js.md +++ b/packages/commonjs/test/snapshots/test.js.md @@ -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 () {␊ @@ -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 () {␊ @@ -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 () {␊ diff --git a/packages/commonjs/test/snapshots/test.js.snap b/packages/commonjs/test/snapshots/test.js.snap index 850d912ad..c7ff770ef 100644 Binary files a/packages/commonjs/test/snapshots/test.js.snap and b/packages/commonjs/test/snapshots/test.js.snap differ