Skip to content

Commit

Permalink
fix export-from declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Aug 12, 2017
1 parent adef43f commit 20bff7d
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 18 deletions.
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -66,6 +66,7 @@
"rollup-plugin-commonjs": "^8.0.2",
"rollup-plugin-json": "^2.3.0",
"rollup-plugin-node-resolve": "^3.0.0",
"rollup-plugin-replace": "^1.1.1",
"rollup-plugin-string": "^2.0.0",
"rollup-pluginutils": "^2.0.1",
"rollup-watch": "^4.3.1",
Expand Down
52 changes: 34 additions & 18 deletions src/finalisers/es.js
Expand Up @@ -65,35 +65,51 @@ export default function es ( bundle, magicString, { intro, outro } ) {

const module = bundle.entryModule;

const exportInternalSpecifiers = [];
const exportExternalSpecifiers = new Map();
const exportAllDeclarations = [];

const specifiers = module.getExports().concat( module.getReexports() )
module.getExports()
.filter( notDefault )
.map( name => {
.forEach( name => {
const declaration = module.traceExport( name );
const rendered = declaration.getName( true );
exportInternalSpecifiers.push( rendered === name ? name : `${rendered} as ${name}` );
});

if ( name[0] === '*' ) {
// export * from 'external'
exportAllDeclarations.push( `export * from '${name.slice( 1 )}';` );
return;
}
module.getReexports()
.filter( notDefault )
.forEach( name => {
const declaration = module.traceExport( name );

return rendered === name ?
name :
`${rendered} as ${name}`;
})
.filter( Boolean );
if ( declaration.isExternal ) {
if ( name[0] === '*' ) {
// export * from 'external'
exportAllDeclarations.push( `export * from '${name.slice( 1 )}';` );
} else {
if ( !exportExternalSpecifiers.has( declaration.module.id ) ) exportExternalSpecifiers.set( declaration.module.id, [] );
exportExternalSpecifiers.get( declaration.module.id ).push( name );
}

let exportBlock = specifiers.length ? `export { ${specifiers.join(', ')} };` : '';
return;
}

const defaultExport = module.exports.default || module.reexports.default;
if ( defaultExport ) {
exportBlock += `export default ${module.traceExport( 'default' ).getName( true )};`;
const rendered = declaration.getName( true );
exportInternalSpecifiers.push( rendered === name ? name : `${rendered} as ${name}` );
});

const exportBlock = [];
if ( exportInternalSpecifiers.length ) exportBlock.push( `export { ${exportInternalSpecifiers.join(', ')} };` );
if ( module.exports.default || module.reexports.default ) exportBlock.push( `export default ${module.traceExport( 'default' ).getName( true )};` );
if ( exportAllDeclarations.length ) exportBlock.push( exportAllDeclarations.join( '\n' ) );
if ( exportExternalSpecifiers.size ) {
exportExternalSpecifiers.forEach( ( specifiers, id ) => {
exportBlock.push( `export { ${specifiers.join( ', ' )} } from '${id}';` );
});
}

if ( exportBlock ) magicString.append( '\n\n' + exportBlock.trim() );
if ( exportAllDeclarations.length ) magicString.append( '\n\n' + exportAllDeclarations.join( '\n' ).trim() );
if ( exportBlock.length ) magicString.append( '\n\n' + exportBlock.join( '\n' ).trim() );

if ( outro ) magicString.append( outro );

return magicString.trim();
Expand Down
9 changes: 9 additions & 0 deletions test/form/samples/reexports-name-from-external/_config.js
@@ -0,0 +1,9 @@
const assert = require( 'assert' );

module.exports = {
description: 're-exports name from external module',
options: {
external: [ 'external' ],
moduleName: 'myBundle'
}
};
@@ -0,0 +1,9 @@
define(['exports', 'external'], function (exports, external) { 'use strict';



exports.foo = external.foo;

Object.defineProperty(exports, '__esModule', { value: true });

});
@@ -0,0 +1,9 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var external = require('external');



exports.foo = external.foo;
@@ -0,0 +1 @@
export { foo } from 'external';
10 changes: 10 additions & 0 deletions test/form/samples/reexports-name-from-external/_expected/iife.js
@@ -0,0 +1,10 @@
var myBundle = (function (exports,external) {
'use strict';



exports.foo = external.foo;

return exports;

}({},external));
11 changes: 11 additions & 0 deletions test/form/samples/reexports-name-from-external/_expected/umd.js
@@ -0,0 +1,11 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('external')) :
typeof define === 'function' && define.amd ? define(['exports', 'external'], factory) :
(factory((global.myBundle = {}),global.external));
}(this, (function (exports,external) { 'use strict';

exports.foo = external.foo;

Object.defineProperty(exports, '__esModule', { value: true });

})));
1 change: 1 addition & 0 deletions test/form/samples/reexports-name-from-external/main.js
@@ -0,0 +1 @@
export { foo } from 'external';

0 comments on commit 20bff7d

Please sign in to comment.