From 42723e8a8be4b8ae19e09a528d708a323f416710 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Segersv=C3=A4rd?= Date: Thu, 10 Sep 2015 14:44:57 +0200 Subject: [PATCH] Made sure unused, intermediate namepaces are excluded when bundling. Fixes form/namespace-optimization --- src/Statement.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Statement.js b/src/Statement.js index f7b9998..9043630 100644 --- a/src/Statement.js +++ b/src/Statement.js @@ -151,6 +151,7 @@ export default class Statement { let writeDepth = 0; // Used to track + let topName; let currentMemberExpression = null; let namespace = null; @@ -176,7 +177,8 @@ export default class Statement { currentMemberExpression = node; if ( !namespace ) { - const id = this.module.locals.lookup( node.object.name ); + topName = node.object.name; + const id = this.module.locals.lookup( topName ); if ( !id || !id.isModule || id.isExternal ) return; @@ -224,6 +226,17 @@ export default class Statement { if ( !~this.dependantIds.indexOf( id ) ) { this.dependantIds.push( id ); } + + // FIXME: do this better + // If we depend on this name... + if ( this.dependsOn[ topName ] ) { + // ... decrement the count... + if ( !--this.dependsOn[ topName ] ) { + // ... and remove it if the count is 0. + delete this.dependsOn[ topName ]; + } + } + this.namespaceReplacements.push( [ node, id ] ); namespace = null; currentMemberExpression = null; @@ -262,7 +275,11 @@ export default class Statement { const definingScope = scope.findDefiningScope( node.name ); if ( !definingScope || definingScope.depth === 0 ) { - this.dependsOn[ node.name ] = true; + if ( !( node.name in this.dependsOn ) ) { + this.dependsOn[ node.name ] = 0; + } + + this.dependsOn[ node.name ]++; if ( strong ) this.stronglyDependsOn[ node.name ] = true; } }