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

Commit

Permalink
Merge pull request #113 from rollup/gh-112
Browse files Browse the repository at this point in the history
Add named exports to default, in optimised modules
  • Loading branch information
Rich-Harris committed Sep 18, 2016
2 parents c998c3d + 289fa23 commit e24f1e8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ export default function transform ( code, id, isEntry, ignoreGlobal, customNamed

if ( customNamedExports ) customNamedExports.forEach( addExport );

const defaultExportPropertyAssignments = [];
let hasDefaultExport = false;

if ( shouldWrap ) {
const args = `module${uses.exports ? ', exports' : ''}`;

Expand All @@ -250,7 +253,6 @@ export default function transform ( code, id, isEntry, ignoreGlobal, customNamed
.filter( key => !blacklistedExports[ key ] )
.forEach( addExport );
} else {
let hasDefaultExport = false;
const names = [];

ast.body.forEach( node => {
Expand Down Expand Up @@ -279,6 +281,7 @@ export default function transform ( code, id, isEntry, ignoreGlobal, customNamed
`export { ${deconflicted} as ${name} };`;

namedExportDeclarations.push( declaration );
defaultExportPropertyAssignments.push( `${moduleName}.${name} = ${deconflicted};` );
}
}
});
Expand All @@ -294,7 +297,10 @@ export default function transform ( code, id, isEntry, ignoreGlobal, customNamed
`export default ${HELPERS_NAME}.unwrapExports(${moduleName});` :
`export default ${moduleName};`;

const exportBlock = '\n\n' + [ defaultExport ].concat( namedExportDeclarations ).join( '\n' );
const exportBlock = '\n\n' + [ defaultExport ]
.concat( namedExportDeclarations )
.concat( hasDefaultExport ? defaultExportPropertyAssignments : [] )
.join( '\n' );

magicString.trim()
.prepend( importBlock + wrapperStart )
Expand Down
5 changes: 5 additions & 0 deletions test/function/assign-properties-to-default-export/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var foo = {};

module.exports = foo;
module.exports.bar = 1;
exports.baz = 2;
4 changes: 4 additions & 0 deletions test/function/assign-properties-to-default-export/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import foo from './foo.js';

assert.equal( foo.bar, 1 );
assert.equal( foo.baz, 2 );

0 comments on commit e24f1e8

Please sign in to comment.