Skip to content

Commit dcb86a9

Browse files
authored
fix(commonjs): produce code which works when __esModule is already defined (#1379)
1 parent b46cb29 commit dcb86a9

File tree

12 files changed

+166
-0
lines changed

12 files changed

+166
-0
lines changed

packages/commonjs/src/helpers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export function getDefaultExportFromNamespaceIfNotNamed (n) {
3535
}
3636
3737
export function getAugmentedNamespace(n) {
38+
if (n.__esModule) return n;
3839
var f = n.default;
3940
if (typeof f == "function") {
4041
var a = function a () {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
description: 'produces working code when esm module with __esModule property set is being bundled'
3+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const foo = 'foo';
2+
3+
export const __esModule = true;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const esm = require('./esm.js');
2+
3+
t.is(esm.foo, 'foo');
4+
t.is(esm.__esModule, true);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
description: 'transforms mixed modules when CommonJS module contains __esModule property',
3+
pluginOptions: {
4+
transformMixedEsModules: true
5+
}
6+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const other = require('./other.js');
2+
3+
export { other };
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { other } from './dep.js';
2+
3+
const dep = require('./dep.js');
4+
5+
t.is(other, 'other');
6+
t.deepEqual(dep, { other: 'other' });
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Object.defineProperty(exports, '__esModule', {
2+
value: true
3+
});
4+
module.exports = 'other';

packages/commonjs/test/snapshots/function.js.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3491,6 +3491,7 @@ Generated by [AVA](https://avajs.dev).
34913491
var externalEsmDefault__namespace = /*#__PURE__*/_interopNamespaceCompat(externalEsmDefault);␊
34923492
34933493
function getAugmentedNamespace(n) {␊
3494+
if (n.__esModule) return n;␊
34943495
var f = n.default;␊
34953496
if (typeof f == "function") {␊
34963497
var a = function a () {␊
@@ -3577,6 +3578,7 @@ Generated by [AVA](https://avajs.dev).
35773578
var externalEsmDefault__namespace = /*#__PURE__*/_interopNamespaceCompat(externalEsmDefault);␊
35783579
35793580
function getAugmentedNamespace(n) {␊
3581+
if (n.__esModule) return n;␊
35803582
var f = n.default;␊
35813583
if (typeof f == "function") {␊
35823584
var a = function a () {␊
@@ -3657,6 +3659,7 @@ Generated by [AVA](https://avajs.dev).
36573659
var externalEsmDefault__namespace = /*#__PURE__*/_interopNamespaceCompat(externalEsmDefault);␊
36583660
36593661
function getAugmentedNamespace(n) {␊
3662+
if (n.__esModule) return n;␊
36603663
var f = n.default;␊
36613664
if (typeof f == "function") {␊
36623665
var a = function a () {␊
@@ -3750,6 +3753,7 @@ Generated by [AVA](https://avajs.dev).
37503753
'main.js': `'use strict';␊
37513754
37523755
function getAugmentedNamespace(n) {␊
3756+
if (n.__esModule) return n;␊
37533757
var f = n.default;␊
37543758
if (typeof f == "function") {␊
37553759
var a = function a () {␊
@@ -3834,6 +3838,64 @@ Generated by [AVA](https://avajs.dev).
38343838
`,
38353839
}
38363840

3841+
## esm-with-esmodule
3842+
3843+
> Snapshot 1
3844+
3845+
{
3846+
'main.js': `'use strict';␊
3847+
3848+
function getAugmentedNamespace(n) {␊
3849+
if (n.__esModule) return n;␊
3850+
var f = n.default;␊
3851+
if (typeof f == "function") {␊
3852+
var a = function a () {␊
3853+
if (this instanceof a) {␊
3854+
var args = [null];␊
3855+
args.push.apply(args, arguments);␊
3856+
var Ctor = Function.bind.apply(f, args);␊
3857+
return new Ctor();␊
3858+
}␊
3859+
return f.apply(this, arguments);␊
3860+
};␊
3861+
a.prototype = f.prototype;␊
3862+
} else a = {};␊
3863+
Object.defineProperty(a, '__esModule', {value: true});␊
3864+
Object.keys(n).forEach(function (k) {␊
3865+
var d = Object.getOwnPropertyDescriptor(n, k);␊
3866+
Object.defineProperty(a, k, d.get ? d : {␊
3867+
enumerable: true,␊
3868+
get: function () {␊
3869+
return n[k];␊
3870+
}␊
3871+
});␊
3872+
});␊
3873+
return a;␊
3874+
}␊
3875+
3876+
var main = {};␊
3877+
3878+
const foo = 'foo';␊
3879+
3880+
const __esModule = true;␊
3881+
3882+
var esm$1 = /*#__PURE__*/Object.freeze({␊
3883+
__proto__: null,␊
3884+
foo: foo,␊
3885+
__esModule: __esModule␊
3886+
});␊
3887+
3888+
var require$$0 = /*@__PURE__*/getAugmentedNamespace(esm$1);␊
3889+
3890+
const esm = require$$0;␊
3891+
3892+
t.is(esm.foo, 'foo');␊
3893+
t.is(esm.__esModule, true);␊
3894+
3895+
module.exports = main;␊
3896+
`,
3897+
}
3898+
38373899
## export-default-from
38383900

38393901
> Snapshot 1
@@ -3957,6 +4019,7 @@ Generated by [AVA](https://avajs.dev).
39574019
'main.js': `'use strict';␊
39584020
39594021
function getAugmentedNamespace(n) {␊
4022+
if (n.__esModule) return n;␊
39604023
var f = n.default;␊
39614024
if (typeof f == "function") {␊
39624025
var a = function a () {␊
@@ -4187,6 +4250,7 @@ Generated by [AVA](https://avajs.dev).
41874250
var externalEsmDefault__namespace = /*#__PURE__*/_interopNamespaceCompat(externalEsmDefault);␊
41884251
41894252
function getAugmentedNamespace(n) {␊
4253+
if (n.__esModule) return n;␊
41904254
var f = n.default;␊
41914255
if (typeof f == "function") {␊
41924256
var a = function a () {␊
@@ -4289,6 +4353,7 @@ Generated by [AVA](https://avajs.dev).
42894353
'main.js': `'use strict';␊
42904354
42914355
function getAugmentedNamespace(n) {␊
4356+
if (n.__esModule) return n;␊
42924357
var f = n.default;␊
42934358
if (typeof f == "function") {␊
42944359
var a = function a () {␊
@@ -4482,6 +4547,7 @@ Generated by [AVA](https://avajs.dev).
44824547
}␊
44834548
44844549
function getAugmentedNamespace(n) {␊
4550+
if (n.__esModule) return n;␊
44854551
var f = n.default;␊
44864552
if (typeof f == "function") {␊
44874553
var a = function a () {␊
@@ -4760,6 +4826,7 @@ Generated by [AVA](https://avajs.dev).
47604826
var externalEsmDefault__namespace = /*#__PURE__*/_interopNamespaceCompat(externalEsmDefault);␊
47614827
47624828
function getAugmentedNamespace(n) {␊
4829+
if (n.__esModule) return n;␊
47634830
var f = n.default;␊
47644831
if (typeof f == "function") {␊
47654832
var a = function a () {␊
@@ -4854,6 +4921,7 @@ Generated by [AVA](https://avajs.dev).
48544921
'main.js': `'use strict';␊
48554922
48564923
function getAugmentedNamespace(n) {␊
4924+
if (n.__esModule) return n;␊
48574925
var f = n.default;␊
48584926
if (typeof f == "function") {␊
48594927
var a = function a () {␊
@@ -6163,6 +6231,7 @@ Generated by [AVA](https://avajs.dev).
61636231
'main.js': `'use strict';␊
61646232
61656233
function getAugmentedNamespace(n) {␊
6234+
if (n.__esModule) return n;␊
61666235
var f = n.default;␊
61676236
if (typeof f == "function") {␊
61686237
var a = function a () {␊
@@ -6214,6 +6283,7 @@ Generated by [AVA](https://avajs.dev).
62146283
'main.js': `'use strict';␊
62156284
62166285
function getAugmentedNamespace(n) {␊
6286+
if (n.__esModule) return n;␊
62176287
var f = n.default;␊
62186288
if (typeof f == "function") {␊
62196289
var a = function a () {␊
@@ -7167,6 +7237,7 @@ Generated by [AVA](https://avajs.dev).
71677237
'main.js': `'use strict';␊
71687238
71697239
function getAugmentedNamespace(n) {␊
7240+
if (n.__esModule) return n;␊
71707241
var f = n.default;␊
71717242
if (typeof f == "function") {␊
71727243
var a = function a () {␊
@@ -7211,6 +7282,68 @@ Generated by [AVA](https://avajs.dev).
72117282
`,
72127283
}
72137284

7285+
## transform-mixed-modules-esmodule
7286+
7287+
> Snapshot 1
7288+
7289+
{
7290+
'main.js': `'use strict';␊
7291+
7292+
function getAugmentedNamespace(n) {␊
7293+
if (n.__esModule) return n;␊
7294+
var f = n.default;␊
7295+
if (typeof f == "function") {␊
7296+
var a = function a () {␊
7297+
if (this instanceof a) {␊
7298+
var args = [null];␊
7299+
args.push.apply(args, arguments);␊
7300+
var Ctor = Function.bind.apply(f, args);␊
7301+
return new Ctor();␊
7302+
}␊
7303+
return f.apply(this, arguments);␊
7304+
};␊
7305+
a.prototype = f.prototype;␊
7306+
} else a = {};␊
7307+
Object.defineProperty(a, '__esModule', {value: true});␊
7308+
Object.keys(n).forEach(function (k) {␊
7309+
var d = Object.getOwnPropertyDescriptor(n, k);␊
7310+
Object.defineProperty(a, k, d.get ? d : {␊
7311+
enumerable: true,␊
7312+
get: function () {␊
7313+
return n[k];␊
7314+
}␊
7315+
});␊
7316+
});␊
7317+
return a;␊
7318+
}␊
7319+
7320+
var otherExports = {};␊
7321+
var other$1 = {␊
7322+
get exports(){ return otherExports; },␊
7323+
set exports(v){ otherExports = v; },␊
7324+
};␊
7325+
7326+
Object.defineProperty(otherExports, '__esModule', {␊
7327+
value: true␊
7328+
});␊
7329+
other$1.exports = 'other';␊
7330+
7331+
const other = otherExports;␊
7332+
7333+
var dep$1 = /*#__PURE__*/Object.freeze({␊
7334+
__proto__: null,␊
7335+
other: other␊
7336+
});␊
7337+
7338+
var require$$0 = /*@__PURE__*/getAugmentedNamespace(dep$1);␊
7339+
7340+
const dep = require$$0;␊
7341+
7342+
t.is(other, 'other');␊
7343+
t.deepEqual(dep, { other: 'other' });␊
7344+
`,
7345+
}
7346+
72147347
## transpiled-esm-default
72157348

72167349
> Snapshot 1
165 Bytes
Binary file not shown.

packages/commonjs/test/snapshots/test.js.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Generated by [AVA](https://avajs.dev).
4242
}␊
4343
4444
function getAugmentedNamespace(n) {␊
45+
if (n.__esModule) return n;␊
4546
var f = n.default;␊
4647
if (typeof f == "function") {␊
4748
var a = function a () {␊
@@ -199,6 +200,7 @@ Generated by [AVA](https://avajs.dev).
199200
`'use strict';␊
200201
201202
function getAugmentedNamespace(n) {␊
203+
if (n.__esModule) return n;␊
202204
var f = n.default;␊
203205
if (typeof f == "function") {␊
204206
var a = function a () {␊
@@ -279,6 +281,7 @@ Generated by [AVA](https://avajs.dev).
279281
`'use strict';␊
280282
281283
function getAugmentedNamespace(n) {␊
284+
if (n.__esModule) return n;␊
282285
var f = n.default;␊
283286
if (typeof f == "function") {␊
284287
var a = function a () {␊
12 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)