Skip to content

Commit

Permalink
fix(commonjs)!: check if defaultIsModuleExports is auto for getDefaul…
Browse files Browse the repository at this point in the history
…tExportFromCjs (#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 <>
  • Loading branch information
flyingbirdhub authored Dec 18, 2022
1 parent 2c2fe9b commit 4766d93
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/commonjs/src/proxies.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function getEsImportProxy(id, defaultIsModuleExports) {
`import { __require as ${requireModule} } from ${JSON.stringify(id)};\n` +
`var ${exportsName} = ${requireModule}();\n` +
`export { ${exportsName} as __moduleExports };`;
if (defaultIsModuleExports) {
if (defaultIsModuleExports === true) {
code += `\nexport { ${exportsName} as default };`;
} else {
code += `export default /*@__PURE__*/getDefaultExportFromCjs(${exportsName});`;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
description: 'esm import cjs circular dependency',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import cjs from 'cjs-circular';

t.is(cjs, 'foo');
10 changes: 10 additions & 0 deletions packages/commonjs/test/node_modules/cjs-circular/circular.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/commonjs/test/node_modules/cjs-circular/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions packages/commonjs/test/node_modules/cjs-circular/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

119 changes: 89 additions & 30 deletions packages/commonjs/test/snapshots/function.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -1654,19 +1654,24 @@ Generated by [AVA](https://avajs.dev).
{
'main.js': `'use strict';␊
var submodule;␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
var submodule$1;␊
var hasRequiredSubmodule;␊
function requireSubmodule () {␊
if (hasRequiredSubmodule) return submodule;␊
if (hasRequiredSubmodule) return submodule$1;␊
hasRequiredSubmodule = 1;␊
submodule = 'submodule';␊
return submodule;␊
submodule$1 = 'submodule';␊
return submodule$1;␊
}␊
var submoduleExports = requireSubmodule();␊
var submodule = /*@__PURE__*/getDefaultExportFromCjs(submoduleExports);␊
t.is(submoduleExports, 'submodule');␊
t.is(submodule, 'submodule');␊
`,
}

Expand Down Expand Up @@ -3745,6 +3750,23 @@ Generated by [AVA](https://avajs.dev).
`,
}

## esm-import-cjs-circular-dependency

> Snapshot 1
{
'main.js': `'use strict';␊
var cjs = require('cjs-circular');␊
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }␊
var cjs__default = /*#__PURE__*/_interopDefaultCompat(cjs);␊
t.is(cjs__default.default, 'foo');␊
`,
}

## esm-mixed-exports-function-default

> Snapshot 1
Expand Down Expand Up @@ -5826,15 +5848,22 @@ Generated by [AVA](https://avajs.dev).
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
exports.commonjsGlobal = commonjsGlobal;␊
exports.getDefaultExportFromCjs = getDefaultExportFromCjs;␊
`,
'main.js': `'use strict';␊
var main = require('./main2.js');␊
var _commonjsHelpers = require('./_virtual/_commonjsHelpers.js');␊
var main$1 = require('./main2.js');␊
var mainExports = main.__require();␊
var mainExports = main$1.__require();␊
var main = /*@__PURE__*/_commonjsHelpers.getDefaultExportFromCjs(mainExports);␊
module.exports = mainExports;␊
module.exports = main;␊
`,
'main2.js': `'use strict';␊
Expand Down Expand Up @@ -6490,7 +6519,11 @@ Generated by [AVA](https://avajs.dev).
{
'main.js': `'use strict';␊
var main = {};␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
var main$1 = {};␊
var other = {};␊
Expand All @@ -6506,16 +6539,17 @@ Generated by [AVA](https://avajs.dev).
var hasRequiredMain;␊
function requireMain () {␊
if (hasRequiredMain) return main;␊
if (hasRequiredMain) return main$1;␊
hasRequiredMain = 1;␊
main.foo = 'foo';␊
main$1.foo = 'foo';␊
t.is(requireOther().foo, 'foo');␊
return main;␊
return main$1;␊
}␊
var mainExports = requireMain();␊
var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);␊
module.exports = mainExports;␊
module.exports = main;␊
`,
}

Expand Down Expand Up @@ -6708,20 +6742,25 @@ Generated by [AVA](https://avajs.dev).
{
'main.js': `'use strict';␊
var main = {};␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
var main$1 = {};␊
var hasRequiredMain;␊
function requireMain () {␊
if (hasRequiredMain) return main;␊
if (hasRequiredMain) return main$1;␊
hasRequiredMain = 1;␊
main.foo = 'foo';␊
return main;␊
main$1.foo = 'foo';␊
return main$1;␊
}␊
var mainExports = requireMain();␊
var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);␊
module.exports = mainExports;␊
module.exports = main;␊
`,
}

Expand Down Expand Up @@ -6934,20 +6973,25 @@ Generated by [AVA](https://avajs.dev).
var require$$0__default = /*#__PURE__*/_interopDefaultCompat(require$$0);␊
var main = {};␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
var main$1 = {};␊
var hasRequiredMain;␊
function requireMain () {␊
if (hasRequiredMain) return main;␊
if (hasRequiredMain) return main$1;␊
hasRequiredMain = 1;␊
t.is(require$$0__default.default.message, 'it works');␊
return main;␊
return main$1;␊
}␊
var mainExports = requireMain();␊
var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);␊
module.exports = mainExports;␊
module.exports = main;␊
`,
}

Expand All @@ -6960,7 +7004,11 @@ Generated by [AVA](https://avajs.dev).
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
var main = {};␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
var main$1 = {};␊
var error = {};␊
Expand All @@ -6975,18 +7023,19 @@ Generated by [AVA](https://avajs.dev).
var hasRequiredMain;␊
function requireMain () {␊
if (hasRequiredMain) return main;␊
if (hasRequiredMain) return main$1;␊
hasRequiredMain = 1;␊
commonjsGlobal.null = 0;␊
// eslint-disable-next-line global-require␊
t.is(commonjsGlobal.null && requireError(), 0);␊
return main;␊
return main$1;␊
}␊
var mainExports = requireMain();␊
var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);␊
module.exports = mainExports;␊
module.exports = main;␊
`,
}

Expand Down Expand Up @@ -7023,6 +7072,10 @@ Generated by [AVA](https://avajs.dev).
{
'main.js': `'use strict';␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
var require$1 = {};␊
var hasRequiredRequire;␊
Expand All @@ -7046,10 +7099,11 @@ Generated by [AVA](https://avajs.dev).
}␊
var _importExports = require_import();␊
var esm = /*@__PURE__*/getDefaultExportFromCjs(_importExports);␊
const cjs = requireRequire();␊
t.is(_importExports.foo, 'foo');␊
t.is(esm.foo, 'foo');␊
t.is(cjs.foo, 'foo');␊
`,
}
Expand All @@ -7076,11 +7130,16 @@ Generated by [AVA](https://avajs.dev).
`,
'main.js': `'use strict';␊
var main = require('./generated-main.js');␊
var main$1 = require('./generated-main.js');␊
var mainExports = main.requireMain();␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
module.exports = mainExports;␊
var mainExports = main$1.requireMain();␊
var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);␊
module.exports = main;␊
`,
'other.js': `'use strict';␊
Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.

0 comments on commit 4766d93

Please sign in to comment.