Skip to content

Commit

Permalink
fix(commonjs): Support __esModule packages with a default export
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott committed Jun 20, 2020
1 parent d906efa commit 92166de
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/commonjs/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ 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;
return x && !x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
export function createCommonjsModule(fn, basedir, module) {
Expand Down
40 changes: 20 additions & 20 deletions packages/commonjs/test/snapshots/function.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ 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;␊
return x && !x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
function createCommonjsModule(fn, basedir, module) {␊
Expand Down Expand Up @@ -264,7 +264,7 @@ 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;␊
return x && !x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
function createCommonjsModule(fn, basedir, module) {␊
Expand Down Expand Up @@ -467,7 +467,7 @@ 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;␊
return x && !x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
function createCommonjsModule(fn, basedir, module) {␊
Expand Down Expand Up @@ -666,7 +666,7 @@ 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;␊
return x && !x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
function createCommonjsModule(fn, basedir, module) {␊
Expand Down Expand Up @@ -884,7 +884,7 @@ 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;␊
return x && !x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
function createCommonjsModule(fn, basedir, module) {␊
Expand Down Expand Up @@ -1067,12 +1067,12 @@ Generated by [AVA](https://ava.li).
> Snapshot 1
{
'lib2-9224c76d.js': `'use strict';␊
'lib2-07a971d0.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;␊
return x && !x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
function createCommonjsModule(fn, basedir, module) {␊
Expand Down Expand Up @@ -1262,7 +1262,7 @@ Generated by [AVA](https://ava.li).
`,
'main.js': `'use strict';␊
␊
var lib2 = require('./lib2-9224c76d.js');␊
var lib2 = require('./lib2-07a971d0.js');␊
␊
/* eslint-disable import/no-dynamic-require, global-require */␊
␊
Expand All @@ -1279,7 +1279,7 @@ Generated by [AVA](https://ava.li).
`,
'main2.js': `'use strict';␊
␊
require('./lib2-9224c76d.js');␊
require('./lib2-07a971d0.js');␊
␊
`,
}
Expand All @@ -1294,7 +1294,7 @@ 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;␊
return x && !x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
function createCommonjsModule(fn, basedir, module) {␊
Expand Down Expand Up @@ -1484,7 +1484,7 @@ 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;␊
return x && !x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
function createCommonjsModule(fn, basedir, module) {␊
Expand Down Expand Up @@ -1680,7 +1680,7 @@ 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;␊
return x && !x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
function createCommonjsModule(fn, basedir, module) {␊
Expand Down Expand Up @@ -1889,7 +1889,7 @@ 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;␊
return x && !x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
function createCommonjsModule(fn, basedir, module) {␊
Expand Down Expand Up @@ -2096,7 +2096,7 @@ 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;␊
return x && !x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
function createCommonjsModule(fn, basedir, module) {␊
Expand Down Expand Up @@ -2297,7 +2297,7 @@ 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;␊
return x && !x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
function createCommonjsModule(fn, basedir, module) {␊
Expand Down Expand Up @@ -2515,7 +2515,7 @@ 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;␊
return x && !x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
function createCommonjsModule(fn, basedir, module) {␊
Expand Down Expand Up @@ -2719,7 +2719,7 @@ 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;␊
return x && !x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
function createCommonjsModule(fn, basedir, module) {␊
Expand Down Expand Up @@ -2915,7 +2915,7 @@ 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;␊
return x && !x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
function createCommonjsModule(fn, basedir, module) {␊
Expand Down Expand Up @@ -3552,7 +3552,7 @@ 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;␊
return x && !x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
␊
function createCommonjsModule(fn, basedir, module) {␊
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);
});

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

0 comments on commit 92166de

Please sign in to comment.