Browse Source

Merge pull request #1226 from rollup/gh-791

implement export * from external
gh-1187
Rich Harris 8 years ago
committed by GitHub
parent
commit
47e0f93a6e
  1. 5
      src/Module.js
  2. 2
      src/finalisers/amd.js
  3. 2
      src/finalisers/cjs.js
  4. 37
      src/finalisers/es.js
  5. 2
      src/finalisers/iife.js
  6. 12
      src/finalisers/shared/getExportBlock.js
  7. 2
      src/finalisers/umd.js
  8. 9
      test/form/reexports-from-external/_config.js
  9. 9
      test/form/reexports-from-external/_expected/amd.js
  10. 9
      test/form/reexports-from-external/_expected/cjs.js
  11. 1
      test/form/reexports-from-external/_expected/es.js
  12. 8
      test/form/reexports-from-external/_expected/iife.js
  13. 11
      test/form/reexports-from-external/_expected/umd.js
  14. 1
      test/form/reexports-from-external/main.js
  15. 22
      test/function/reexports-from-external/_config.js
  16. 1
      test/function/reexports-from-external/main.js

5
src/Module.js

@ -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;

2
src/finalisers/amd.js

@ -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 );

2
src/finalisers/cjs.js

@ -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 );

37
src/finalisers/es.js

@ -6,6 +6,10 @@ 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];
@ -13,11 +17,16 @@ export default function es ( bundle, magicString, { intro, outro } ) {
.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 ) {
@ -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(', ')} };` : '';
@ -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();

2
src/finalisers/iife.js

@ -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 );

12
src/finalisers/shared/getExportBlock.js

@ -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 );

2
src/finalisers/umd.js

@ -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 );

9
test/form/reexports-from-external/_config.js

@ -0,0 +1,9 @@
const assert = require( 'assert' );
module.exports = {
description: 're-exports * from external module (#791)',
options: {
external: [ 'external' ],
moduleName: 'myBundle'
}
};

9
test/form/reexports-from-external/_expected/amd.js

@ -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
test/form/reexports-from-external/_expected/cjs.js

@ -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
test/form/reexports-from-external/_expected/es.js

@ -0,0 +1 @@
export * from 'external';

8
test/form/reexports-from-external/_expected/iife.js

@ -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
test/form/reexports-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.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
test/form/reexports-from-external/main.js

@ -0,0 +1 @@
export * from 'external';

22
test/function/reexports-from-external/_config.js

@ -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
test/function/reexports-from-external/main.js

@ -0,0 +1 @@
export * from 'external';
Loading…
Cancel
Save