mirror of https://github.com/lukechilds/rollup.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.6 KiB
69 lines
1.6 KiB
import { keys } from '../utils/object';
|
|
|
|
function specifiersFor ( externalModule ) {
|
|
return keys( externalModule.importedByBundle )
|
|
.filter( notDefault )
|
|
.sort()
|
|
.map( name => {
|
|
const id = externalModule.exports.lookup( name );
|
|
|
|
return name !== id.name ? `${name} as ${id.name}` : name;
|
|
});
|
|
}
|
|
|
|
function notDefault ( name ) {
|
|
return name !== 'default';
|
|
}
|
|
|
|
export default function es6 ( bundle, magicString ) {
|
|
const importBlock = bundle.externalModules
|
|
.map( module => {
|
|
const specifiers = [];
|
|
|
|
const id = module.exports.lookup( 'default' );
|
|
|
|
if ( id ) {
|
|
specifiers.push( id.name );
|
|
}
|
|
|
|
if ( module.needsAll ) {
|
|
specifiers.push( '* as ' + module.name );
|
|
}
|
|
|
|
if ( module.needsNamed ) {
|
|
specifiers.push( '{ ' + specifiersFor( module ).join( ', ' ) + ' }' );
|
|
}
|
|
|
|
return specifiers.length ?
|
|
`import ${specifiers.join( ', ' )} from '${module.id}';` :
|
|
`import '${module.id}';`;
|
|
})
|
|
.join( '\n' );
|
|
|
|
if ( importBlock ) {
|
|
magicString.prepend( importBlock + '\n\n' );
|
|
}
|
|
|
|
const module = bundle.entryModule;
|
|
|
|
const specifiers = bundle.toExport.filter( notDefault ).map( name => {
|
|
const id = bundle.exports.lookup( name );
|
|
|
|
return id.name === name ?
|
|
name :
|
|
`${id.name} as ${name}`;
|
|
});
|
|
|
|
let exportBlock = specifiers.length ? `export { ${specifiers.join(', ')} };` : '';
|
|
|
|
const defaultExport = module.exports.lookup( 'default' );
|
|
if ( defaultExport ) {
|
|
exportBlock += `export default ${ defaultExport.name };`;
|
|
}
|
|
|
|
if ( exportBlock ) {
|
|
magicString.append( '\n\n' + exportBlock.trim() );
|
|
}
|
|
|
|
return magicString.trim();
|
|
}
|
|
|