Skip to content

Commit f873993

Browse files
authored
fix(commonjs): fix error when bundle contains require() of module with falsy __esModule export (#1850)
fix crash Co-authored-by: easrng <[email protected]>
1 parent 0293e8f commit f873993

File tree

8 files changed

+99
-20
lines changed

8 files changed

+99
-20
lines changed

packages/commonjs/src/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function getDefaultExportFromNamespaceIfNotNamed (n) {
4242
}
4343
4444
export function getAugmentedNamespace(n) {
45-
if (n.__esModule) return n;
45+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;
4646
var f = n.default;
4747
if (typeof f == "function") {
4848
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 falsy __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 let __esModule;
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, undefined);

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

Lines changed: 85 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4210,7 +4210,7 @@ Generated by [AVA](https://avajs.dev).
42104210
}␊
42114211
42124212
function getAugmentedNamespace(n) {␊
4213-
if (n.__esModule) return n;␊
4213+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;␊
42144214
var f = n.default;␊
42154215
if (typeof f == "function") {␊
42164216
var a = function a () {␊
@@ -4308,7 +4308,7 @@ Generated by [AVA](https://avajs.dev).
43084308
}␊
43094309
43104310
function getAugmentedNamespace(n) {␊
4311-
if (n.__esModule) return n;␊
4311+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;␊
43124312
var f = n.default;␊
43134313
if (typeof f == "function") {␊
43144314
var a = function a () {␊
@@ -4400,7 +4400,7 @@ Generated by [AVA](https://avajs.dev).
44004400
}␊
44014401
44024402
function getAugmentedNamespace(n) {␊
4403-
if (n.__esModule) return n;␊
4403+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;␊
44044404
var f = n.default;␊
44054405
if (typeof f == "function") {␊
44064406
var a = function a () {␊
@@ -4536,7 +4536,7 @@ Generated by [AVA](https://avajs.dev).
45364536
}␊
45374537
45384538
function getAugmentedNamespace(n) {␊
4539-
if (n.__esModule) return n;␊
4539+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;␊
45404540
var f = n.default;␊
45414541
if (typeof f == "function") {␊
45424542
var a = function a () {␊
@@ -4640,7 +4640,7 @@ Generated by [AVA](https://avajs.dev).
46404640
}␊
46414641
46424642
function getAugmentedNamespace(n) {␊
4643-
if (n.__esModule) return n;␊
4643+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;␊
46444644
var f = n.default;␊
46454645
if (typeof f == "function") {␊
46464646
var a = function a () {␊
@@ -4697,6 +4697,75 @@ Generated by [AVA](https://avajs.dev).
46974697
`,
46984698
}
46994699

4700+
## esm-with-falsy-esmodule
4701+
4702+
> Snapshot 1
4703+
4704+
{
4705+
'main.js': `'use strict';␊
4706+
4707+
function getDefaultExportFromCjs (x) {␊
4708+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
4709+
}␊
4710+
4711+
function getAugmentedNamespace(n) {␊
4712+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;␊
4713+
var f = n.default;␊
4714+
if (typeof f == "function") {␊
4715+
var a = function a () {␊
4716+
if (this instanceof a) {␊
4717+
return Reflect.construct(f, arguments, this.constructor);␊
4718+
}␊
4719+
return f.apply(this, arguments);␊
4720+
};␊
4721+
a.prototype = f.prototype;␊
4722+
} else a = {};␊
4723+
Object.defineProperty(a, '__esModule', {value: true});␊
4724+
Object.keys(n).forEach(function (k) {␊
4725+
var d = Object.getOwnPropertyDescriptor(n, k);␊
4726+
Object.defineProperty(a, k, d.get ? d : {␊
4727+
enumerable: true,␊
4728+
get: function () {␊
4729+
return n[k];␊
4730+
}␊
4731+
});␊
4732+
});␊
4733+
return a;␊
4734+
}␊
4735+
4736+
var main$1 = {};␊
4737+
4738+
const foo = 'foo';␊
4739+
4740+
let __esModule;␊
4741+
4742+
var esm = /*#__PURE__*/Object.freeze({␊
4743+
__proto__: null,␊
4744+
__esModule: __esModule,␊
4745+
foo: foo␊
4746+
});␊
4747+
4748+
var require$$0 = /*@__PURE__*/getAugmentedNamespace(esm);␊
4749+
4750+
var hasRequiredMain;␊
4751+
4752+
function requireMain () {␊
4753+
if (hasRequiredMain) return main$1;␊
4754+
hasRequiredMain = 1;␊
4755+
const esm = require$$0;␊
4756+
4757+
t.is(esm.foo, 'foo');␊
4758+
t.is(esm.__esModule, undefined);␊
4759+
return main$1;␊
4760+
}␊
4761+
4762+
var mainExports = requireMain();␊
4763+
var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);␊
4764+
4765+
module.exports = main;␊
4766+
`,
4767+
}
4768+
47004769
## export-default-from
47014770

47024771
> Snapshot 1
@@ -4909,7 +4978,7 @@ Generated by [AVA](https://avajs.dev).
49094978
}␊
49104979
49114980
function getAugmentedNamespace(n) {␊
4912-
if (n.__esModule) return n;␊
4981+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;␊
49134982
var f = n.default;␊
49144983
if (typeof f == "function") {␊
49154984
var a = function a () {␊
@@ -5198,7 +5267,7 @@ Generated by [AVA](https://avajs.dev).
51985267
}␊
51995268
52005269
function getAugmentedNamespace(n) {␊
5201-
if (n.__esModule) return n;␊
5270+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;␊
52025271
var f = n.default;␊
52035272
if (typeof f == "function") {␊
52045273
var a = function a () {␊
@@ -5321,7 +5390,7 @@ Generated by [AVA](https://avajs.dev).
53215390
}␊
53225391
53235392
function getAugmentedNamespace(n) {␊
5324-
if (n.__esModule) return n;␊
5393+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;␊
53255394
var f = n.default;␊
53265395
if (typeof f == "function") {␊
53275396
var a = function a () {␊
@@ -5551,7 +5620,7 @@ Generated by [AVA](https://avajs.dev).
55515620
}␊
55525621
55535622
function getAugmentedNamespace(n) {␊
5554-
if (n.__esModule) return n;␊
5623+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;␊
55555624
var f = n.default;␊
55565625
if (typeof f == "function") {␊
55575626
var a = function a () {␊
@@ -5901,7 +5970,7 @@ Generated by [AVA](https://avajs.dev).
59015970
}␊
59025971
59035972
function getAugmentedNamespace(n) {␊
5904-
if (n.__esModule) return n;␊
5973+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;␊
59055974
var f = n.default;␊
59065975
if (typeof f == "function") {␊
59075976
var a = function a () {␊
@@ -6016,7 +6085,7 @@ Generated by [AVA](https://avajs.dev).
60166085
}␊
60176086
60186087
function getAugmentedNamespace(n) {␊
6019-
if (n.__esModule) return n;␊
6088+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;␊
60206089
var f = n.default;␊
60216090
if (typeof f == "function") {␊
60226091
var a = function a () {␊
@@ -7466,7 +7535,7 @@ Generated by [AVA](https://avajs.dev).
74667535
}␊
74677536
74687537
function getAugmentedNamespace(n) {␊
7469-
if (n.__esModule) return n;␊
7538+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;␊
74707539
var f = n.default;␊
74717540
if (typeof f == "function") {␊
74727541
var a = function a () {␊
@@ -8212,7 +8281,7 @@ Generated by [AVA](https://avajs.dev).
82128281
}␊
82138282
82148283
function getAugmentedNamespace(n) {␊
8215-
if (n.__esModule) return n;␊
8284+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;␊
82168285
var f = n.default;␊
82178286
if (typeof f == "function") {␊
82188287
var a = function a () {␊
@@ -8275,7 +8344,7 @@ Generated by [AVA](https://avajs.dev).
82758344
}␊
82768345
82778346
function getAugmentedNamespace(n) {␊
8278-
if (n.__esModule) return n;␊
8347+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;␊
82798348
var f = n.default;␊
82808349
if (typeof f == "function") {␊
82818350
var a = function a () {␊
@@ -9521,7 +9590,7 @@ Generated by [AVA](https://avajs.dev).
95219590
'main.js': `'use strict';␊
95229591
95239592
function getAugmentedNamespace(n) {␊
9524-
if (n.__esModule) return n;␊
9593+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;␊
95259594
var f = n.default;␊
95269595
if (typeof f == "function") {␊
95279596
var a = function a () {␊
@@ -9579,7 +9648,7 @@ Generated by [AVA](https://avajs.dev).
95799648
'main.js': `'use strict';␊
95809649
95819650
function getAugmentedNamespace(n) {␊
9582-
if (n.__esModule) return n;␊
9651+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;␊
95839652
var f = n.default;␊
95849653
if (typeof f == "function") {␊
95859654
var a = function a () {␊
19 Bytes
Binary file not shown.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Generated by [AVA](https://avajs.dev).
4343
}␊
4444
4545
function getAugmentedNamespace(n) {␊
46-
if (n.__esModule) return n;␊
46+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;␊
4747
var f = n.default;␊
4848
if (typeof f == "function") {␊
4949
var a = function a () {␊
@@ -252,7 +252,7 @@ Generated by [AVA](https://avajs.dev).
252252
}␊
253253
254254
function getAugmentedNamespace(n) {␊
255-
if (n.__esModule) return n;␊
255+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;␊
256256
var f = n.default;␊
257257
if (typeof f == "function") {␊
258258
var a = function a () {␊
@@ -378,7 +378,7 @@ Generated by [AVA](https://avajs.dev).
378378
`'use strict';␊
379379
380380
function getAugmentedNamespace(n) {␊
381-
if (n.__esModule) return n;␊
381+
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;␊
382382
var f = n.default;␊
383383
if (typeof f == "function") {␊
384384
var a = function a () {␊
5 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)