From 4dae4606924e9835e77ce52ffc4a1c1d63885027 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Fri, 8 Sep 2017 10:27:02 +0200 Subject: [PATCH] Make unwrapExports only pick out default when it exists Issue #224 --- src/helpers.js | 2 +- test/samples/use-own-output/from-rollup.js | 2 ++ test/samples/use-own-output/main.js | 2 ++ test/test.js | 12 ++++++++++++ 4 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 test/samples/use-own-output/from-rollup.js create mode 100644 test/samples/use-own-output/main.js diff --git a/src/helpers.js b/src/helpers.js index cb62211..7748cd8 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -8,7 +8,7 @@ export function commonjsRequire () { } export function unwrapExports (x) { - return x && x.__esModule ? x['default'] : x; + return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; } export function createCommonjsModule(fn, module) { diff --git a/test/samples/use-own-output/from-rollup.js b/test/samples/use-own-output/from-rollup.js new file mode 100644 index 0000000..3a0bcda --- /dev/null +++ b/test/samples/use-own-output/from-rollup.js @@ -0,0 +1,2 @@ +Object.defineProperty(exports, '__esModule', { value: true }); +exports.x = 10 diff --git a/test/samples/use-own-output/main.js b/test/samples/use-own-output/main.js new file mode 100644 index 0000000..c011220 --- /dev/null +++ b/test/samples/use-own-output/main.js @@ -0,0 +1,2 @@ +import * as b from "./from-rollup"; +window.b = b; diff --git a/test/test.js b/test/test.js index 074c357..cb581f4 100644 --- a/test/test.js +++ b/test/test.js @@ -420,5 +420,17 @@ describe( 'rollup-plugin-commonjs', () => { assert.notEqual( code.indexOf( 'var validVar' ), -1 ); assert.notEqual( code.indexOf( 'var nonIndex' ), -1 ); }); + + it( 'does not misassign default when consuming rollup output', async () => { + // Issue #224 + const bundle = await rollup({ + entry: 'samples/use-own-output/main.js', + plugins: [ commonjs() ], + }); + + const window = {}; + await executeBundle( bundle, { context: { window } } ); + assert.notEqual( window.b.default, undefined ); + }); }); });