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

Add named exports to default, in optimised modules #113

Merged
merged 3 commits into from
Sep 18, 2016
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
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 );