|
@ -5,7 +5,6 @@ import MagicString from 'magic-string'; |
|
|
import analyse from '../ast/analyse'; |
|
|
import analyse from '../ast/analyse'; |
|
|
import { hasOwnProp } from '../utils/object'; |
|
|
import { hasOwnProp } from '../utils/object'; |
|
|
import { sequence } from '../utils/promise'; |
|
|
import { sequence } from '../utils/promise'; |
|
|
import replaceIdentifiers from '../utils/replaceIdentifiers'; |
|
|
|
|
|
|
|
|
|
|
|
const emptyArrayPromise = Promise.resolve([]); |
|
|
const emptyArrayPromise = Promise.resolve([]); |
|
|
|
|
|
|
|
@ -21,7 +20,7 @@ export default class Module { |
|
|
sourceType: 'module' |
|
|
sourceType: 'module' |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
analyse( this.ast, this.code ); |
|
|
analyse( this.ast, this.code, this ); |
|
|
|
|
|
|
|
|
this.nameReplacements = {}; |
|
|
this.nameReplacements = {}; |
|
|
|
|
|
|
|
@ -130,10 +129,12 @@ export default class Module { |
|
|
throw new Error( `Module ${module.path} does not export ${importDeclaration.name} (imported by ${this.path})` ); |
|
|
throw new Error( `Module ${module.path} does not export ${importDeclaration.name} (imported by ${this.path})` ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// we 'suggest' that the bundle use our local name for this import
|
|
|
const globalName = module.nameReplacements[ exportDeclaration.localName ]; |
|
|
// throughout the bundle. If that causes a conflict, we'll end up
|
|
|
if ( globalName ) { |
|
|
// with something slightly different
|
|
|
this.rename( importDeclaration.localName, globalName ); |
|
|
module.nameReplacements[ exportDeclaration.localName ] = importDeclaration.localName; |
|
|
} else { |
|
|
|
|
|
module.rename( exportDeclaration.localName, importDeclaration.localName ); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return module.define( exportDeclaration.localName ); |
|
|
return module.define( exportDeclaration.localName ); |
|
|
}); |
|
|
}); |
|
@ -143,17 +144,13 @@ export default class Module { |
|
|
else if ( name === 'default' && this.exports.default.isDeclaration ) { |
|
|
else if ( name === 'default' && this.exports.default.isDeclaration ) { |
|
|
// We have something like `export default foo` - so we just start again,
|
|
|
// We have something like `export default foo` - so we just start again,
|
|
|
// searching for `foo` instead of default. First, sync up names
|
|
|
// searching for `foo` instead of default. First, sync up names
|
|
|
this.nameReplacements.default = this.exports.default.name; |
|
|
this.rename( 'default', this.exports.default.name ); |
|
|
promise = this.define( this.exports.default.name ); |
|
|
promise = this.define( this.exports.default.name ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
else { |
|
|
else { |
|
|
let statement; |
|
|
let statement; |
|
|
|
|
|
|
|
|
if ( !name ) { |
|
|
|
|
|
console.log( new Error( 'no name' ).stack ); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ( name === 'default' ) { |
|
|
if ( name === 'default' ) { |
|
|
// We have an expression, e.g. `export default 42`. We have
|
|
|
// We have an expression, e.g. `export default 42`. We have
|
|
|
// to assign that expression to a variable
|
|
|
// to assign that expression to a variable
|
|
@ -177,9 +174,6 @@ export default class Module { |
|
|
if ( statement && !statement._imported ) { |
|
|
if ( statement && !statement._imported ) { |
|
|
const nodes = []; |
|
|
const nodes = []; |
|
|
|
|
|
|
|
|
// replace identifiers, as necessary
|
|
|
|
|
|
replaceIdentifiers( statement, statement._source, this.nameReplacements ); |
|
|
|
|
|
|
|
|
|
|
|
const include = statement => { |
|
|
const include = statement => { |
|
|
if ( statement._imported ) return emptyArrayPromise; |
|
|
if ( statement._imported ) return emptyArrayPromise; |
|
|
|
|
|
|
|
@ -214,7 +208,11 @@ export default class Module { |
|
|
return this.definitionPromises[ name ]; |
|
|
return this.definitionPromises[ name ]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
replaceName ( name, replacement ) { |
|
|
rename ( name, replacement ) { |
|
|
|
|
|
if ( hasOwnProp.call( this.nameReplacements, name ) ) { |
|
|
|
|
|
throw new Error( 'Cannot rename an identifier twice' ); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
this.nameReplacements[ name ] = replacement; |
|
|
this.nameReplacements[ name ] = replacement; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |