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.

57 lines
1.6 KiB

import { keys } from '../utils/object';
export default function es6 ( bundle, magicString ) {
const importBlock = bundle.externalModules
.map( module => {
const specifiers = [];
if ( module.needsDefault ) {
specifiers.push( module.importedByBundle.filter( declaration =>
declaration.name === 'default' )[0].localName );
}
if ( module.needsAll ) {
specifiers.push( '* as ' + module.importedByBundle.filter( declaration =>
declaration.name === '*' )[0].localName );
}
if ( module.needsNamed ) {
specifiers.push( '{ ' + module.importedByBundle
.filter( declaration => !/^(default|\*)$/.test( declaration.name ) )
.map( ({ name, localName }) =>
name === localName ? name : `${name} as ${localName}` )
.join( ', ' ) + ' }' );
}
return specifiers.length ?
`import ${specifiers.join( ', ' )} from '${module.id}';` :
`import '${module.id}';`;
})
.join( '\n' );
if ( importBlock ) {
magicString.prepend( importBlock + '\n\n' );
}
const exports = bundle.entryModule.exports;
const exportBlock = keys( exports ).map( exportedName => {
const specifier = exports[ exportedName ];
const canonicalName = bundle.entryModule.replacements[ specifier.localName ] || specifier.localName;
if ( exportedName === 'default' ) {
return `export default ${canonicalName};`;
}
return exportedName === canonicalName ?
`export { ${exportedName} };` :
`export { ${canonicalName} as ${exportedName} };`;
}).join( '\n' );
if ( exportBlock ) {
magicString.append( '\n\n' + exportBlock );
}
return magicString.trim();
}