diff --git a/src/transform.js b/src/transform.js index eb12e45..b6fd675 100644 --- a/src/transform.js +++ b/src/transform.js @@ -359,10 +359,6 @@ export function transformCommonjs ( code, id, isEntry, ignoreGlobal, ignoreRequi wrapperStart = `var ${moduleName} = ${HELPERS_NAME}.createCommonjsModule(function (${args}) {\n`; wrapperEnd = `\n});`; - - Object.keys( namedExports ) - .filter( key => !blacklist[ key ] ) - .forEach( addExport ); } else { const names = []; @@ -396,6 +392,7 @@ export function transformCommonjs ( code, id, isEntry, ignoreGlobal, ignoreRequi str: declaration, name }); + delete namedExports[name]; } defaultExportPropertyAssignments.push( `${moduleName}.${name} = ${deconflicted};` ); @@ -409,6 +406,9 @@ export function transformCommonjs ( code, id, isEntry, ignoreGlobal, ignoreRequi }\n};`; } } + Object.keys( namedExports ) + .filter( key => !blacklist[ key ] ) + .forEach( addExport ); const defaultExport = /__esModule/.test( code ) ? `export default ${HELPERS_NAME}.unwrapExports(${moduleName});` : diff --git a/test/test.js b/test/test.js index 37655c2..04941e7 100644 --- a/test/test.js +++ b/test/test.js @@ -252,6 +252,20 @@ describe( 'rollup-plugin-commonjs', () => { assert.equal( (await executeBundle( bundle )).exports, 42 ); }); + it( 'identifies named exports from object literals', async () => { + const bundle = await rollup({ + input: 'samples/named-exports-from-object-literal/main.js', + plugins: [ commonjs() ] + }); + + const { code } = await bundle.generate({ + format: 'cjs' + }); + + const fn = new Function ( 'module', 'assert', code ); + fn( {}, assert ); + }); + it( 'can ignore references to `global`', async () => { const bundle = await rollup({ input: 'samples/ignore-global/main.js', @@ -450,4 +464,4 @@ describe( 'rollup-plugin-commonjs', () => { assert.equal( warns.length, 0 ); }); }); -}); \ No newline at end of file +});