Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.

Commit 61c6e00

Browse files
committed
more complete CJS <-> ES interop
1 parent 715f9cd commit 61c6e00

5 files changed

Lines changed: 25 additions & 3 deletions

File tree

src/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ const HELPERS_ID = '\0commonjsHelpers';
4949
const HELPERS = `
5050
export var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}
5151
52-
export function interopDefault(ex) {
53-
return ex && typeof ex === 'object' && 'default' in ex ? ex['default'] : ex;
52+
export function interopDefault(x) {
53+
if ( !x || typeof x !== 'object' || !x.default ) return x;
54+
if ( typeof x['default'] === 'object' || typeof x['default'] === 'function' ) x['default']['default'] = x['default'];
55+
return x['default'];
5456
}
5557
5658
export function createCommonjsModule(fn, module) {
@@ -125,7 +127,7 @@ export default function commonjs ( options = {} ) {
125127
// Because objects have no guaranteed ordering, yet we need it,
126128
// we need to keep track of the order in a array
127129
let sources = [];
128-
130+
129131
let uid = 0;
130132

131133
let scope = attachScopes( ast, 'scope' );
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function Bar () {
2+
this.x = 42;
3+
}
4+
5+
exports.__esModule = true;
6+
exports.default = Bar;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var Bar = require( './commonjs-bar' );
2+
3+
exports.__esModule = true;
4+
exports.Bar = Bar.default;

test/samples/react-apollo/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Bar } from './commonjs-foo.js';
2+
3+
assert.equal( new Bar().x, 42 );

test/test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,4 +370,11 @@ describe( 'rollup-plugin-commonjs', () => {
370370
plugins: [ commonjs() ]
371371
}).then( executeBundle );
372372
});
373+
374+
it( 'does not remove .default properties', () => {
375+
return rollup({
376+
entry: 'samples/react-apollo/main.js',
377+
plugins: [ commonjs() ]
378+
}).then( executeBundle );
379+
});
373380
});

0 commit comments

Comments
 (0)