From 5cf3b07ab0f57d821b0051e2f2d409065af7fe13 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Tue, 7 Mar 2017 17:05:17 -0500 Subject: [PATCH] handle duplicate default export case (#158) --- src/transform.js | 23 +++++++++++++++---- .../duplicate-default-exports-b/main.js | 3 +++ .../function/duplicate-default-exports-b/x.js | 4 ++++ .../duplicate-default-exports/main.js | 3 +++ test/function/duplicate-default-exports/x.js | 4 ++++ 5 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 test/function/duplicate-default-exports-b/main.js create mode 100644 test/function/duplicate-default-exports-b/x.js create mode 100644 test/function/duplicate-default-exports/main.js create mode 100644 test/function/duplicate-default-exports/x.js diff --git a/src/transform.js b/src/transform.js index fc41fe5..b853d43 100644 --- a/src/transform.js +++ b/src/transform.js @@ -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 ); } @@ -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 ); @@ -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};` ); } } @@ -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' ); diff --git a/test/function/duplicate-default-exports-b/main.js b/test/function/duplicate-default-exports-b/main.js new file mode 100644 index 0000000..69bd01f --- /dev/null +++ b/test/function/duplicate-default-exports-b/main.js @@ -0,0 +1,3 @@ +import x from './x'; + +assert.deepEqual( x, { default: 42 }); \ No newline at end of file diff --git a/test/function/duplicate-default-exports-b/x.js b/test/function/duplicate-default-exports-b/x.js new file mode 100644 index 0000000..e48696f --- /dev/null +++ b/test/function/duplicate-default-exports-b/x.js @@ -0,0 +1,4 @@ +var x = {}; + +module.exports = x; +module.exports.default = 42; \ No newline at end of file diff --git a/test/function/duplicate-default-exports/main.js b/test/function/duplicate-default-exports/main.js new file mode 100644 index 0000000..389d125 --- /dev/null +++ b/test/function/duplicate-default-exports/main.js @@ -0,0 +1,3 @@ +import x from './x'; + +assert.strictEqual( x.default, x ); \ No newline at end of file diff --git a/test/function/duplicate-default-exports/x.js b/test/function/duplicate-default-exports/x.js new file mode 100644 index 0000000..ff6c6fb --- /dev/null +++ b/test/function/duplicate-default-exports/x.js @@ -0,0 +1,4 @@ +var x = {}; + +module.exports = x; +module.exports.default = x; \ No newline at end of file