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..910b3a0af --- /dev/null +++ b/packages/commonjs/test/fixtures/function/esm-with-esmodule/_config.js @@ -0,0 +1,3 @@ +module.exports = { + 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/fixtures/function/transform-mixed-modules-esmodule/_config.js b/packages/commonjs/test/fixtures/function/transform-mixed-modules-esmodule/_config.js new file mode 100644 index 000000000..f42c36ec2 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/transform-mixed-modules-esmodule/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'transforms mixed modules when CommonJS module contains __esModule property', + pluginOptions: { + transformMixedEsModules: true + } +}; diff --git a/packages/commonjs/test/fixtures/function/transform-mixed-modules-esmodule/dep.js b/packages/commonjs/test/fixtures/function/transform-mixed-modules-esmodule/dep.js new file mode 100644 index 000000000..5f66b6b89 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/transform-mixed-modules-esmodule/dep.js @@ -0,0 +1,3 @@ +const other = require('./other.js'); + +export { other }; diff --git a/packages/commonjs/test/fixtures/function/transform-mixed-modules-esmodule/main.js b/packages/commonjs/test/fixtures/function/transform-mixed-modules-esmodule/main.js new file mode 100644 index 000000000..5d9c3a017 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/transform-mixed-modules-esmodule/main.js @@ -0,0 +1,6 @@ +import { other } from './dep.js'; + +const dep = require('./dep.js'); + +t.is(other, 'other'); +t.deepEqual(dep, { other: 'other' }); diff --git a/packages/commonjs/test/fixtures/function/transform-mixed-modules-esmodule/other.js b/packages/commonjs/test/fixtures/function/transform-mixed-modules-esmodule/other.js new file mode 100644 index 000000000..e2e129fbe --- /dev/null +++ b/packages/commonjs/test/fixtures/function/transform-mixed-modules-esmodule/other.js @@ -0,0 +1,4 @@ +Object.defineProperty(exports, '__esModule', { + value: true +}); +module.exports = 'other'; diff --git a/packages/commonjs/test/snapshots/function.js.md b/packages/commonjs/test/snapshots/function.js.md index e01a4cfeb..2828b895e 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 () {␊ @@ -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 diff --git a/packages/commonjs/test/snapshots/function.js.snap b/packages/commonjs/test/snapshots/function.js.snap index c004e1f6b..0380d4724 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