Skip to content

Commit

Permalink
Merge pull request #472 from rollup/export-global
Browse files Browse the repository at this point in the history
Support export { global }
  • Loading branch information
Rich-Harris committed Jan 31, 2016
2 parents 6200ade + d638fa6 commit 51a87a8
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 18 deletions.
9 changes: 4 additions & 5 deletions src/Bundle.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Promise from 'es6-promise/lib/es6-promise/promise.js';
import MagicString from 'magic-string';
import first from './utils/first.js';
import { blank, keys } from './utils/object.js';
import { blank, forOwn, keys } from './utils/object.js';
import Module from './Module.js';
import ExternalModule from './ExternalModule.js';
import finalisers from './finalisers/index.js';
Expand Down Expand Up @@ -130,15 +130,14 @@ export default class Bundle {

// ensure we don't shadow named external imports, if
// we're creating an ES6 bundle
keys( module.declarations ).forEach( name => {
const declaration = module.declarations[ name ];
forOwn( module.declarations, ( declaration, name ) => {
declaration.setSafeName( getSafeName( name ) );
});
});

this.modules.forEach( module => {
keys( module.declarations ).forEach( originalName => {
const declaration = module.declarations[ originalName ];
forOwn( module.declarations, ( declaration, originalName ) => {
if ( declaration.isGlobal ) return;

if ( originalName === 'default' ) {
if ( declaration.original && !declaration.original.isReassigned ) return;
Expand Down
56 changes: 46 additions & 10 deletions src/Declaration.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { blank, keys } from './utils/object.js';
import { blank, forOwn, keys } from './utils/object.js';
import run from './utils/run.js';
import { SyntheticReference } from './Reference.js';

const use = alias => alias.use();

export default class Declaration {
constructor ( node, isParam, statement ) {
if ( node ) {
Expand Down Expand Up @@ -68,7 +70,7 @@ export default class Declaration {
this.isUsed = true;
if ( this.statement ) this.statement.mark();

this.aliases.forEach( alias => alias.use() );
this.aliases.forEach( use );
}
}

Expand Down Expand Up @@ -129,7 +131,44 @@ export class SyntheticDefaultDeclaration {

if ( this.original ) this.original.use();

this.aliases.forEach( alias => alias.use() );
this.aliases.forEach( use );
}
}

export class SyntheticGlobalDeclaration {
constructor ( name ) {
this.name = name;
this.isExternal = true;
this.isGlobal = true;
this.isReassigned = false;

this.aliases = [];

this.isUsed = false;
}

addAlias ( declaration ) {
this.aliases.push( declaration );
}

addReference ( reference ) {
reference.declaration = this;
if ( reference.isReassignment ) this.isReassigned = true;
}

render () {
return this.name;
}

run () {
return true;
}

use () {
if ( this.isUsed ) return;
this.isUsed = true;

this.aliases.forEach( use );
}
}

Expand Down Expand Up @@ -182,8 +221,8 @@ export class SyntheticNamespaceDeclaration {

// add synthetic references, in case of chained
// namespace imports
keys( this.originals ).forEach( name => {
this.originals[ name ].addReference( new SyntheticReference( name ) );
forOwn( this.originals, ( original, name ) => {
original.addReference( new SyntheticReference( name ) );
});
}

Expand All @@ -210,11 +249,8 @@ export class SyntheticNamespaceDeclaration {
}

use () {
keys( this.originals ).forEach( name => {
this.originals[ name ].use();
});

this.aliases.forEach( alias => alias.use() );
forOwn( this.originals, use );
this.aliases.forEach( use );
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { basename, extname } from './utils/path.js';
import getLocation from './utils/getLocation.js';
import makeLegalIdentifier from './utils/makeLegalIdentifier.js';
import SOURCEMAPPING_URL from './utils/sourceMappingURL.js';
import { SyntheticDefaultDeclaration, SyntheticNamespaceDeclaration } from './Declaration.js';
import {
SyntheticDefaultDeclaration,
SyntheticGlobalDeclaration,
SyntheticNamespaceDeclaration
} from './Declaration.js';
import { isFalsy, isTruthy } from './ast/conditions.js';
import { emptyBlockStatement } from './ast/create.js';
import extractNames from './ast/extractNames.js';
Expand Down Expand Up @@ -633,7 +637,13 @@ export default class Module {

const exportDeclaration = this.exports[ name ];
if ( exportDeclaration ) {
return this.trace( exportDeclaration.localName );
const name = exportDeclaration.localName;
const declaration = this.trace( name );

if ( declaration ) return declaration;

this.bundle.assumedGlobals[ name ] = true;
return ( this.declarations[ name ] = new SyntheticGlobalDeclaration( name ) );
}

for ( let i = 0; i < this.exportAllModules.length; i += 1 ) {
Expand Down
6 changes: 5 additions & 1 deletion src/utils/object.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export const keys = Object.keys;
export const { keys } = Object;

export function blank () {
return Object.create( null );
}

export function forOwn ( object, func ) {
Object.keys( object ).forEach( key => func( object[ key ], key ) );
}
9 changes: 9 additions & 0 deletions test/function/export-global/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var assert = require( 'assert' );

module.exports = {
description: 'any global variables in scope can be re-exported',

exports: function ( exports ) {
assert.equal( exports.Buffer, Buffer );
}
};
1 change: 1 addition & 0 deletions test/function/export-global/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Buffer };

0 comments on commit 51a87a8

Please sign in to comment.