1 | import { keys } from '../utils/object.js';
|
2 |
|
3 | function notDefault ( name ) {
|
4 | return name !== 'default';
|
5 | }
|
6 |
|
7 | export default function es6 ( bundle, magicString ) {
|
8 | const importBlock = bundle.externalModules
|
9 | .map( module => {
|
10 | const specifiers = [];
|
11 | const importedNames = keys( module.declarations )
|
12 | .filter( name => name !== '*' && name !== 'default' );
|
13 |
|
14 | if ( module.declarations.default ) {
|
15 | specifiers.push( module.name );
|
16 | }
|
17 |
|
18 | if ( module.declarations['*'] ) {
|
19 | specifiers.push( `* as ${module.name}` );
|
20 | }
|
21 |
|
22 | if ( importedNames.length ) {
|
23 | specifiers.push( `{ ${importedNames.join( ', ' )} }` );
|
24 | }
|
25 |
|
26 | return specifiers.length ?
|
27 | `import ${specifiers.join( ', ' )} from '${module.id}';` :
|
28 | `import '${module.id}';`;
|
29 | })
|
30 | .join( '\n' );
|
31 |
|
32 | if ( importBlock ) {
|
33 | magicString.prepend( importBlock + '\n\n' );
|
34 | }
|
35 |
|
36 | const module = bundle.entryModule;
|
37 |
|
38 | const specifiers = module.getExports().filter( notDefault ).map( name => {
|
39 | const declaration = module.traceExport( name );
|
40 |
|
41 | return declaration.name === name ?
|
42 | name :
|
43 | `${declaration.name} as ${name}`;
|
44 | });
|
45 |
|
46 | let exportBlock = specifiers.length ? `export { ${specifiers.join(', ')} };` : '';
|
47 |
|
48 | const defaultExport = module.exports.default || module.reexports.default;
|
49 | if ( defaultExport ) {
|
50 | exportBlock += `export default ${module.traceExport( 'default' ).name};`;
|
51 | }
|
52 |
|
53 | if ( exportBlock ) {
|
54 | magicString.append( '\n\n' + exportBlock.trim() );
|
55 | }
|
56 |
|
57 | return magicString.trim();
|
58 | }
|