Skip to content
Permalink
Browse files

Merge pull request #113 from rollup/gh-112

Add named exports to default, in optimised modules
  • Loading branch information...
Rich-Harris committed Sep 18, 2016
2 parents c998c3d + 289fa23 commit e24f1e85a4a1a28927c9375422f676dde5aba3ed
@@ -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' : ''}`;

@@ -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 => {
@@ -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};` );
}
}
});
@@ -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 )
@@ -0,0 +1,5 @@
var foo = {};

module.exports = foo;
module.exports.bar = 1;
exports.baz = 2;
@@ -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.
You can’t perform that action at this time.