From 0334cc86fe5ae32687f2a9d4163b78be48d6e24b Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sat, 3 Oct 2015 18:39:20 -0400 Subject: [PATCH] more fixes --- src/Bundle.js | 23 ++++++++++++++++++----- src/Module.js | 10 +++++++--- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/Bundle.js b/src/Bundle.js index f126049..5d132b1 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -68,19 +68,32 @@ export default class Bundle { this.markAllModifierStatements(); this.orderedModules = this.sort(); + this.deconflict(); }); } - // TODO would be better to deconflict once, rather than per-render deconflict () { - let nameCount = blank(); + let used = blank(); // ensure no conflicts with globals - keys( this.assumedGlobals ).forEach( name => nameCount[ name ] = 0 ); + keys( this.assumedGlobals ).forEach( name => used[ name ] = 1 ); - let allReplacements = blank(); + this.externalModules.forEach( module => { + // TODO treat external module names/imports as globals + }); - return allReplacements; + this.modules.forEach( module => { + keys( module.declarations ).forEach( originalName => { + const declaration = module.declarations[ originalName ]; + const name = declaration.name; + + if ( used[ name ] ) { + declaration.name = `${name}$${used[name]++}`; + } else { + used[ name ] = 1; + } + }); + }); } fetchModule ( id ) { diff --git a/src/Module.js b/src/Module.js index 4ae634d..f355026 100644 --- a/src/Module.js +++ b/src/Module.js @@ -13,13 +13,15 @@ class SyntheticDefaultDeclaration { this.node = node; this.statement = statement; this.name = name; - + this.references = []; } addReference ( reference ) { reference.declaration = this; this.name = reference.name; + + console.log( 'this.name', this.name ) } } @@ -511,11 +513,13 @@ export default class Module { } else if ( statement.node.type === 'ExportDefaultDeclaration' ) { + const defaultName = this.declarations.default.name; + // anonymous functions should be converted into declarations if ( statement.node.declaration.type === 'FunctionExpression' ) { - magicString.overwrite( statement.node.start, statement.node.declaration.start + 8, `function ${this.defaultName()}` ); + magicString.overwrite( statement.node.start, statement.node.declaration.start + 8, `function ${defaultName}` ); } else { - magicString.overwrite( statement.node.start, statement.node.declaration.start, `var ${this.defaultName()} = ` ); + magicString.overwrite( statement.node.start, statement.node.declaration.start, `var ${defaultName} = ` ); } }