Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warn if default and named exports are used together in auto mode #631

Merged
merged 2 commits into from
May 3, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export default class Bundle {
const format = options.format || 'es6';

// Determine export mode - 'default', 'named', 'none'
const exportMode = getExportMode( this, options.exports );
const exportMode = getExportMode( this, options.exports, options.moduleName );

let magicString = new MagicString.Bundle({ separator: '\n\n' });
let usedModules = [];
Expand Down
5 changes: 4 additions & 1 deletion src/utils/getExportMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function badExports ( option, keys ) {
throw new Error( `'${option}' was specified for options.exports, but entry module has following exports: ${keys.join(', ')}` );
}

export default function getExportMode ( bundle, exportMode ) {
export default function getExportMode ( bundle, exportMode, moduleName ) {
const exportKeys = keys( bundle.entryModule.exports )
.concat( keys( bundle.entryModule.reexports ) )
.concat( bundle.entryModule.exportAllSources ); // not keys, but makes our job easier this way
Expand All @@ -23,6 +23,9 @@ export default function getExportMode ( bundle, exportMode ) {
} else if ( exportKeys.length === 1 && exportKeys[0] === 'default' ) {
exportMode = 'default';
} else {
if ( bundle.entryModule.exports.default ) {
bundle.onwarn( `Using named and default exports together. Consumers of your bundle will have to use ${moduleName || 'bundle'}['default'] to access the default export, which may not be what you want. Use \`exports: 'named'\` to disable this warning. See https://github.com/rollup/rollup/wiki/JavaScript-API#exports for more information` );
}
exportMode = 'named';
}
}
Expand Down
4 changes: 1 addition & 3 deletions test/function/confused-default-identifier/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ var assert = require( 'assert' );

module.exports = {
description: 'Rollup should not get confused and allow "default" as an identifier name',
warnings: function ( warnings ) {
// ignore pending gh-587
}
warnings: function () {} // suppress
};

// https://github.com/rollup/rollup/issues/215
10 changes: 10 additions & 0 deletions test/function/warn-on-auto-named-default-exports/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var assert = require( 'assert' );

module.exports = {
description: 'warns if default and named exports are used in auto mode',
warnings: function ( warnings ) {
assert.deepEqual( warnings, [
'Using named and default exports together. Consumers of your bundle will have to use bundle[\'default\'] to access the default export, which may not be what you want. Use `exports: \'named\'` to disable this warning. See https://github.com/rollup/rollup/wiki/JavaScript-API#exports for more information'
]);
}
};
10 changes: 10 additions & 0 deletions test/function/warn-on-auto-named-default-exports/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function foo () {
console.log( 'foo' );
}

function bar () {
console.log( 'bar' );
}

export default foo;
export { bar };