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

Add back support for named exports from module.exports objects #272

Merged
merged 2 commits into from
Jan 26, 2018
Merged
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: 4 additions & 4 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down Expand Up @@ -396,6 +392,7 @@ export function transformCommonjs ( code, id, isEntry, ignoreGlobal, ignoreRequi
str: declaration,
name
});
delete namedExports[name];
}

defaultExportPropertyAssignments.push( `${moduleName}.${name} = ${deconflicted};` );
Expand All @@ -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});` :
Expand Down
16 changes: 15 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -450,4 +464,4 @@ describe( 'rollup-plugin-commonjs', () => {
assert.equal( warns.length, 0 );
});
});
});
});