|
@ -3,7 +3,7 @@ import { Promise } from 'sander'; |
|
|
import { parse } from 'acorn'; |
|
|
import { parse } from 'acorn'; |
|
|
import MagicString from 'magic-string'; |
|
|
import MagicString from 'magic-string'; |
|
|
import analyse from '../ast/analyse'; |
|
|
import analyse from '../ast/analyse'; |
|
|
import { hasOwnProp } from '../utils/object'; |
|
|
import { has } from '../utils/object'; |
|
|
import { sequence } from '../utils/promise'; |
|
|
import { sequence } from '../utils/promise'; |
|
|
|
|
|
|
|
|
const emptyArrayPromise = Promise.resolve([]); |
|
|
const emptyArrayPromise = Promise.resolve([]); |
|
@ -20,9 +20,18 @@ export default class Module { |
|
|
sourceType: 'module' |
|
|
sourceType: 'module' |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
this.analyse(); |
|
|
|
|
|
|
|
|
|
|
|
this.deconflict(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
analyse () { |
|
|
analyse( this.ast, this.code, this ); |
|
|
analyse( this.ast, this.code, this ); |
|
|
|
|
|
|
|
|
|
|
|
this.definedNames = this.ast._scope.names.slice(); |
|
|
|
|
|
|
|
|
this.nameReplacements = {}; |
|
|
this.nameReplacements = {}; |
|
|
|
|
|
this.canonicalNames = {}; |
|
|
|
|
|
|
|
|
this.definitions = {}; |
|
|
this.definitions = {}; |
|
|
this.definitionPromises = {}; |
|
|
this.definitionPromises = {}; |
|
@ -34,7 +43,7 @@ export default class Module { |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
Object.keys( statement._modifies ).forEach( name => { |
|
|
Object.keys( statement._modifies ).forEach( name => { |
|
|
if ( !hasOwnProp.call( this.modifications, name ) ) { |
|
|
if ( !has( this.modifications, name ) ) { |
|
|
this.modifications[ name ] = []; |
|
|
this.modifications[ name ] = []; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -46,6 +55,8 @@ export default class Module { |
|
|
this.exports = {}; |
|
|
this.exports = {}; |
|
|
|
|
|
|
|
|
this.ast.body.forEach( node => { |
|
|
this.ast.body.forEach( node => { |
|
|
|
|
|
// import foo from './foo';
|
|
|
|
|
|
// import { bar } from './bar';
|
|
|
if ( node.type === 'ImportDeclaration' ) { |
|
|
if ( node.type === 'ImportDeclaration' ) { |
|
|
const source = node.source.value; |
|
|
const source = node.source.value; |
|
|
|
|
|
|
|
@ -61,6 +72,9 @@ export default class Module { |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// export default function foo () {}
|
|
|
|
|
|
// export default foo;
|
|
|
|
|
|
// export default 42;
|
|
|
else if ( node.type === 'ExportDefaultDeclaration' ) { |
|
|
else if ( node.type === 'ExportDefaultDeclaration' ) { |
|
|
const isDeclaration = /Declaration$/.test( node.declaration.type ); |
|
|
const isDeclaration = /Declaration$/.test( node.declaration.type ); |
|
|
|
|
|
|
|
@ -68,151 +82,214 @@ export default class Module { |
|
|
node, |
|
|
node, |
|
|
name: 'default', |
|
|
name: 'default', |
|
|
localName: isDeclaration ? node.declaration.id.name : 'default', |
|
|
localName: isDeclaration ? node.declaration.id.name : 'default', |
|
|
isDeclaration |
|
|
isDeclaration, |
|
|
|
|
|
module: null // filled in later
|
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// export { foo, bar, baz }
|
|
|
|
|
|
// export var foo = 42;
|
|
|
|
|
|
// export function foo () {}
|
|
|
else if ( node.type === 'ExportNamedDeclaration' ) { |
|
|
else if ( node.type === 'ExportNamedDeclaration' ) { |
|
|
let declaration = node.declaration; |
|
|
if ( node.specifiers.length ) { |
|
|
|
|
|
// export { foo, bar, baz }
|
|
|
|
|
|
node.specifiers.forEach( specifier => { |
|
|
|
|
|
const localName = specifier.local.name; |
|
|
|
|
|
const exportedName = specifier.exported.name; |
|
|
|
|
|
|
|
|
|
|
|
this.exports[ exportedName ] = { |
|
|
|
|
|
localName |
|
|
|
|
|
}; |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
else { |
|
|
|
|
|
let declaration = node.declaration; |
|
|
|
|
|
|
|
|
if ( declaration ) { |
|
|
|
|
|
let name; |
|
|
let name; |
|
|
|
|
|
|
|
|
if ( declaration.type === 'VariableDeclaration' ) { |
|
|
if ( declaration.type === 'VariableDeclaration' ) { |
|
|
// `export var foo = /*...*/`
|
|
|
// export var foo = 42
|
|
|
name = declaration.declarations[0].id.name; |
|
|
name = declaration.declarations[0].id.name; |
|
|
} else { |
|
|
} else { |
|
|
// `export function foo () {/*...*/}`
|
|
|
// export function foo () {}
|
|
|
name = declaration.id.name; |
|
|
name = declaration.id.name; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
this.exports[ name ] = { |
|
|
this.exports[ name ] = { |
|
|
localName: name, |
|
|
localName: name, |
|
|
expression: node.declaration |
|
|
expression: declaration |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
else if ( node.specifiers ) { |
|
|
getCanonicalName ( name ) { |
|
|
node.specifiers.forEach( specifier => { |
|
|
if ( has( this.imports, name ) ) { |
|
|
const localName = specifier.local.name; |
|
|
const importDeclaration = this.imports[ name ]; |
|
|
const exportedName = specifier.exported.name; |
|
|
const module = importDeclaration.module; |
|
|
|
|
|
const exportDeclaration = module.exports[ importDeclaration.name ]; |
|
|
|
|
|
|
|
|
this.exports[ exportedName ] = { |
|
|
return module.getCanonicalName( exportDeclaration.localName ); |
|
|
localName |
|
|
} |
|
|
}; |
|
|
|
|
|
}); |
|
|
if ( name === 'default' ) { |
|
|
} |
|
|
name = this.defaultExportName; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return has( this.canonicalNames, name ) ? this.canonicalNames[ name ] : name; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
deconflict () { |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
define ( name ) { |
|
|
define ( name ) { |
|
|
// shortcut cycles. TODO this won't work everywhere...
|
|
|
// shortcut cycles. TODO this won't work everywhere...
|
|
|
if ( hasOwnProp.call( this.definitionPromises, name ) ) { |
|
|
if ( has( this.definitionPromises, name ) ) { |
|
|
return emptyArrayPromise; |
|
|
return emptyArrayPromise; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if ( !hasOwnProp.call( this.definitionPromises, name ) ) { |
|
|
let promise; |
|
|
let promise; |
|
|
|
|
|
|
|
|
|
|
|
// The definition for this name is in a different module
|
|
|
// The definition for this name is in a different module
|
|
|
if ( hasOwnProp.call( this.imports, name ) ) { |
|
|
if ( has( this.imports, name ) ) { |
|
|
const importDeclaration = this.imports[ name ]; |
|
|
const importDeclaration = this.imports[ name ]; |
|
|
const path = resolve( dirname( this.path ), importDeclaration.source ) + '.js'; |
|
|
const path = resolve( dirname( this.path ), importDeclaration.source ) + '.js'; |
|
|
|
|
|
|
|
|
promise = this.bundle.fetchModule( path ) |
|
|
promise = this.bundle.fetchModule( path ) |
|
|
.then( module => { |
|
|
.then( module => { |
|
|
const exportDeclaration = module.exports[ importDeclaration.name ]; |
|
|
importDeclaration.module = module; |
|
|
|
|
|
|
|
|
if ( !exportDeclaration ) { |
|
|
const exportDeclaration = module.exports[ importDeclaration.name ]; |
|
|
throw new Error( `Module ${module.path} does not export ${importDeclaration.name} (imported by ${this.path})` ); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const globalName = module.nameReplacements[ exportDeclaration.localName ]; |
|
|
if ( !exportDeclaration ) { |
|
|
if ( globalName ) { |
|
|
throw new Error( `Module ${module.path} does not export ${importDeclaration.name} (imported by ${this.path})` ); |
|
|
this.rename( importDeclaration.localName, globalName ); |
|
|
} |
|
|
} else { |
|
|
|
|
|
module.rename( exportDeclaration.localName, importDeclaration.localName ); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return module.define( exportDeclaration.localName ); |
|
|
if ( importDeclaration.name === 'default' ) { |
|
|
}); |
|
|
module.suggestDefaultName( importDeclaration.localName ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// The definition is in this module
|
|
|
// const globalName = module.nameReplacements[ exportDeclaration.localName ];
|
|
|
else if ( name === 'default' && this.exports.default.isDeclaration ) { |
|
|
// if ( globalName ) {
|
|
|
// We have something like `export default foo` - so we just start again,
|
|
|
// this.rename( importDeclaration.localName, globalName, true );
|
|
|
// searching for `foo` instead of default. First, sync up names
|
|
|
// } else {
|
|
|
this.rename( 'default', this.exports.default.name ); |
|
|
// module.rename( exportDeclaration.localName, importDeclaration.localName );
|
|
|
promise = this.define( this.exports.default.name ); |
|
|
// }
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
else { |
|
|
return module.define( exportDeclaration.localName ); |
|
|
let statement; |
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if ( name === 'default' ) { |
|
|
// The definition is in this module
|
|
|
// We have an expression, e.g. `export default 42`. We have
|
|
|
else if ( name === 'default' && this.exports.default.isDeclaration ) { |
|
|
// to assign that expression to a variable
|
|
|
// We have something like `export default foo` - so we just start again,
|
|
|
const replacement = this.nameReplacements.default; |
|
|
// searching for `foo` instead of default. First, sync up names
|
|
|
|
|
|
this.rename( 'default', this.exports.default.name ); |
|
|
|
|
|
promise = this.define( this.exports.default.name ); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
statement = this.exports.default.node; |
|
|
else { |
|
|
|
|
|
let statement; |
|
|
|
|
|
|
|
|
if ( !statement._imported ) { |
|
|
if ( name === 'default' ) { |
|
|
statement._source.overwrite( statement.start, statement.declaration.start, `var ${replacement} = ` ) |
|
|
// We have an expression, e.g. `export default 42`. We have
|
|
|
|
|
|
// to assign that expression to a variable
|
|
|
|
|
|
const replacement = this.defaultExportName; |
|
|
|
|
|
|
|
|
|
|
|
statement = this.exports.default.node; |
|
|
|
|
|
|
|
|
|
|
|
if ( !statement._imported ) { |
|
|
|
|
|
// if we have `export default foo`, we don't want to turn it into `var foo = foo`
|
|
|
|
|
|
// - we want to remove it altogether (but keep the statement, so we can include
|
|
|
|
|
|
// its dependencies). TODO is there an easier way to do this?
|
|
|
|
|
|
const shouldRemove = statement.declaration.type === 'Identifier' && statement.declaration.name === replacement; |
|
|
|
|
|
|
|
|
|
|
|
if ( shouldRemove ) { |
|
|
|
|
|
statement._source.remove( statement.start, statement.end ); |
|
|
|
|
|
} else { |
|
|
|
|
|
statement._source.overwrite( statement.start, statement.declaration.start, `var ${replacement} = ` ); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
else { |
|
|
else { |
|
|
statement = this.definitions[ name ]; |
|
|
statement = this.definitions[ name ]; |
|
|
|
|
|
|
|
|
if ( statement && /^Export/.test( statement.type ) ) { |
|
|
if ( statement && /^Export/.test( statement.type ) ) { |
|
|
statement._source.remove( statement.start, statement.declaration.start ); |
|
|
statement._source.remove( statement.start, statement.declaration.start ); |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if ( statement && !statement._imported ) { |
|
|
if ( statement && !statement._imported ) { |
|
|
const nodes = []; |
|
|
const nodes = []; |
|
|
|
|
|
|
|
|
const include = statement => { |
|
|
const include = statement => { |
|
|
if ( statement._imported ) return emptyArrayPromise; |
|
|
if ( statement._imported ) return emptyArrayPromise; |
|
|
|
|
|
|
|
|
const dependencies = Object.keys( statement._dependsOn ); |
|
|
const dependencies = Object.keys( statement._dependsOn ); |
|
|
|
|
|
|
|
|
return sequence( dependencies, name => this.define( name ) ) |
|
|
return sequence( dependencies, name => this.define( name ) ) |
|
|
.then( definitions => { |
|
|
.then( definitions => { |
|
|
definitions.forEach( definition => nodes.push.apply( nodes, definition ) ); |
|
|
definitions.forEach( definition => nodes.push.apply( nodes, definition ) ); |
|
|
}) |
|
|
}) |
|
|
.then( () => { |
|
|
.then( () => { |
|
|
statement._imported = true; |
|
|
statement._imported = true; |
|
|
nodes.push( statement ); |
|
|
nodes.push( statement ); |
|
|
|
|
|
|
|
|
const modifications = hasOwnProp.call( this.modifications, name ) && this.modifications[ name ]; |
|
|
const modifications = has( this.modifications, name ) && this.modifications[ name ]; |
|
|
|
|
|
|
|
|
if ( modifications ) { |
|
|
if ( modifications ) { |
|
|
return sequence( modifications, include ); |
|
|
return sequence( modifications, include ); |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
.then( () => { |
|
|
.then( () => { |
|
|
return nodes; |
|
|
return nodes; |
|
|
}); |
|
|
}); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
promise = include( statement ); |
|
|
promise = include( statement ); |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
this.definitionPromises[ name ] = promise || emptyArrayPromise; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.definitionPromises[ name ] = promise || emptyArrayPromise; |
|
|
return this.definitionPromises[ name ]; |
|
|
return this.definitionPromises[ name ]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
rename ( name, replacement ) { |
|
|
rename ( name, replacement ) { |
|
|
if ( hasOwnProp.call( this.nameReplacements, name ) ) { |
|
|
this.canonicalNames[ name ] = replacement; |
|
|
throw new Error( 'Cannot rename an identifier twice' ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.nameReplacements[ name ] = replacement; |
|
|
suggestDefaultName ( name ) { |
|
|
|
|
|
if ( !this.defaultExportName ) { |
|
|
|
|
|
this.defaultExportName = name; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// rename ( name, replacement, force ) {
|
|
|
|
|
|
// if ( has( this.nameReplacements, name ) ) {
|
|
|
|
|
|
// throw new Error( 'Cannot rename an identifier twice' );
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// if ( !force ) {
|
|
|
|
|
|
// replacement = this.bundle.getSafeReplacement( replacement, this );
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// if ( name === replacement ) {
|
|
|
|
|
|
// return;
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// console.log( 'renamining %s : %s -> %s (%s)', this.relativePath, name, replacement, force );
|
|
|
|
|
|
|
|
|
|
|
|
// const index = this.definedNames.indexOf( name );
|
|
|
|
|
|
// if ( ~index ) {
|
|
|
|
|
|
// this.definedNames[ index ] = replacement;
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// this.nameReplacements[ name ] = replacement;
|
|
|
|
|
|
// }
|
|
|
} |
|
|
} |