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

Commit

Permalink
assign properties to default export (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Sep 18, 2016
1 parent b5cbba3 commit b45ecdc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ export default function transform ( code, id, isEntry, ignoreGlobal, customNamed

if ( customNamedExports ) customNamedExports.forEach( addExport );

const defaultExportPropertyAssignments = [];

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

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( 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 b45ecdc

Please sign in to comment.