Skip to content

Commit

Permalink
treat reexports differently from exports
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Aug 11, 2017
1 parent 6ba6440 commit 671757e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
17 changes: 14 additions & 3 deletions src/Bundle.js
Expand Up @@ -169,7 +169,18 @@ export default class Bundle {
if ( declaration.isNamespace ) {
declaration.needsNamespaceBlock = true;
}
} );
});

entryModule.getReexports().forEach( name => {
const declaration = entryModule.traceExport( name );

if ( declaration.isExternal ) {
declaration.reexported = declaration.module.reexported = true;
} else {
declaration.exportName = name;
declaration.activate();
}
});

// mark statements that should appear in the bundle
if ( this.treeshake ) {
Expand Down Expand Up @@ -211,7 +222,7 @@ export default class Bundle {
this.externalModules.forEach( module => {
const unused = Object.keys( module.declarations )
.filter( name => name !== '*' )
.filter( name => !module.declarations[ name ].activated );
.filter( name => !module.declarations[ name ].activated && !module.declarations[ name ].reexported );

if ( unused.length === 0 ) return;

Expand Down Expand Up @@ -471,7 +482,7 @@ export default class Bundle {
}
} );

if ( !magicString.toString().trim() && this.entryModule.getExports().length === 0 ) {
if ( !magicString.toString().trim() && this.entryModule.getExports().length === 0 && this.entryModule.getReexports().length === 0 ) {
this.warn( {
code: 'EMPTY_BUNDLE',
message: 'Generated an empty bundle'
Expand Down
2 changes: 1 addition & 1 deletion src/Declaration.js
Expand Up @@ -48,7 +48,7 @@ export class SyntheticNamespaceDeclaration {
this.needsNamespaceBlock = false;

this.originals = blank();
module.getExports().forEach( name => {
module.getExports().concat( module.getReexports() ).forEach( name => {
this.originals[ name ] = module.traceExport( name );
});
}
Expand Down
18 changes: 9 additions & 9 deletions src/Module.js
Expand Up @@ -303,28 +303,28 @@ export default class Module {
}

getExports () {
const exports = blank();
return keys( this.exports );
}

keys( this.exports ).forEach( name => {
exports[ name ] = true;
} );
getReexports () {
const reexports = blank();

keys( this.reexports ).forEach( name => {
exports[ name ] = true;
reexports[ name ] = true;
} );

this.exportAllModules.forEach( module => {
if ( module.isExternal ) {
exports[ `*${module.id}` ] = true;
reexports[ `*${module.id}` ] = true;
return;
}

module.getExports().forEach( name => {
if ( name !== 'default' ) exports[ name ] = true;
module.getExports().concat( module.getReexports() ).forEach( name => {
if ( name !== 'default' ) reexports[ name ] = true;
} );
} );

return keys( exports );
return keys( reexports );
}

namespace () {
Expand Down
4 changes: 3 additions & 1 deletion src/finalisers/cjs.js
Expand Up @@ -31,7 +31,9 @@ export default function cjs ( bundle, magicString, { exportMode, intro, outro },
const activated = Object.keys( module.declarations )
.filter( name => module.declarations[ name ].activated );

return activated.length ?
const needsVar = activated.length || module.reexported;

return needsVar ?
`${varOrConst} ${module.name} = require('${module.path}');` :
`require('${module.path}');`;
}
Expand Down
2 changes: 1 addition & 1 deletion src/finalisers/es.js
Expand Up @@ -62,7 +62,7 @@ export default function es ( bundle, magicString, { intro, outro } ) {

const exportAllDeclarations = [];

const specifiers = module.getExports()
const specifiers = module.getExports().concat( module.getReexports() )
.filter( notDefault )
.map( name => {
const declaration = module.traceExport( name );
Expand Down
6 changes: 4 additions & 2 deletions src/finalisers/shared/getExportBlock.js
Expand Up @@ -5,7 +5,7 @@ export default function getExportBlock ( bundle, exportMode, mechanism = 'return
return `${mechanism} ${entryModule.traceExport( 'default' ).getName( false )};`;
}

return entryModule.getExports()
const exports = entryModule.getExports().concat( entryModule.getReexports() )
.map( name => {
if ( name[0] === '*' ) {
// export all from external
Expand All @@ -27,7 +27,9 @@ export default function getExportBlock ( bundle, exportMode, mechanism = 'return
if ( lhs === rhs ) return null;

return `${lhs} = ${rhs};`;
})
});

return exports
.filter( Boolean )
.join( '\n' );
}

0 comments on commit 671757e

Please sign in to comment.