Browse Source

Add SyntheticGlobalDeclaration

gh-669
Oskar Segersvärd 9 years ago
parent
commit
1297998ca7
  1. 49
      src/Declaration.js
  2. 14
      src/Module.js
  3. 6
      src/utils/object.js

49
src/Declaration.js

@ -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 ) {
@ -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 );
}
}
@ -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 );
}
}
@ -210,11 +249,13 @@ export class SyntheticNamespaceDeclaration {
}
use () {
// forOwn( this.originals, use );
keys( this.originals ).forEach( name => {
this.originals[ name ].use();
});
this.aliases.forEach( alias => alias.use() );
this.aliases.forEach( use );
}
}

14
src/Module.js

@ -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';
@ -624,7 +628,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 ) {

6
src/utils/object.js

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

Loading…
Cancel
Save