Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.
Closed
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
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ const HELPERS_ID = '\0commonjsHelpers';
const HELPERS = `
export var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}

export function interopDefault(ex) {
return ex && typeof ex === 'object' && 'default' in ex ? ex['default'] : ex;
export function interopDefault(x) {
if ( !x || typeof x !== 'object' || !x.default ) return x;
if ( typeof x['default'] === 'object' || typeof x['default'] === 'function' ) x['default']['default'] = x['default'];
return x['default'];
}

export function createCommonjsModule(fn, module) {
Expand Down Expand Up @@ -125,7 +127,7 @@ export default function commonjs ( options = {} ) {
// Because objects have no guaranteed ordering, yet we need it,
// we need to keep track of the order in a array
let sources = [];

let uid = 0;

let scope = attachScopes( ast, 'scope' );
Expand Down
6 changes: 6 additions & 0 deletions test/samples/react-apollo/commonjs-bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function Bar () {
this.x = 42;
}

exports.__esModule = true;
exports.default = Bar;
4 changes: 4 additions & 0 deletions test/samples/react-apollo/commonjs-foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var Bar = require( './commonjs-bar' );

exports.__esModule = true;
exports.Bar = Bar.default;
3 changes: 3 additions & 0 deletions test/samples/react-apollo/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Bar } from './commonjs-foo.js';

assert.equal( new Bar().x, 42 );
7 changes: 7 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,11 @@ describe( 'rollup-plugin-commonjs', () => {
plugins: [ commonjs() ]
}).then( executeBundle );
});

it( 'does not remove .default properties', () => {
return rollup({
entry: 'samples/react-apollo/main.js',
plugins: [ commonjs() ]
}).then( executeBundle );
});
});