Skip to content

Commit

Permalink
Merge pull request #1543 from rollup/export-name-from
Browse files Browse the repository at this point in the history
Export name from
  • Loading branch information
Rich-Harris committed Aug 12, 2017
2 parents b7e1d49 + 20bff7d commit 6edcd30
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -66,7 +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.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
11 changes: 3 additions & 8 deletions rollup.config.js
Expand Up @@ -2,7 +2,7 @@ import { readFileSync } from 'fs';
import buble from 'rollup-plugin-buble';
import nodeResolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import replace from 'rollup-plugin-replace';
import json from 'rollup-plugin-json';

var pkg = JSON.parse( readFileSync( 'package.json', 'utf-8' ) );

Expand All @@ -22,6 +22,8 @@ var banner = readFileSync( 'src/banner.js', 'utf-8' )
export default {
entry: 'src/node-entry.js',
plugins: [
json(),

buble({
include: [ 'src/**', 'node_modules/acorn/**' ],
target: {
Expand All @@ -33,13 +35,6 @@ export default {
jsnext: true
}),

replace({
include: 'src/rollup.js',
delimiters: [ '<@', '@>' ],
sourceMap: true,
values: { VERSION: pkg.version }
}),

commonjs()
],
external: [
Expand Down
3 changes: 2 additions & 1 deletion src/browser-entry.js
@@ -1 +1,2 @@
export { default as rollup } from './rollup/index.js';
export { default as rollup } from './rollup/index.js';
export { version as VERSION } from '../package.json';
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
3 changes: 2 additions & 1 deletion src/node-entry.js
@@ -1,2 +1,3 @@
export { default as rollup } from './rollup/index.js';
export { default as watch } from './watch/index.js';
export { default as watch } from './watch/index.js';
export { version as VERSION } from '../package.json';
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 6edcd30

Please sign in to comment.