|
@ -24,8 +24,14 @@ class SyntheticDefaultDeclaration { |
|
|
this.name = reference.name; |
|
|
this.name = reference.name; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bind ( declaration ) { |
|
|
|
|
|
this.original = declaration; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
render () { |
|
|
render () { |
|
|
return this.name; |
|
|
return !this.original || this.original.isReassigned ? |
|
|
|
|
|
this.name : |
|
|
|
|
|
this.original.render(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -120,11 +126,9 @@ export default class Module { |
|
|
isModified: false // in case of `export default foo; foo = somethingElse`
|
|
|
isModified: false // in case of `export default foo; foo = somethingElse`
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
if ( !identifier ) { |
|
|
|
|
|
// create a synthetic declaration
|
|
|
// create a synthetic declaration
|
|
|
this.declarations.default = new SyntheticDefaultDeclaration( node, statement, this.defaultName() ); |
|
|
this.declarations.default = new SyntheticDefaultDeclaration( node, statement, this.defaultName() ); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// export { foo, bar, baz }
|
|
|
// export { foo, bar, baz }
|
|
|
// export var foo = 42;
|
|
|
// export var foo = 42;
|
|
@ -231,6 +235,13 @@ export default class Module { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
bindReferences () { |
|
|
bindReferences () { |
|
|
|
|
|
if ( this.declarations.default ) { |
|
|
|
|
|
if ( this.exports.default.identifier ) { |
|
|
|
|
|
const declaration = this.trace( this.exports.default.identifier ); |
|
|
|
|
|
if ( declaration ) this.declarations.default.bind( declaration ); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
this.statements.forEach( statement => { |
|
|
this.statements.forEach( statement => { |
|
|
statement.references.forEach( reference => { |
|
|
statement.references.forEach( reference => { |
|
|
const declaration = reference.scope.findDeclaration( reference.name ) || |
|
|
const declaration = reference.scope.findDeclaration( reference.name ) || |
|
@ -484,8 +495,16 @@ export default class Module { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
else if ( statement.node.type === 'ExportDefaultDeclaration' ) { |
|
|
else if ( statement.node.type === 'ExportDefaultDeclaration' ) { |
|
|
|
|
|
// TODO unify these
|
|
|
const defaultDeclaration = this.declarations.default; |
|
|
const defaultDeclaration = this.declarations.default; |
|
|
const defaultName = defaultDeclaration.name; |
|
|
|
|
|
|
|
|
// prevent `var foo = foo`
|
|
|
|
|
|
if ( defaultDeclaration.original && !defaultDeclaration.original.isReassigned ) { |
|
|
|
|
|
magicString.remove( statement.start, statement.next ); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const defaultName = defaultDeclaration.render(); |
|
|
|
|
|
|
|
|
// prevent `var undefined = sideEffectyDefault(foo)`
|
|
|
// prevent `var undefined = sideEffectyDefault(foo)`
|
|
|
if ( !defaultDeclaration.isExported && !defaultDeclaration.references.length ) { |
|
|
if ( !defaultDeclaration.isExported && !defaultDeclaration.references.length ) { |
|
@ -530,12 +549,7 @@ export default class Module { |
|
|
|
|
|
|
|
|
const exportDeclaration = this.exports[ name ]; |
|
|
const exportDeclaration = this.exports[ name ]; |
|
|
if ( exportDeclaration ) { |
|
|
if ( exportDeclaration ) { |
|
|
// TODO defaults should not be live...
|
|
|
|
|
|
if ( name === 'default' ) { |
|
|
if ( name === 'default' ) { |
|
|
if ( exportDeclaration.identifier ) { |
|
|
|
|
|
return this.trace( exportDeclaration.identifier ); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return this.declarations.default; |
|
|
return this.declarations.default; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|