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