diff --git a/src/Bundle.js b/src/Bundle.js index b41a6bd..1b9834e 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -58,9 +58,7 @@ export default class Bundle { const declaration = entryModule.traceExport( name ); declaration.isExported = true; - if ( declaration.statement ) { - declaration.statement.mark(); - } + if ( declaration.statement ) declaration.use(); }); let settled = false; diff --git a/src/ExternalModule.js b/src/ExternalModule.js index 81a1a17..f0d3408 100644 --- a/src/ExternalModule.js +++ b/src/ExternalModule.js @@ -6,8 +6,6 @@ class ExternalDeclaration { this.module = module; this.name = name; this.isExternal = true; - - this.references = []; } addReference ( reference ) { @@ -31,6 +29,10 @@ class ExternalDeclaration { return es6 ? this.name : `${this.module.name}.${this.name}`; } + + use () { + // noop? + } } export default class ExternalModule { diff --git a/src/Module.js b/src/Module.js index dd67cf0..18670b2 100644 --- a/src/Module.js +++ b/src/Module.js @@ -14,12 +14,11 @@ class SyntheticDefaultDeclaration { this.statement = statement; this.name = name; - this.references = []; + this.original = null; + this.isExported = false; } addReference ( reference ) { - this.references.push( reference ); - reference.declaration = this; this.name = reference.name; } @@ -33,6 +32,11 @@ class SyntheticDefaultDeclaration { this.name : this.original.render(); } + + use () { + this.isUsed = true; + this.statement.mark(); + } } class SyntheticNamespaceDeclaration { @@ -40,7 +44,6 @@ class SyntheticNamespaceDeclaration { this.module = module; this.name = null; - this.references = []; this.needsNamespaceBlock = false; this.originals = blank(); @@ -72,12 +75,10 @@ class SyntheticNamespaceDeclaration { keys( this.originals ).forEach( name => { const original = this.originals[ name ]; - original.statement.mark(); + original.use(); }); } - this.references.push( reference ); - reference.declaration = this; this.name = reference.name; } @@ -99,6 +100,10 @@ class SyntheticNamespaceDeclaration { render () { return this.name; } + + use () { + // noop + } } export default class Module { @@ -581,7 +586,7 @@ export default class Module { const defaultName = defaultDeclaration.render(); // prevent `var undefined = sideEffectyDefault(foo)` - if ( !defaultDeclaration.isExported && !defaultDeclaration.references.length ) { + if ( !defaultDeclaration.isExported && !defaultDeclaration.isUsed ) { magicString.remove( statement.start, statement.node.declaration.start ); return; } diff --git a/src/Statement.js b/src/Statement.js index bf35eba..27f1f36 100644 --- a/src/Statement.js +++ b/src/Statement.js @@ -155,9 +155,7 @@ export default class Statement { this.isIncluded = true; this.references.forEach( reference => { - if ( reference.declaration && reference.declaration.statement ) { - reference.declaration.statement.mark(); - } + if ( reference.declaration ) reference.declaration.use(); }); } diff --git a/src/ast/Scope.js b/src/ast/Scope.js index 2e6a543..64c90be 100644 --- a/src/ast/Scope.js +++ b/src/ast/Scope.js @@ -35,7 +35,6 @@ function extractNames ( param ) { class Declaration { constructor () { - this.references = []; this.statement = null; this.name = null; @@ -55,6 +54,11 @@ class Declaration { return `exports.${this.name}`; } + + use () { + this.isUsed = true; + if ( this.statement ) this.statement.mark(); + } } export default class Scope {