diff --git a/src/index.js b/src/index.js index bdacf61..ef64748 100644 --- a/src/index.js +++ b/src/index.js @@ -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) { @@ -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' ); diff --git a/test/samples/react-apollo/commonjs-bar.js b/test/samples/react-apollo/commonjs-bar.js new file mode 100644 index 0000000..d371ae5 --- /dev/null +++ b/test/samples/react-apollo/commonjs-bar.js @@ -0,0 +1,6 @@ +function Bar () { + this.x = 42; +} + +exports.__esModule = true; +exports.default = Bar; diff --git a/test/samples/react-apollo/commonjs-foo.js b/test/samples/react-apollo/commonjs-foo.js new file mode 100644 index 0000000..ea021a0 --- /dev/null +++ b/test/samples/react-apollo/commonjs-foo.js @@ -0,0 +1,4 @@ +var Bar = require( './commonjs-bar' ); + +exports.__esModule = true; +exports.Bar = Bar.default; diff --git a/test/samples/react-apollo/main.js b/test/samples/react-apollo/main.js new file mode 100644 index 0000000..b3979e6 --- /dev/null +++ b/test/samples/react-apollo/main.js @@ -0,0 +1,3 @@ +import { Bar } from './commonjs-foo.js'; + +assert.equal( new Bar().x, 42 ); diff --git a/test/test.js b/test/test.js index 976da8f..262d5ec 100644 --- a/test/test.js +++ b/test/test.js @@ -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 ); + }); });