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

Commit

Permalink
handle duplicate default export case (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Mar 7, 2017
1 parent 81dc501 commit 5cf3b07
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,11 @@ export default function transformCommonjs ( code, id, isEntry, ignoreGlobal, cus

const moduleName = deconflict( scope, globals, getName( id ) );
if ( !isEntry ) {
const exportModuleExports = `export { ${moduleName} as __moduleExports };`;
const exportModuleExports = {
str: `export { ${moduleName} as __moduleExports };`,
name: '__moduleExports'
};

namedExportDeclarations.push( exportModuleExports );
}

Expand All @@ -248,7 +252,10 @@ export default function transformCommonjs ( code, id, isEntry, ignoreGlobal, cus
`export var ${x} = ${moduleName}.${x};` :
`var ${deconflicted} = ${moduleName}.${x};\nexport { ${deconflicted} as ${x} };`;

namedExportDeclarations.push( declaration );
namedExportDeclarations.push({
str: declaration,
name: x
});
}

if ( customNamedExports ) customNamedExports.forEach( addExport );
Expand Down Expand Up @@ -293,7 +300,11 @@ export default function transformCommonjs ( code, id, isEntry, ignoreGlobal, cus
`export { ${name} };` :
`export { ${deconflicted} as ${name} };`;

namedExportDeclarations.push( declaration );
namedExportDeclarations.push({
str: declaration,
name
});

defaultExportPropertyAssignments.push( `${moduleName}.${name} = ${deconflicted};` );
}
}
Expand All @@ -310,8 +321,12 @@ export default function transformCommonjs ( code, id, isEntry, ignoreGlobal, cus
`export default ${HELPERS_NAME}.unwrapExports(${moduleName});` :
`export default ${moduleName};`;

const named = namedExportDeclarations
.filter( x => x.name !== 'default' || !hasDefaultExport )
.map( x => x.str );

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

Expand Down
3 changes: 3 additions & 0 deletions test/function/duplicate-default-exports-b/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import x from './x';

assert.deepEqual( x, { default: 42 });
4 changes: 4 additions & 0 deletions test/function/duplicate-default-exports-b/x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var x = {};

module.exports = x;
module.exports.default = 42;
3 changes: 3 additions & 0 deletions test/function/duplicate-default-exports/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import x from './x';

assert.strictEqual( x.default, x );
4 changes: 4 additions & 0 deletions test/function/duplicate-default-exports/x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var x = {};

module.exports = x;
module.exports.default = x;

0 comments on commit 5cf3b07

Please sign in to comment.