diff --git a/packages/commonjs/src/proxies.js b/packages/commonjs/src/proxies.js index eb6ff9ea5..2930f9e5c 100644 --- a/packages/commonjs/src/proxies.js +++ b/packages/commonjs/src/proxies.js @@ -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});`; diff --git a/packages/commonjs/test/fixtures/function/esm-import-cjs-circular-dependency/_config.js b/packages/commonjs/test/fixtures/function/esm-import-cjs-circular-dependency/_config.js new file mode 100644 index 000000000..d2599e095 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/esm-import-cjs-circular-dependency/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'esm import cjs circular dependency', +}; diff --git a/packages/commonjs/test/fixtures/function/esm-import-cjs-circular-dependency/main.js b/packages/commonjs/test/fixtures/function/esm-import-cjs-circular-dependency/main.js new file mode 100644 index 000000000..28c5203b3 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/esm-import-cjs-circular-dependency/main.js @@ -0,0 +1,3 @@ +import cjs from 'cjs-circular'; + +t.is(cjs, 'foo'); diff --git a/packages/commonjs/test/node_modules/cjs-circular/circular.js b/packages/commonjs/test/node_modules/cjs-circular/circular.js new file mode 100644 index 000000000..119d8e448 --- /dev/null +++ b/packages/commonjs/test/node_modules/cjs-circular/circular.js @@ -0,0 +1,10 @@ +"use strict"; +exports.__esModule = true; +exports.getMain = void 0; +var _1 = require("."); +var name = 'circular'; +function getMain() { + return "".concat(_1["default"], "-circular"); +} +exports.getMain = getMain; +exports["default"] = name; diff --git a/packages/commonjs/test/node_modules/cjs-circular/index.js b/packages/commonjs/test/node_modules/cjs-circular/index.js new file mode 100644 index 000000000..832a35fa0 --- /dev/null +++ b/packages/commonjs/test/node_modules/cjs-circular/index.js @@ -0,0 +1,7 @@ +"use strict"; +exports.__esModule = true; +exports.main = void 0; +var circular_1 = require("./circular"); +var foo = 'foo'; +exports["default"] = foo; +exports.main = "".concat((0, circular_1.getMain)(), "-circular"); diff --git a/packages/commonjs/test/node_modules/cjs-circular/package.json b/packages/commonjs/test/node_modules/cjs-circular/package.json new file mode 100644 index 000000000..14ba84a16 --- /dev/null +++ b/packages/commonjs/test/node_modules/cjs-circular/package.json @@ -0,0 +1,11 @@ +{ + "name": "cjs-circular", + "private": true, + "version": "0.0.0", + "type": "commonjs", + "main": "./index.js", + "scripts": {}, + "devDependencies": { + "typescript": "~4.9.3" + } +} \ No newline at end of file diff --git a/packages/commonjs/test/snapshots/function.js.md b/packages/commonjs/test/snapshots/function.js.md index 2828b895e..28879bc83 100644 --- a/packages/commonjs/test/snapshots/function.js.md +++ b/packages/commonjs/test/snapshots/function.js.md @@ -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');␊ `, } @@ -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 @@ -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';␊ ␊ @@ -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 = {};␊ ␊ @@ -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;␊ `, } @@ -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;␊ `, } @@ -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;␊ `, } @@ -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 = {};␊ ␊ @@ -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;␊ `, } @@ -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;␊ @@ -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');␊ `, } @@ -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';␊ ␊ diff --git a/packages/commonjs/test/snapshots/function.js.snap b/packages/commonjs/test/snapshots/function.js.snap index 0380d4724..62bfca56f 100644 Binary files a/packages/commonjs/test/snapshots/function.js.snap and b/packages/commonjs/test/snapshots/function.js.snap differ