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): Support __esModule packages with a default export #465

Merged
merged 2 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions packages/commonjs/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ export const HELPERS_ID = '\0commonjsHelpers.js';
export const HELPERS = `
export var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};

export function unwrapExports (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}

export function createCommonjsModule(fn, basedir, module) {
return module = {
path: basedir,
Expand Down
4 changes: 1 addition & 3 deletions packages/commonjs/src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,9 +642,7 @@ export function transformCommonjs(
}
}

const defaultExport = /__esModule/.test(code)
? `export default /*@__PURE__*/${HELPERS_NAME}.unwrapExports(${moduleName});`
: `export default ${moduleName};`;
const defaultExport = `export default ${moduleName};`;

const named = namedExportDeclarations
.filter((x) => x.name !== 'default' || !hasDefaultExport)
Expand Down
96 changes: 7 additions & 89 deletions packages/commonjs/test/snapshots/function.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ The actual snapshot is saved in `function.js.snap`.

Generated by [AVA](https://ava.li).

## __esModule
## \_\_esModule

> Snapshot 1

{
'main.js': `'use strict';␊
function unwrapExports (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
function createCommonjsModule(fn, basedir, module) {␊
return module = {␊
path: basedir,␊
Expand All @@ -35,10 +31,8 @@ Generated by [AVA](https://ava.li).
exports.answer = 42;␊
});␊
var answer$1 = /*@__PURE__*/unwrapExports(answer);␊
var x = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), answer$1, {␊
'default': answer$1,␊
var x = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), answer, {␊
'default': answer,␊
__moduleExports: answer␊
}));␊
Expand Down Expand Up @@ -263,10 +257,6 @@ Generated by [AVA](https://ava.li).
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
function unwrapExports (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
function createCommonjsModule(fn, basedir, module) {␊
return module = {␊
path: basedir,␊
Expand Down Expand Up @@ -420,7 +410,6 @@ Generated by [AVA](https://ava.li).
var _commonjsHelpers = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
commonjsGlobal: commonjsGlobal,␊
unwrapExports: unwrapExports,␊
createCommonjsModule: createCommonjsModule,␊
getCjsExportFromNamespace: getCjsExportFromNamespace,␊
commonjsRegister: commonjsRegister,␊
Expand Down Expand Up @@ -466,10 +455,6 @@ Generated by [AVA](https://ava.li).
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
function unwrapExports (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
function createCommonjsModule(fn, basedir, module) {␊
return module = {␊
path: basedir,␊
Expand Down Expand Up @@ -623,7 +608,6 @@ Generated by [AVA](https://ava.li).
var _commonjsHelpers = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
commonjsGlobal: commonjsGlobal,␊
unwrapExports: unwrapExports,␊
createCommonjsModule: createCommonjsModule,␊
getCjsExportFromNamespace: getCjsExportFromNamespace,␊
commonjsRegister: commonjsRegister,␊
Expand Down Expand Up @@ -665,10 +649,6 @@ Generated by [AVA](https://ava.li).
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
function unwrapExports (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
function createCommonjsModule(fn, basedir, module) {␊
return module = {␊
path: basedir,␊
Expand Down Expand Up @@ -822,7 +802,6 @@ Generated by [AVA](https://ava.li).
var _commonjsHelpers = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
commonjsGlobal: commonjsGlobal,␊
unwrapExports: unwrapExports,␊
createCommonjsModule: createCommonjsModule,␊
getCjsExportFromNamespace: getCjsExportFromNamespace,␊
commonjsRegister: commonjsRegister,␊
Expand Down Expand Up @@ -883,10 +862,6 @@ Generated by [AVA](https://ava.li).
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
function unwrapExports (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
function createCommonjsModule(fn, basedir, module) {␊
return module = {␊
path: basedir,␊
Expand Down Expand Up @@ -1040,7 +1015,6 @@ Generated by [AVA](https://ava.li).
var _commonjsHelpers = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
commonjsGlobal: commonjsGlobal,␊
unwrapExports: unwrapExports,␊
createCommonjsModule: createCommonjsModule,␊
getCjsExportFromNamespace: getCjsExportFromNamespace,␊
commonjsRegister: commonjsRegister,␊
Expand All @@ -1067,14 +1041,10 @@ Generated by [AVA](https://ava.li).
> Snapshot 1

{
'lib2-9224c76d.js': `'use strict';␊
'lib2-f863e9df.js': `'use strict';␊
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
function unwrapExports (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
function createCommonjsModule(fn, basedir, module) {␊
return module = {␊
path: basedir,␊
Expand Down Expand Up @@ -1228,7 +1198,6 @@ Generated by [AVA](https://ava.li).
var _commonjsHelpers = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
commonjsGlobal: commonjsGlobal,␊
unwrapExports: unwrapExports,␊
createCommonjsModule: createCommonjsModule,␊
getCjsExportFromNamespace: getCjsExportFromNamespace,␊
commonjsRegister: commonjsRegister,␊
Expand Down Expand Up @@ -1262,7 +1231,7 @@ Generated by [AVA](https://ava.li).
`,
'main.js': `'use strict';␊
var lib2 = require('./lib2-9224c76d.js');␊
var lib2 = require('./lib2-f863e9df.js');␊
/* eslint-disable import/no-dynamic-require, global-require */␊
Expand All @@ -1279,7 +1248,7 @@ Generated by [AVA](https://ava.li).
`,
'main2.js': `'use strict';␊
require('./lib2-9224c76d.js');␊
require('./lib2-f863e9df.js');␊
`,
}
Expand All @@ -1293,10 +1262,6 @@ Generated by [AVA](https://ava.li).
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
function unwrapExports (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
function createCommonjsModule(fn, basedir, module) {␊
return module = {␊
path: basedir,␊
Expand Down Expand Up @@ -1450,7 +1415,6 @@ Generated by [AVA](https://ava.li).
var _commonjsHelpers = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
commonjsGlobal: commonjsGlobal,␊
unwrapExports: unwrapExports,␊
createCommonjsModule: createCommonjsModule,␊
getCjsExportFromNamespace: getCjsExportFromNamespace,␊
commonjsRegister: commonjsRegister,␊
Expand Down Expand Up @@ -1483,10 +1447,6 @@ Generated by [AVA](https://ava.li).
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
function unwrapExports (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
function createCommonjsModule(fn, basedir, module) {␊
return module = {␊
path: basedir,␊
Expand Down Expand Up @@ -1640,7 +1600,6 @@ Generated by [AVA](https://ava.li).
var _commonjsHelpers = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
commonjsGlobal: commonjsGlobal,␊
unwrapExports: unwrapExports,␊
createCommonjsModule: createCommonjsModule,␊
getCjsExportFromNamespace: getCjsExportFromNamespace,␊
commonjsRegister: commonjsRegister,␊
Expand Down Expand Up @@ -1679,10 +1638,6 @@ Generated by [AVA](https://ava.li).
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
function unwrapExports (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
function createCommonjsModule(fn, basedir, module) {␊
return module = {␊
path: basedir,␊
Expand Down Expand Up @@ -1836,7 +1791,6 @@ Generated by [AVA](https://ava.li).
var _commonjsHelpers = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
commonjsGlobal: commonjsGlobal,␊
unwrapExports: unwrapExports,␊
createCommonjsModule: createCommonjsModule,␊
getCjsExportFromNamespace: getCjsExportFromNamespace,␊
commonjsRegister: commonjsRegister,␊
Expand Down Expand Up @@ -1888,10 +1842,6 @@ Generated by [AVA](https://ava.li).
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
function unwrapExports (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
function createCommonjsModule(fn, basedir, module) {␊
return module = {␊
path: basedir,␊
Expand Down Expand Up @@ -2045,7 +1995,6 @@ Generated by [AVA](https://ava.li).
var _commonjsHelpers = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
commonjsGlobal: commonjsGlobal,␊
unwrapExports: unwrapExports,␊
createCommonjsModule: createCommonjsModule,␊
getCjsExportFromNamespace: getCjsExportFromNamespace,␊
commonjsRegister: commonjsRegister,␊
Expand Down Expand Up @@ -2095,10 +2044,6 @@ Generated by [AVA](https://ava.li).
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
function unwrapExports (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
function createCommonjsModule(fn, basedir, module) {␊
return module = {␊
path: basedir,␊
Expand Down Expand Up @@ -2252,7 +2197,6 @@ Generated by [AVA](https://ava.li).
var _commonjsHelpers = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
commonjsGlobal: commonjsGlobal,␊
unwrapExports: unwrapExports,␊
createCommonjsModule: createCommonjsModule,␊
getCjsExportFromNamespace: getCjsExportFromNamespace,␊
commonjsRegister: commonjsRegister,␊
Expand Down Expand Up @@ -2296,10 +2240,6 @@ Generated by [AVA](https://ava.li).
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
function unwrapExports (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
function createCommonjsModule(fn, basedir, module) {␊
return module = {␊
path: basedir,␊
Expand Down Expand Up @@ -2453,7 +2393,6 @@ Generated by [AVA](https://ava.li).
var _commonjsHelpers = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
commonjsGlobal: commonjsGlobal,␊
unwrapExports: unwrapExports,␊
createCommonjsModule: createCommonjsModule,␊
getCjsExportFromNamespace: getCjsExportFromNamespace,␊
commonjsRegister: commonjsRegister,␊
Expand Down Expand Up @@ -2514,10 +2453,6 @@ Generated by [AVA](https://ava.li).
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
function unwrapExports (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
function createCommonjsModule(fn, basedir, module) {␊
return module = {␊
path: basedir,␊
Expand Down Expand Up @@ -2671,7 +2606,6 @@ Generated by [AVA](https://ava.li).
var _commonjsHelpers = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
commonjsGlobal: commonjsGlobal,␊
unwrapExports: unwrapExports,␊
createCommonjsModule: createCommonjsModule,␊
getCjsExportFromNamespace: getCjsExportFromNamespace,␊
commonjsRegister: commonjsRegister,␊
Expand Down Expand Up @@ -2718,10 +2652,6 @@ Generated by [AVA](https://ava.li).
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
function unwrapExports (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
function createCommonjsModule(fn, basedir, module) {␊
return module = {␊
path: basedir,␊
Expand Down Expand Up @@ -2875,7 +2805,6 @@ Generated by [AVA](https://ava.li).
var _commonjsHelpers = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
commonjsGlobal: commonjsGlobal,␊
unwrapExports: unwrapExports,␊
createCommonjsModule: createCommonjsModule,␊
getCjsExportFromNamespace: getCjsExportFromNamespace,␊
commonjsRegister: commonjsRegister,␊
Expand Down Expand Up @@ -2914,10 +2843,6 @@ Generated by [AVA](https://ava.li).
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
function unwrapExports (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
function createCommonjsModule(fn, basedir, module) {␊
return module = {␊
path: basedir,␊
Expand Down Expand Up @@ -3071,7 +2996,6 @@ Generated by [AVA](https://ava.li).
var _commonjsHelpers = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
commonjsGlobal: commonjsGlobal,␊
unwrapExports: unwrapExports,␊
createCommonjsModule: createCommonjsModule,␊
getCjsExportFromNamespace: getCjsExportFromNamespace,␊
commonjsRegister: commonjsRegister,␊
Expand Down Expand Up @@ -3551,10 +3475,6 @@ Generated by [AVA](https://ava.li).
{
'main.js': `'use strict';␊
function unwrapExports (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
function createCommonjsModule(fn, basedir, module) {␊
return module = {␊
path: basedir,␊
Expand Down Expand Up @@ -3587,9 +3507,7 @@ Generated by [AVA](https://ava.li).
exports.Bar = commonjsBar.default;␊
});␊
var commonjsFoo$1 = /*@__PURE__*/unwrapExports(commonjsFoo);␊
t.is(new commonjsFoo$1.Bar().x, 42);␊
t.is(new commonjsFoo.Bar().x, 42);␊
`,
}

Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.
4 changes: 2 additions & 2 deletions packages/commonjs/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ test('handles transpiled CommonJS modules', async (t) => {
const fn = new Function('module', 'exports', code);
fn(module, module.exports);

t.is(module.exports, 'foobar', code);
t.deepEqual(module.exports, { __esModule: true, default: 'foobar' }, code);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

considering this test as broken as written, and fixed by this PR

});

test('handles successive builds', async (t) => {
Expand All @@ -177,7 +177,7 @@ test('handles successive builds', async (t) => {
const fn = new Function('module', 'exports', code);
fn(module, module.exports);

t.is(module.exports, 'foobar', code);
t.deepEqual(module.exports, { __esModule: true, default: 'foobar' }, code);
});

test.serial('handles symlinked node_modules with preserveSymlinks: false', (t) => {
Expand Down