Skip to content

Commit 4766d93

Browse files
fix(commonjs)!: check if defaultIsModuleExports is auto for getDefaultExportFromCjs (#1358)
* fix: getEsImportProxy should check whether defaultIsModuleExports option is auto, if defaultIsModuleExports option is auto, the export may should be wrapped with getDefaultExportFromCjs * fix: add test cases when import a module from a cjs with circular Co-authored-by: flyingbirdhub <>
1 parent 2c2fe9b commit 4766d93

File tree

8 files changed

+124
-31
lines changed

8 files changed

+124
-31
lines changed

packages/commonjs/src/proxies.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function getEsImportProxy(id, defaultIsModuleExports) {
6666
`import { __require as ${requireModule} } from ${JSON.stringify(id)};\n` +
6767
`var ${exportsName} = ${requireModule}();\n` +
6868
`export { ${exportsName} as __moduleExports };`;
69-
if (defaultIsModuleExports) {
69+
if (defaultIsModuleExports === true) {
7070
code += `\nexport { ${exportsName} as default };`;
7171
} else {
7272
code += `export default /*@__PURE__*/getDefaultExportFromCjs(${exportsName});`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
description: 'esm import cjs circular dependency',
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import cjs from 'cjs-circular';
2+
3+
t.is(cjs, 'foo');

packages/commonjs/test/node_modules/cjs-circular/circular.js

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/commonjs/test/node_modules/cjs-circular/index.js

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/commonjs/test/node_modules/cjs-circular/package.json

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+89-30
Original file line numberDiff line numberDiff line change
@@ -1654,19 +1654,24 @@ Generated by [AVA](https://avajs.dev).
16541654
{
16551655
'main.js': `'use strict';␊
16561656
1657-
var submodule;␊
1657+
function getDefaultExportFromCjs (x) {␊
1658+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
1659+
}␊
1660+
1661+
var submodule$1;␊
16581662
var hasRequiredSubmodule;␊
16591663
16601664
function requireSubmodule () {␊
1661-
if (hasRequiredSubmodule) return submodule;␊
1665+
if (hasRequiredSubmodule) return submodule$1;␊
16621666
hasRequiredSubmodule = 1;␊
1663-
submodule = 'submodule';␊
1664-
return submodule;␊
1667+
submodule$1 = 'submodule';␊
1668+
return submodule$1;␊
16651669
}␊
16661670
16671671
var submoduleExports = requireSubmodule();␊
1672+
var submodule = /*@__PURE__*/getDefaultExportFromCjs(submoduleExports);␊
16681673
1669-
t.is(submoduleExports, 'submodule');␊
1674+
t.is(submodule, 'submodule');␊
16701675
`,
16711676
}
16721677

@@ -3745,6 +3750,23 @@ Generated by [AVA](https://avajs.dev).
37453750
`,
37463751
}
37473752

3753+
## esm-import-cjs-circular-dependency
3754+
3755+
> Snapshot 1
3756+
3757+
{
3758+
'main.js': `'use strict';␊
3759+
3760+
var cjs = require('cjs-circular');␊
3761+
3762+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }␊
3763+
3764+
var cjs__default = /*#__PURE__*/_interopDefaultCompat(cjs);␊
3765+
3766+
t.is(cjs__default.default, 'foo');␊
3767+
`,
3768+
}
3769+
37483770
## esm-mixed-exports-function-default
37493771

37503772
> Snapshot 1
@@ -5826,15 +5848,22 @@ Generated by [AVA](https://avajs.dev).
58265848
58275849
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
58285850
5851+
function getDefaultExportFromCjs (x) {␊
5852+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
5853+
}␊
5854+
58295855
exports.commonjsGlobal = commonjsGlobal;␊
5856+
exports.getDefaultExportFromCjs = getDefaultExportFromCjs;␊
58305857
`,
58315858
'main.js': `'use strict';␊
58325859
5833-
var main = require('./main2.js');␊
5860+
var _commonjsHelpers = require('./_virtual/_commonjsHelpers.js');␊
5861+
var main$1 = require('./main2.js');␊
58345862
5835-
var mainExports = main.__require();␊
5863+
var mainExports = main$1.__require();␊
5864+
var main = /*@__PURE__*/_commonjsHelpers.getDefaultExportFromCjs(mainExports);␊
58365865
5837-
module.exports = mainExports;␊
5866+
module.exports = main;␊
58385867
`,
58395868
'main2.js': `'use strict';␊
58405869
@@ -6490,7 +6519,11 @@ Generated by [AVA](https://avajs.dev).
64906519
{
64916520
'main.js': `'use strict';␊
64926521
6493-
var main = {};␊
6522+
function getDefaultExportFromCjs (x) {␊
6523+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
6524+
}␊
6525+
6526+
var main$1 = {};␊
64946527
64956528
var other = {};␊
64966529
@@ -6506,16 +6539,17 @@ Generated by [AVA](https://avajs.dev).
65066539
var hasRequiredMain;␊
65076540
65086541
function requireMain () {␊
6509-
if (hasRequiredMain) return main;␊
6542+
if (hasRequiredMain) return main$1;␊
65106543
hasRequiredMain = 1;␊
6511-
main.foo = 'foo';␊
6544+
main$1.foo = 'foo';␊
65126545
t.is(requireOther().foo, 'foo');␊
6513-
return main;␊
6546+
return main$1;␊
65146547
}␊
65156548
65166549
var mainExports = requireMain();␊
6550+
var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);␊
65176551
6518-
module.exports = mainExports;␊
6552+
module.exports = main;␊
65196553
`,
65206554
}
65216555

@@ -6708,20 +6742,25 @@ Generated by [AVA](https://avajs.dev).
67086742
{
67096743
'main.js': `'use strict';␊
67106744
6711-
var main = {};␊
6745+
function getDefaultExportFromCjs (x) {␊
6746+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
6747+
}␊
6748+
6749+
var main$1 = {};␊
67126750
67136751
var hasRequiredMain;␊
67146752
67156753
function requireMain () {␊
6716-
if (hasRequiredMain) return main;␊
6754+
if (hasRequiredMain) return main$1;␊
67176755
hasRequiredMain = 1;␊
6718-
main.foo = 'foo';␊
6719-
return main;␊
6756+
main$1.foo = 'foo';␊
6757+
return main$1;␊
67206758
}␊
67216759
67226760
var mainExports = requireMain();␊
6761+
var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);␊
67236762
6724-
module.exports = mainExports;␊
6763+
module.exports = main;␊
67256764
`,
67266765
}
67276766

@@ -6934,20 +6973,25 @@ Generated by [AVA](https://avajs.dev).
69346973
69356974
var require$$0__default = /*#__PURE__*/_interopDefaultCompat(require$$0);␊
69366975
6937-
var main = {};␊
6976+
function getDefaultExportFromCjs (x) {␊
6977+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
6978+
}␊
6979+
6980+
var main$1 = {};␊
69386981
69396982
var hasRequiredMain;␊
69406983
69416984
function requireMain () {␊
6942-
if (hasRequiredMain) return main;␊
6985+
if (hasRequiredMain) return main$1;␊
69436986
hasRequiredMain = 1;␊
69446987
t.is(require$$0__default.default.message, 'it works');␊
6945-
return main;␊
6988+
return main$1;␊
69466989
}␊
69476990
69486991
var mainExports = requireMain();␊
6992+
var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);␊
69496993
6950-
module.exports = mainExports;␊
6994+
module.exports = main;␊
69516995
`,
69526996
}
69536997

@@ -6960,7 +7004,11 @@ Generated by [AVA](https://avajs.dev).
69607004
69617005
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
69627006
6963-
var main = {};␊
7007+
function getDefaultExportFromCjs (x) {␊
7008+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
7009+
}␊
7010+
7011+
var main$1 = {};␊
69647012
69657013
var error = {};␊
69667014
@@ -6975,18 +7023,19 @@ Generated by [AVA](https://avajs.dev).
69757023
var hasRequiredMain;␊
69767024
69777025
function requireMain () {␊
6978-
if (hasRequiredMain) return main;␊
7026+
if (hasRequiredMain) return main$1;␊
69797027
hasRequiredMain = 1;␊
69807028
commonjsGlobal.null = 0;␊
69817029
69827030
// eslint-disable-next-line global-require␊
69837031
t.is(commonjsGlobal.null && requireError(), 0);␊
6984-
return main;␊
7032+
return main$1;␊
69857033
}␊
69867034
69877035
var mainExports = requireMain();␊
7036+
var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);␊
69887037
6989-
module.exports = mainExports;␊
7038+
module.exports = main;␊
69907039
`,
69917040
}
69927041

@@ -7023,6 +7072,10 @@ Generated by [AVA](https://avajs.dev).
70237072
{
70247073
'main.js': `'use strict';␊
70257074
7075+
function getDefaultExportFromCjs (x) {␊
7076+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
7077+
}␊
7078+
70267079
var require$1 = {};␊
70277080
70287081
var hasRequiredRequire;␊
@@ -7046,10 +7099,11 @@ Generated by [AVA](https://avajs.dev).
70467099
}␊
70477100
70487101
var _importExports = require_import();␊
7102+
var esm = /*@__PURE__*/getDefaultExportFromCjs(_importExports);␊
70497103
70507104
const cjs = requireRequire();␊
70517105
7052-
t.is(_importExports.foo, 'foo');␊
7106+
t.is(esm.foo, 'foo');␊
70537107
t.is(cjs.foo, 'foo');␊
70547108
`,
70557109
}
@@ -7076,11 +7130,16 @@ Generated by [AVA](https://avajs.dev).
70767130
`,
70777131
'main.js': `'use strict';␊
70787132
7079-
var main = require('./generated-main.js');␊
7133+
var main$1 = require('./generated-main.js');␊
70807134
7081-
var mainExports = main.requireMain();␊
7135+
function getDefaultExportFromCjs (x) {␊
7136+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
7137+
}␊
70827138
7083-
module.exports = mainExports;␊
7139+
var mainExports = main$1.requireMain();␊
7140+
var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);␊
7141+
7142+
module.exports = main;␊
70847143
`,
70857144
'other.js': `'use strict';␊
70867145
192 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)