diff --git a/src/Bundle.js b/src/Bundle.js index ac8dd50..31627f2 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -94,7 +94,24 @@ export default class Bundle { // Discover conflicts (i.e. two statements in separate modules both define `foo`) this.statements.forEach( statement => { - keys( statement.defines ).forEach( name => { + const module = statement.module; + const names = keys( statement.defines ); + + // with default exports that are expressions (`export default 42`), + // we need to ensure that the name chosen for the expression does + // not conflict + if ( statement.node.type === 'ExportDefaultDeclaration' ) { + const name = module.getCanonicalName( 'default' ); + + const isProxy = statement.node.declaration && statement.node.declaration.type === 'Identifier'; + const shouldDeconflict = !isProxy || ( module.getCanonicalName( statement.node.declaration.name ) !== name ); + + if ( shouldDeconflict && !~names.indexOf( name ) ) { + names.push( name ); + } + } + + names.forEach( name => { if ( has( definers, name ) ) { conflicts[ name ] = true; } else { @@ -103,7 +120,7 @@ export default class Bundle { // TODO in good js, there shouldn't be duplicate definitions // per module... but some people write bad js - definers[ name ].push( statement.module ); + definers[ name ].push( module ); }); }); diff --git a/test/function/imports-are-deconflicted-redux/_config.js b/test/function/imports-are-deconflicted-redux/_config.js new file mode 100644 index 0000000..a7c1041 --- /dev/null +++ b/test/function/imports-are-deconflicted-redux/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'deconflicts imports (redux)' +}; diff --git a/test/function/imports-are-deconflicted-redux/bar.js b/test/function/imports-are-deconflicted-redux/bar.js new file mode 100644 index 0000000..22af609 --- /dev/null +++ b/test/function/imports-are-deconflicted-redux/bar.js @@ -0,0 +1,5 @@ +import foo from 'path'; + +export default function () { + return foo.relative( 'foo/bar/baz', 'foo/baz/bar' ); +}; diff --git a/test/function/imports-are-deconflicted-redux/foo.js b/test/function/imports-are-deconflicted-redux/foo.js new file mode 100644 index 0000000..4b02f78 --- /dev/null +++ b/test/function/imports-are-deconflicted-redux/foo.js @@ -0,0 +1,2 @@ +var z = 'foo'; +export default z; diff --git a/test/function/imports-are-deconflicted-redux/main.js b/test/function/imports-are-deconflicted-redux/main.js new file mode 100644 index 0000000..fa81bd0 --- /dev/null +++ b/test/function/imports-are-deconflicted-redux/main.js @@ -0,0 +1,5 @@ +import foo from './foo'; +import bar from './bar'; + +assert.equal( foo, 'foo' ); +assert.equal( bar(), '../../baz/bar' ); diff --git a/test/function/imports-are-deconflicted/_config.js b/test/function/imports-are-deconflicted/_config.js new file mode 100644 index 0000000..49dbfd8 --- /dev/null +++ b/test/function/imports-are-deconflicted/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'deconflicts imports' +}; diff --git a/test/function/imports-are-deconflicted/bar.js b/test/function/imports-are-deconflicted/bar.js new file mode 100644 index 0000000..22af609 --- /dev/null +++ b/test/function/imports-are-deconflicted/bar.js @@ -0,0 +1,5 @@ +import foo from 'path'; + +export default function () { + return foo.relative( 'foo/bar/baz', 'foo/baz/bar' ); +}; diff --git a/test/function/imports-are-deconflicted/foo.js b/test/function/imports-are-deconflicted/foo.js new file mode 100644 index 0000000..d02ba54 --- /dev/null +++ b/test/function/imports-are-deconflicted/foo.js @@ -0,0 +1 @@ +export default 'foo'; diff --git a/test/function/imports-are-deconflicted/main.js b/test/function/imports-are-deconflicted/main.js new file mode 100644 index 0000000..fa81bd0 --- /dev/null +++ b/test/function/imports-are-deconflicted/main.js @@ -0,0 +1,5 @@ +import foo from './foo'; +import bar from './bar'; + +assert.equal( foo, 'foo' ); +assert.equal( bar(), '../../baz/bar' );