Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(commonjs)!: check if defaultIsModuleExports is auto for getDefaultExportFromCjs #1358

Merged
merged 5 commits into from Dec 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/commonjs/src/proxies.js
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
@@ -0,0 +1,3 @@
module.exports = {
description: 'esm import cjs circular dependency',
};
@@ -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
Expand Up @@ -1638,19 +1638,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 @@ -3718,6 +3723,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 @@ -5690,15 +5712,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 @@ -6332,7 +6361,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 @@ -6348,16 +6381,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 @@ -6550,20 +6584,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 @@ -6764,20 +6803,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 @@ -6790,7 +6834,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 @@ -6805,18 +6853,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 @@ -6853,6 +6902,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 @@ -6876,10 +6929,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 @@ -6906,11 +6960,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.