Skip to content

Commit

Permalink
Merge pull request #1226 from rollup/gh-791
Browse files Browse the repository at this point in the history
implement export * from external
  • Loading branch information
Rich-Harris authored Jan 6, 2017
2 parents be76a1f + ade3ee3 commit 47e0f93
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 14 deletions.
5 changes: 4 additions & 1 deletion src/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,10 @@ export default class Module {
});

this.exportAllModules.forEach( module => {
if ( module.isExternal ) return; // TODO
if ( module.isExternal ) {
exports[ `*${module.id}` ] = true;
return;
}

module.getExports().forEach( name => {
if ( name !== 'default' ) exports[ name ] = true;
Expand Down
2 changes: 1 addition & 1 deletion src/finalisers/amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function amd ( bundle, magicString, { exportMode, indentString, i

if ( intro ) magicString.prepend( intro );

const exportBlock = getExportBlock( bundle.entryModule, exportMode );
const exportBlock = getExportBlock( bundle, exportMode );
if ( exportBlock ) magicString.append( '\n\n' + exportBlock );
if ( exportMode === 'named' && options.legacy !== true ) magicString.append( `\n\n${esModuleExport}` );
if ( outro ) magicString.append( outro );
Expand Down
2 changes: 1 addition & 1 deletion src/finalisers/cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function cjs ( bundle, magicString, { exportMode, intro, outro },

magicString.prepend( intro );

const exportBlock = getExportBlock( bundle.entryModule, exportMode, 'module.exports =' );
const exportBlock = getExportBlock( bundle, exportMode, 'module.exports =' );
if ( exportBlock ) magicString.append( '\n\n' + exportBlock );
if ( outro ) magicString.append( outro );

Expand Down
37 changes: 29 additions & 8 deletions src/finalisers/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@ function notDefault ( name ) {

export default function es ( bundle, magicString, { intro, outro } ) {
const importBlock = bundle.externalModules
.filter( module => {
const imported = keys( module.declarations );
return imported.length !== 1 || imported[0][0] !== '*';
})
.map( module => {
const specifiers = [];
const specifiersList = [specifiers];
const importedNames = keys( module.declarations )
.filter( name => name !== '*' && name !== 'default' )
.filter( name => module.declarations[ name ].activated )
.map( name => {
if ( name[0] === '*' ) {
return `* as ${module.name}`;
}

const declaration = module.declarations[ name ];

if ( declaration.name === declaration.safeName ) return declaration.name;
return `${declaration.name} as ${declaration.safeName}`;
});
})
.filter( Boolean );

if ( module.declarations.default ) {
if ( module.exportsNamespace ) {
Expand Down Expand Up @@ -55,14 +64,25 @@ export default function es ( bundle, magicString, { intro, outro } ) {

const module = bundle.entryModule;

const specifiers = module.getExports().filter( notDefault ).map( name => {
const declaration = module.traceExport( name );
const rendered = declaration.getName( true );
const exportAllDeclarations = [];

const specifiers = module.getExports()
.filter( notDefault )
.map( name => {
const declaration = module.traceExport( name );
const rendered = declaration.getName( true );

return rendered === name ?
name :
`${rendered} as ${name}`;
});
if ( name[0] === '*' ) {
// export * from 'external'
exportAllDeclarations.push( `export * from '${name.slice( 1 )}';` );
return;
}

return rendered === name ?
name :
`${rendered} as ${name}`;
})
.filter( Boolean );

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

Expand All @@ -72,6 +92,7 @@ export default function es ( bundle, magicString, { intro, outro } ) {
}

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

return magicString.trim();
Expand Down
2 changes: 1 addition & 1 deletion src/finalisers/iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default function iife ( bundle, magicString, { exportMode, indentString,

if ( intro ) magicString.prepend( intro );

const exportBlock = getExportBlock( bundle.entryModule, exportMode );
const exportBlock = getExportBlock( bundle, exportMode );
if ( exportBlock ) magicString.append( '\n\n' + exportBlock );
if ( outro ) magicString.append( outro );

Expand Down
12 changes: 11 additions & 1 deletion src/finalisers/shared/getExportBlock.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
export default function getExportBlock ( entryModule, exportMode, mechanism = 'return' ) {
export default function getExportBlock ( bundle, exportMode, mechanism = 'return' ) {
const entryModule = bundle.entryModule;

if ( exportMode === 'default' ) {
return `${mechanism} ${entryModule.traceExport( 'default' ).getName( false )};`;
}

return entryModule.getExports()
.map( name => {
if ( name[0] === '*' ) {
// export all from external
const id = name.slice( 1 );
const module = bundle.moduleById.get( id );

return `Object.keys(${module.name}).forEach(function (key) { exports[key] = ${module.name}[key]; });`;
}

const prop = name === 'default' ? `['default']` : `.${name}`;
const declaration = entryModule.traceExport( name );

Expand Down
2 changes: 1 addition & 1 deletion src/finalisers/umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default function umd ( bundle, magicString, { exportMode, indentString, i

if ( intro ) magicString.prepend( intro );

const exportBlock = getExportBlock( bundle.entryModule, exportMode );
const exportBlock = getExportBlock( bundle, exportMode );
if ( exportBlock ) magicString.append( '\n\n' + exportBlock );
if ( exportMode === 'named' && options.legacy !== true ) magicString.append( `\n\n${esModuleExport}` );
if ( outro ) magicString.append( outro );
Expand Down
9 changes: 9 additions & 0 deletions test/form/reexports-from-external/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const assert = require( 'assert' );

module.exports = {
description: 're-exports * from external module (#791)',
options: {
external: [ 'external' ],
moduleName: 'myBundle'
}
};
9 changes: 9 additions & 0 deletions test/form/reexports-from-external/_expected/amd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define(['exports', 'external'], function (exports, external) { 'use strict';



Object.keys(external).forEach(function (key) { exports[key] = external[key]; });

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

});
9 changes: 9 additions & 0 deletions test/form/reexports-from-external/_expected/cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

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

var external = require('external');



Object.keys(external).forEach(function (key) { exports[key] = external[key]; });
1 change: 1 addition & 0 deletions test/form/reexports-from-external/_expected/es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from 'external';
8 changes: 8 additions & 0 deletions test/form/reexports-from-external/_expected/iife.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(function (exports,external) {
'use strict';



Object.keys(external).forEach(function (key) { exports[key] = external[key]; });

}((this.myBundle = this.myBundle || {}),external));
11 changes: 11 additions & 0 deletions test/form/reexports-from-external/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -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.myBundle || {}),global.external));
}(this, (function (exports,external) { 'use strict';

Object.keys(external).forEach(function (key) { exports[key] = external[key]; });

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

})));
1 change: 1 addition & 0 deletions test/form/reexports-from-external/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from 'external';
22 changes: 22 additions & 0 deletions test/function/reexports-from-external/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const assert = require( 'assert' );

module.exports = {
description: 're-exports * from external module (#791)',
options: {
external: [ 'external' ]
},
context: {
require ( id ) {
if ( id === 'external' ) {
return {
foo: 1,
bar: 2
};
}
}
},
exports: exports => {
assert.equal( exports.foo, 1 );
assert.equal( exports.bar, 2 );
}
};
1 change: 1 addition & 0 deletions test/function/reexports-from-external/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from 'external';

0 comments on commit 47e0f93

Please sign in to comment.