|
|
@ -1,3 +1,4 @@ |
|
|
|
import { basename, extname } from './utils/path'; |
|
|
|
import { parse } from 'acorn'; |
|
|
|
import MagicString from 'magic-string'; |
|
|
|
import Statement from './Statement'; |
|
|
@ -6,21 +7,45 @@ import { blank, keys } from './utils/object'; |
|
|
|
import getLocation from './utils/getLocation'; |
|
|
|
import makeLegalIdentifier from './utils/makeLegalIdentifier'; |
|
|
|
|
|
|
|
function deconflict ( name, names ) { |
|
|
|
while ( name in names ) { |
|
|
|
name = `_${name}`; |
|
|
|
function isEmptyExportedVarDeclaration ( node, exports, toExport ) { |
|
|
|
if ( node.type !== 'VariableDeclaration' || node.declarations[0].init ) return false; |
|
|
|
|
|
|
|
const name = node.declarations[0].id.name; |
|
|
|
|
|
|
|
const id = exports.lookup( name ); |
|
|
|
|
|
|
|
return id && id.name in toExport; |
|
|
|
} |
|
|
|
|
|
|
|
function removeSourceMappingURLComments ( source, magicString ) { |
|
|
|
const pattern = /\/\/#\s+sourceMappingURL=.+\n?/g; |
|
|
|
let match; |
|
|
|
while ( match = pattern.exec( source ) ) { |
|
|
|
magicString.remove( match.index, match.index + match[0].length ); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return name; |
|
|
|
function assign ( target, source ) { |
|
|
|
for ( var key in source ) target[ key ] = source[ key ]; |
|
|
|
} |
|
|
|
|
|
|
|
function isEmptyExportedVarDeclaration ( node, allBundleExports, moduleReplacements ) { |
|
|
|
if ( node.type !== 'VariableDeclaration' || node.declarations[0].init ) return false; |
|
|
|
class Id { |
|
|
|
constructor ( module, name, statement ) { |
|
|
|
this.originalName = this.name = name; |
|
|
|
this.module = module; |
|
|
|
this.statement = statement; |
|
|
|
|
|
|
|
const name = node.declarations[0].id.name; |
|
|
|
const canonicalName = moduleReplacements[ name ] || name; |
|
|
|
this.modifierStatements = []; |
|
|
|
|
|
|
|
// modifiers
|
|
|
|
this.isUsed = false; |
|
|
|
} |
|
|
|
|
|
|
|
return canonicalName in allBundleExports; |
|
|
|
mark () { |
|
|
|
this.isUsed = true; |
|
|
|
this.statement.mark(); |
|
|
|
this.modifierStatements.forEach( stmt => stmt.mark() ); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
export default class Module { |
|
|
@ -29,6 +54,16 @@ export default class Module { |
|
|
|
|
|
|
|
this.bundle = bundle; |
|
|
|
this.id = id; |
|
|
|
this.module = this; |
|
|
|
this.isModule = true; |
|
|
|
|
|
|
|
// Implement Identifier interface.
|
|
|
|
this.name = makeLegalIdentifier( basename( id ).slice( 0, -extname( id ).length ) ); |
|
|
|
|
|
|
|
// HACK: If `id` isn't a path, the above code yields the empty string.
|
|
|
|
if ( !this.name ) { |
|
|
|
this.name = makeLegalIdentifier( id ); |
|
|
|
} |
|
|
|
|
|
|
|
// By default, `id` is the filename. Custom resolvers and loaders
|
|
|
|
// can change that, but it makes sense to use it for the source filename
|
|
|
@ -36,41 +71,58 @@ export default class Module { |
|
|
|
filename: id |
|
|
|
}); |
|
|
|
|
|
|
|
// remove existing sourceMappingURL comments
|
|
|
|
const pattern = /\/\/#\s+sourceMappingURL=.+\n?/g; |
|
|
|
let match; |
|
|
|
while ( match = pattern.exec( source ) ) { |
|
|
|
this.magicString.remove( match.index, match.index + match[0].length ); |
|
|
|
} |
|
|
|
removeSourceMappingURLComments( source, this.magicString ); |
|
|
|
|
|
|
|
this.suggestedNames = blank(); |
|
|
|
this.comments = []; |
|
|
|
|
|
|
|
this.statements = this.parse( ast ); |
|
|
|
|
|
|
|
// all dependencies
|
|
|
|
this.dependencies = []; |
|
|
|
this.resolvedIds = blank(); |
|
|
|
this.boundImportSpecifiers = false; |
|
|
|
|
|
|
|
// imports and exports, indexed by local name
|
|
|
|
this.imports = blank(); |
|
|
|
this.exports = blank(); |
|
|
|
this.reexports = blank(); |
|
|
|
this.exportDelegates = blank(); |
|
|
|
// Virtual scopes for the local and exported names.
|
|
|
|
this.locals = bundle.scope.virtual( true ); |
|
|
|
this.exports = bundle.scope.virtual( false ); |
|
|
|
|
|
|
|
this.exportAlls = []; |
|
|
|
const { reference, inScope } = this.exports; |
|
|
|
|
|
|
|
this.exports.reference = name => { |
|
|
|
// If we have it, grab it.
|
|
|
|
if ( inScope.call( this.exports, name ) ) { |
|
|
|
return reference.call( this.exports, name ); |
|
|
|
} |
|
|
|
|
|
|
|
// ... otherwise search exportAlls
|
|
|
|
for ( let i = 0; i < this.exportAlls.length; i += 1 ) { |
|
|
|
const module = this.exportAlls[i]; |
|
|
|
if ( module.exports.inScope( name ) ) { |
|
|
|
return module.exports.reference( name ); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
this.replacements = blank(); |
|
|
|
// throw new Error( `The name "${name}" is never exported (from ${this.id})!` );
|
|
|
|
return reference.call( this.exports, name ); |
|
|
|
}; |
|
|
|
|
|
|
|
this.exports.inScope = name => { |
|
|
|
if ( inScope.call( this.exports, name ) ) return true; |
|
|
|
|
|
|
|
return this.exportAlls.some( module => module.exports.inScope( name ) ); |
|
|
|
}; |
|
|
|
|
|
|
|
// Create a unique virtual scope for references to the module.
|
|
|
|
// const unique = bundle.scope.virtual();
|
|
|
|
// unique.define( this.name, this );
|
|
|
|
// this.reference = unique.reference( this.name );
|
|
|
|
|
|
|
|
this.exportAlls = []; |
|
|
|
|
|
|
|
this.reassignments = []; |
|
|
|
|
|
|
|
this.marked = blank(); |
|
|
|
this.definitions = blank(); |
|
|
|
this.definitionPromises = blank(); |
|
|
|
this.modifications = blank(); |
|
|
|
// TODO: change to false, and detect when it's necessary.
|
|
|
|
this.needsDynamicAccess = false; |
|
|
|
|
|
|
|
this.analyse(); |
|
|
|
this.dependencies = this.collectDependencies(); |
|
|
|
} |
|
|
|
|
|
|
|
addExport ( statement ) { |
|
|
@ -79,24 +131,24 @@ export default class Module { |
|
|
|
|
|
|
|
// export { name } from './other'
|
|
|
|
if ( source ) { |
|
|
|
if ( !~this.dependencies.indexOf( source ) ) this.dependencies.push( source ); |
|
|
|
const module = this.getModule( source ); |
|
|
|
|
|
|
|
if ( node.type === 'ExportAllDeclaration' ) { |
|
|
|
// Store `export * from '...'` statements in an array of delegates.
|
|
|
|
// When an unknown import is encountered, we see if one of them can satisfy it.
|
|
|
|
this.exportAlls.push({ |
|
|
|
statement, |
|
|
|
source |
|
|
|
}); |
|
|
|
|
|
|
|
if ( module.isExternal ) { |
|
|
|
throw new Error( `Cannot trace 'export *' references through external modules.` ); |
|
|
|
} |
|
|
|
|
|
|
|
this.exportAlls.push( module ); |
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
node.specifiers.forEach( specifier => { |
|
|
|
this.reexports[ specifier.exported.name ] = { |
|
|
|
source, |
|
|
|
localName: specifier.local.name, |
|
|
|
module: null // filled in later
|
|
|
|
}; |
|
|
|
// Bind the export of this module, to the export of the other.
|
|
|
|
this.exports.bind( specifier.exported.name, |
|
|
|
module.exports.reference( specifier.local.name ) ); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
@ -113,16 +165,18 @@ export default class Module { |
|
|
|
node.declaration.type === 'Identifier' ? |
|
|
|
node.declaration.name : |
|
|
|
null; |
|
|
|
const name = identifier || this.name; |
|
|
|
|
|
|
|
// Always define a new `Identifier` for the default export.
|
|
|
|
const id = new Id( this, name, statement ); |
|
|
|
|
|
|
|
this.exports.default = { |
|
|
|
statement, |
|
|
|
name: 'default', |
|
|
|
localName: identifier || 'default', |
|
|
|
identifier, |
|
|
|
isDeclaration, |
|
|
|
isAnonymous, |
|
|
|
isModified: false // in case of `export default foo; foo = somethingElse`
|
|
|
|
}; |
|
|
|
// Keep the identifier name, if one exists.
|
|
|
|
// We can optimize the newly created default `Identifier` away,
|
|
|
|
// if it is never modified.
|
|
|
|
// in case of `export default foo; foo = somethingElse`
|
|
|
|
assign( id, { isDeclaration, isAnonymous, identifier } ); |
|
|
|
|
|
|
|
this.exports.define( 'default', id ); |
|
|
|
} |
|
|
|
|
|
|
|
// export { foo, bar, baz }
|
|
|
@ -135,11 +189,7 @@ export default class Module { |
|
|
|
const localName = specifier.local.name; |
|
|
|
const exportedName = specifier.exported.name; |
|
|
|
|
|
|
|
this.exports[ exportedName ] = { |
|
|
|
statement, |
|
|
|
localName, |
|
|
|
exportedName |
|
|
|
}; |
|
|
|
this.exports.bind( exportedName, this.locals.reference( localName ) ); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
@ -156,40 +206,49 @@ export default class Module { |
|
|
|
name = declaration.id.name; |
|
|
|
} |
|
|
|
|
|
|
|
this.exports[ name ] = { |
|
|
|
statement, |
|
|
|
localName: name, |
|
|
|
expression: declaration |
|
|
|
}; |
|
|
|
this.locals.define( name, new Id( this, name, statement ) ); |
|
|
|
this.exports.bind( name, this.locals.reference( name ) ); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
addImport ( statement ) { |
|
|
|
const node = statement.node; |
|
|
|
const source = node.source.value; |
|
|
|
|
|
|
|
if ( !~this.dependencies.indexOf( source ) ) this.dependencies.push( source ); |
|
|
|
const module = this.getModule( node.source.value ); |
|
|
|
|
|
|
|
node.specifiers.forEach( specifier => { |
|
|
|
const isDefault = specifier.type === 'ImportDefaultSpecifier'; |
|
|
|
const isNamespace = specifier.type === 'ImportNamespaceSpecifier'; |
|
|
|
|
|
|
|
const localName = specifier.local.name; |
|
|
|
const name = isDefault ? 'default' : isNamespace ? '*' : specifier.imported.name; |
|
|
|
|
|
|
|
if ( this.imports[ localName ] ) { |
|
|
|
if ( this.locals.defines( localName ) ) { |
|
|
|
const err = new Error( `Duplicated import '${localName}'` ); |
|
|
|
err.file = this.id; |
|
|
|
err.loc = getLocation( this.source, specifier.start ); |
|
|
|
throw err; |
|
|
|
} |
|
|
|
|
|
|
|
this.imports[ localName ] = { |
|
|
|
source, |
|
|
|
name, |
|
|
|
localName |
|
|
|
}; |
|
|
|
if ( isNamespace ) { |
|
|
|
// If it's a namespace import, we bind the localName to the module itself.
|
|
|
|
module.needsAll = true; |
|
|
|
module.name = localName; |
|
|
|
this.locals.bind( localName, module ); |
|
|
|
} else { |
|
|
|
const name = isDefault ? 'default' : specifier.imported.name; |
|
|
|
|
|
|
|
this.locals.bind( localName, module.exports.reference( name ) ); |
|
|
|
|
|
|
|
// For compliance with earlier Rollup versions.
|
|
|
|
// If the module is external, and we access the default.
|
|
|
|
// Rewrite the module name, and the default name to the
|
|
|
|
// `localName` we use for it.
|
|
|
|
if ( module.isExternal && isDefault ) { |
|
|
|
const id = module.exports.lookup( name ); |
|
|
|
module.name = id.name = localName; |
|
|
|
id.name += '__default'; |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
@ -203,11 +262,7 @@ export default class Module { |
|
|
|
|
|
|
|
// consolidate names that are defined/modified in this module
|
|
|
|
keys( statement.defines ).forEach( name => { |
|
|
|
this.definitions[ name ] = statement; |
|
|
|
}); |
|
|
|
|
|
|
|
keys( statement.modifies ).forEach( name => { |
|
|
|
( this.modifications[ name ] || ( this.modifications[ name ] = [] ) ).push( statement ); |
|
|
|
this.locals.define( name, new Id( this, name, statement ) ); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
@ -236,39 +291,39 @@ export default class Module { |
|
|
|
}); |
|
|
|
|
|
|
|
keys( statement.dependsOn ).forEach( name => { |
|
|
|
if ( !this.definitions[ name ] && !this.imports[ name ] ) { |
|
|
|
this.bundle.assumedGlobals[ name ] = true; |
|
|
|
// For each name we depend on that isn't in scope,
|
|
|
|
// add a new global and bind the local name to it.
|
|
|
|
if ( !this.locals.inScope( name ) ) { |
|
|
|
this.bundle.globals.define( name, { |
|
|
|
originalName: name, |
|
|
|
name, |
|
|
|
mark () {} |
|
|
|
}); |
|
|
|
this.locals.bind( name, this.bundle.globals.reference( name ) ); |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
bindImportSpecifiers () { |
|
|
|
if ( this.boundImportSpecifiers ) return; |
|
|
|
this.boundImportSpecifiers = true; |
|
|
|
|
|
|
|
[ this.imports, this.reexports ].forEach( specifiers => { |
|
|
|
keys( specifiers ).forEach( name => { |
|
|
|
const specifier = specifiers[ name ]; |
|
|
|
// OPTIMIZATION!
|
|
|
|
// If we have a default export and it's value is never modified,
|
|
|
|
// bind to it directly.
|
|
|
|
const def = this.exports.lookup( 'default' ); |
|
|
|
if ( def && !def.isModified && def.identifier ) { |
|
|
|
this.exports.bind( 'default', this.locals.reference( def.identifier ) ); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if ( specifier.module ) return; |
|
|
|
// Returns the set of imported module ids by going through all import/exports statements.
|
|
|
|
collectDependencies () { |
|
|
|
const importedModules = blank(); |
|
|
|
|
|
|
|
const id = this.resolvedIds[ specifier.source ]; |
|
|
|
specifier.module = this.bundle.moduleById[ id ]; |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
this.exportAlls.forEach( delegate => { |
|
|
|
const id = this.resolvedIds[ delegate.source ]; |
|
|
|
delegate.module = this.bundle.moduleById[ id ]; |
|
|
|
this.statements.forEach( statement => { |
|
|
|
if ( statement.isImportDeclaration || ( statement.isExportDeclaration && statement.node.source ) ) { |
|
|
|
importedModules[ statement.node.source.value ] = true; |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
this.dependencies.forEach( source => { |
|
|
|
const id = this.resolvedIds[ source ]; |
|
|
|
const module = this.bundle.moduleById[ id ]; |
|
|
|
|
|
|
|
if ( !module.isExternal ) module.bindImportSpecifiers(); |
|
|
|
}); |
|
|
|
return keys( importedModules ); |
|
|
|
} |
|
|
|
|
|
|
|
consolidateDependencies () { |
|
|
@ -284,8 +339,7 @@ export default class Module { |
|
|
|
this.statements.forEach( statement => { |
|
|
|
if ( statement.isImportDeclaration && !statement.node.specifiers.length ) { |
|
|
|
// include module for its side-effects
|
|
|
|
const id = this.resolvedIds[ statement.node.source.value ]; |
|
|
|
const module = this.bundle.moduleById[ id ]; |
|
|
|
const module = this.getModule( statement.node.source.value ); |
|
|
|
|
|
|
|
if ( !module.isExternal ) strongDependencies[ module.id ] = module; |
|
|
|
} |
|
|
@ -293,17 +347,11 @@ export default class Module { |
|
|
|
else if ( statement.isReexportDeclaration ) { |
|
|
|
if ( statement.node.specifiers ) { |
|
|
|
statement.node.specifiers.forEach( specifier => { |
|
|
|
let reexport; |
|
|
|
|
|
|
|
let module = this; |
|
|
|
let name = specifier.exported.name; |
|
|
|
while ( !module.isExternal && module.reexports[ name ] && module.reexports[ name ].isUsed ) { |
|
|
|
reexport = module.reexports[ name ]; |
|
|
|
module = reexport.module; |
|
|
|
name = reexport.localName; |
|
|
|
} |
|
|
|
|
|
|
|
addDependency( strongDependencies, reexport ); |
|
|
|
let id = this.exports.lookup( name ); |
|
|
|
|
|
|
|
addDependency( strongDependencies, id ); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
@ -312,8 +360,7 @@ export default class Module { |
|
|
|
keys( statement.stronglyDependsOn ).forEach( name => { |
|
|
|
if ( statement.defines[ name ] ) return; |
|
|
|
|
|
|
|
addDependency( strongDependencies, this.exportDelegates[ name ] ) || |
|
|
|
addDependency( strongDependencies, this.imports[ name ] ); |
|
|
|
addDependency( strongDependencies, this.locals.lookup( name ) ); |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
@ -324,98 +371,47 @@ export default class Module { |
|
|
|
keys( statement.dependsOn ).forEach( name => { |
|
|
|
if ( statement.defines[ name ] ) return; |
|
|
|
|
|
|
|
addDependency( weakDependencies, this.exportDelegates[ name ] ) || |
|
|
|
addDependency( weakDependencies, this.imports[ name ] ); |
|
|
|
addDependency( weakDependencies, this.locals.lookup( name ) ); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
return { strongDependencies, weakDependencies }; |
|
|
|
} |
|
|
|
this.locals.getNames().forEach( name => { |
|
|
|
const id = this.locals.lookup( name ); |
|
|
|
|
|
|
|
defaultName () { |
|
|
|
const defaultExport = this.exports.default; |
|
|
|
if ( !id.modifierStatements ) return; |
|
|
|
|
|
|
|
if ( !defaultExport ) return null; |
|
|
|
id.modifierStatements.forEach( statement => { |
|
|
|
const module = statement.module; |
|
|
|
weakDependencies[ module.id ] = module; |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
const name = defaultExport.identifier && !defaultExport.isModified ? |
|
|
|
defaultExport.identifier : |
|
|
|
this.replacements.default; |
|
|
|
// `Bundle.sort` gets stuck in an infinite loop if a module has
|
|
|
|
// `strongDependencies` to itself. Make sure it doesn't happen.
|
|
|
|
delete strongDependencies[ this.id ]; |
|
|
|
delete weakDependencies[ this.id ]; |
|
|
|
|
|
|
|
return this.replacements[ name ] || name; |
|
|
|
return { strongDependencies, weakDependencies }; |
|
|
|
} |
|
|
|
|
|
|
|
findDefiningStatement ( name ) { |
|
|
|
if ( this.definitions[ name ] ) return this.definitions[ name ]; |
|
|
|
// Enforce dynamic access of the module's properties.
|
|
|
|
dynamicAccess () { |
|
|
|
if ( this.needsDynamicAccess ) return; |
|
|
|
|
|
|
|
// TODO what about `default`/`*`?
|
|
|
|
this.needsDynamicAccess = true; |
|
|
|
this.markAllExportStatements(); |
|
|
|
|
|
|
|
const importDeclaration = this.imports[ name ]; |
|
|
|
if ( !importDeclaration ) return null; |
|
|
|
|
|
|
|
return importDeclaration.module.findDefiningStatement( name ); |
|
|
|
if ( !~this.bundle.internalNamespaceModules.indexOf( this ) ) { |
|
|
|
this.bundle.internalNamespaceModules.push( this ); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
mark ( name ) { |
|
|
|
// shortcut cycles
|
|
|
|
if ( this.marked[ name ] ) return; |
|
|
|
this.marked[ name ] = true; |
|
|
|
|
|
|
|
// The definition for this name is in a different module
|
|
|
|
if ( this.imports[ name ] ) { |
|
|
|
const importDeclaration = this.imports[ name ]; |
|
|
|
importDeclaration.isUsed = true; |
|
|
|
|
|
|
|
const module = importDeclaration.module; |
|
|
|
|
|
|
|
// suggest names. TODO should this apply to non default/* imports?
|
|
|
|
if ( importDeclaration.name === 'default' ) { |
|
|
|
// TODO this seems ropey
|
|
|
|
const localName = importDeclaration.localName; |
|
|
|
let suggestion = this.suggestedNames[ localName ] || localName; |
|
|
|
|
|
|
|
// special case - the module has its own import by this name
|
|
|
|
while ( !module.isExternal && module.imports[ suggestion ] ) { |
|
|
|
suggestion = `_${suggestion}`; |
|
|
|
} |
|
|
|
|
|
|
|
module.suggestName( 'default', suggestion ); |
|
|
|
} else if ( importDeclaration.name === '*' ) { |
|
|
|
const localName = importDeclaration.localName; |
|
|
|
const suggestion = this.suggestedNames[ localName ] || localName; |
|
|
|
module.suggestName( '*', suggestion ); |
|
|
|
module.suggestName( 'default', `${suggestion}__default` ); |
|
|
|
} |
|
|
|
|
|
|
|
if ( importDeclaration.name === 'default' ) { |
|
|
|
module.needsDefault = true; |
|
|
|
} else if ( importDeclaration.name === '*' ) { |
|
|
|
module.needsAll = true; |
|
|
|
} else { |
|
|
|
module.needsNamed = true; |
|
|
|
} |
|
|
|
|
|
|
|
if ( module.isExternal ) { |
|
|
|
module.importedByBundle.push( importDeclaration ); |
|
|
|
} |
|
|
|
|
|
|
|
else if ( importDeclaration.name === '*' ) { |
|
|
|
// we need to create an internal namespace
|
|
|
|
if ( !~this.bundle.internalNamespaceModules.indexOf( module ) ) { |
|
|
|
this.bundle.internalNamespaceModules.push( module ); |
|
|
|
} |
|
|
|
|
|
|
|
module.markAllExportStatements(); |
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
module.markExport( importDeclaration.name, name, this ); |
|
|
|
} |
|
|
|
} |
|
|
|
getModule ( source ) { |
|
|
|
return this.bundle.moduleById[ this.resolvedIds[ source ] ]; |
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
const statement = name === 'default' ? this.exports.default.statement : this.definitions[ name ]; |
|
|
|
if ( statement ) statement.mark(); |
|
|
|
} |
|
|
|
mark () { |
|
|
|
this.dynamicAccess(); |
|
|
|
} |
|
|
|
|
|
|
|
markAllStatements ( isEntryModule ) { |
|
|
@ -427,8 +423,7 @@ export default class Module { |
|
|
|
// ...unless they're empty, in which case assume we're importing them for the side-effects
|
|
|
|
// THIS IS NOT FOOLPROOF. Probably need /*rollup: include */ or similar
|
|
|
|
if ( !statement.node.specifiers.length ) { |
|
|
|
const id = this.resolvedIds[ statement.node.source.value ]; |
|
|
|
const otherModule = this.bundle.moduleById[ id ]; |
|
|
|
const otherModule = this.getModule( statement.node.source.value ); |
|
|
|
|
|
|
|
if ( !otherModule.isExternal ) otherModule.markAllStatements(); |
|
|
|
} |
|
|
@ -442,6 +437,11 @@ export default class Module { |
|
|
|
|
|
|
|
// include everything else
|
|
|
|
else { |
|
|
|
// Be sure to mark the default export for the entry module.
|
|
|
|
if ( isEntryModule && statement.node.type === 'ExportDefaultDeclaration' ) { |
|
|
|
this.exports.lookup( 'default' ).mark(); |
|
|
|
} |
|
|
|
|
|
|
|
statement.mark(); |
|
|
|
} |
|
|
|
}); |
|
|
@ -453,49 +453,6 @@ export default class Module { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
markExport ( name, suggestedName, importer ) { |
|
|
|
const reexport = this.reexports[ name ]; |
|
|
|
const exportDeclaration = this.exports[ name ]; |
|
|
|
|
|
|
|
if ( reexport ) { |
|
|
|
reexport.isUsed = true; |
|
|
|
reexport.module.markExport( reexport.localName, suggestedName, this ); |
|
|
|
} |
|
|
|
|
|
|
|
else if ( exportDeclaration ) { |
|
|
|
exportDeclaration.isUsed = true; |
|
|
|
if ( name === 'default' ) { |
|
|
|
this.needsDefault = true; |
|
|
|
this.suggestName( 'default', suggestedName ); |
|
|
|
return exportDeclaration.statement.mark(); |
|
|
|
} |
|
|
|
|
|
|
|
this.mark( exportDeclaration.localName ); |
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
// See if there exists an export delegate that defines `name`.
|
|
|
|
let i; |
|
|
|
for ( i = 0; i < this.exportAlls.length; i += 1 ) { |
|
|
|
const declaration = this.exportAlls[i]; |
|
|
|
|
|
|
|
if ( declaration.module.exports[ name ] ) { |
|
|
|
// It's found! This module exports `name` through declaration.
|
|
|
|
// It is however not imported into this scope.
|
|
|
|
this.exportDelegates[ name ] = declaration; |
|
|
|
declaration.module.markExport( name ); |
|
|
|
|
|
|
|
declaration.statement.dependsOn[ name ] = |
|
|
|
declaration.statement.stronglyDependsOn[ name ] = true; |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
throw new Error( `Module ${this.id} does not export ${name} (imported by ${importer.id})` ); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
parse ( ast ) { |
|
|
|
// The ast can be supplied programmatically (but usually won't be)
|
|
|
|
if ( !ast ) { |
|
|
@ -579,11 +536,7 @@ export default class Module { |
|
|
|
return statements; |
|
|
|
} |
|
|
|
|
|
|
|
rename ( name, replacement ) { |
|
|
|
this.replacements[ name ] = replacement; |
|
|
|
} |
|
|
|
|
|
|
|
render ( allBundleExports, moduleReplacements ) { |
|
|
|
render ( toExport, direct ) { |
|
|
|
let magicString = this.magicString.clone(); |
|
|
|
|
|
|
|
this.statements.forEach( statement => { |
|
|
@ -601,7 +554,7 @@ export default class Module { |
|
|
|
} |
|
|
|
|
|
|
|
// skip `export var foo;` if foo is exported
|
|
|
|
if ( isEmptyExportedVarDeclaration( statement.node.declaration, allBundleExports, moduleReplacements ) ) { |
|
|
|
if ( isEmptyExportedVarDeclaration( statement.node.declaration, this.exports, toExport ) ) { |
|
|
|
magicString.remove( statement.start, statement.next ); |
|
|
|
return; |
|
|
|
} |
|
|
@ -609,7 +562,7 @@ export default class Module { |
|
|
|
|
|
|
|
// skip empty var declarations for exported bindings
|
|
|
|
// (otherwise we're left with `exports.foo;`, which is useless)
|
|
|
|
if ( isEmptyExportedVarDeclaration( statement.node, allBundleExports, moduleReplacements ) ) { |
|
|
|
if ( isEmptyExportedVarDeclaration( statement.node, this.exports, toExport ) ) { |
|
|
|
magicString.remove( statement.start, statement.next ); |
|
|
|
return; |
|
|
|
} |
|
|
@ -617,7 +570,7 @@ export default class Module { |
|
|
|
// split up/remove var declarations as necessary
|
|
|
|
if ( statement.node.isSynthetic ) { |
|
|
|
// insert `var/let/const` if necessary
|
|
|
|
if ( !allBundleExports[ statement.node.declarations[0].id.name ] ) { |
|
|
|
if ( !toExport[ statement.node.declarations[0].id.name ] ) { |
|
|
|
magicString.insert( statement.start, `${statement.node.kind} ` ); |
|
|
|
} |
|
|
|
|
|
|
@ -627,14 +580,34 @@ export default class Module { |
|
|
|
let replacements = blank(); |
|
|
|
let bundleExports = blank(); |
|
|
|
|
|
|
|
// Indirect identifier access.
|
|
|
|
if ( !direct ) { |
|
|
|
keys( statement.dependsOn ) |
|
|
|
.forEach( name => { |
|
|
|
const id = this.locals.lookup( name ); |
|
|
|
|
|
|
|
// We shouldn't create a replacement for `id` if
|
|
|
|
// 1. `id` is a Global, in which case it has no module property
|
|
|
|
// 2. `id.module` isn't external, which means we have direct access
|
|
|
|
// 3. `id` is its own module, in the case of namespace imports
|
|
|
|
if ( id.module && id.module.isExternal && id.module !== id ) { |
|
|
|
replacements[ name ] = id.originalName === 'default' ? |
|
|
|
// default names are always directly accessed
|
|
|
|
id.name : |
|
|
|
// other names are indirectly accessed
|
|
|
|
`${id.module.name}.${id.originalName}`; |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
keys( statement.dependsOn ) |
|
|
|
.concat( keys( statement.defines ) ) |
|
|
|
.forEach( name => { |
|
|
|
const bundleName = moduleReplacements[ name ] || name; |
|
|
|
const bundleName = this.locals.lookup( name ).name; |
|
|
|
|
|
|
|
if ( allBundleExports[ bundleName ] ) { |
|
|
|
bundleExports[ name ] = replacements[ name ] = allBundleExports[ bundleName ]; |
|
|
|
} else if ( bundleName !== name ) { // TODO weird structure
|
|
|
|
if ( toExport[ bundleName ] ) { |
|
|
|
bundleExports[ name ] = replacements[ name ] = toExport[ bundleName ]; |
|
|
|
} else if ( bundleName !== name && !replacements[ name ] ) { // TODO weird structure
|
|
|
|
replacements[ name ] = bundleName; |
|
|
|
} |
|
|
|
}); |
|
|
@ -655,24 +628,25 @@ export default class Module { |
|
|
|
} |
|
|
|
|
|
|
|
else if ( statement.node.type === 'ExportDefaultDeclaration' ) { |
|
|
|
const canonicalName = this.defaultName(); |
|
|
|
const def = this.exports.lookup( 'default' ); |
|
|
|
|
|
|
|
if ( statement.node.declaration.type === 'Identifier' && canonicalName === ( moduleReplacements[ statement.node.declaration.name ] || statement.node.declaration.name ) ) { |
|
|
|
// FIXME: dunno what to do here yet.
|
|
|
|
if ( statement.node.declaration.type === 'Identifier' && def.name === ( replacements[ statement.node.declaration.name ] || statement.node.declaration.name ) ) { |
|
|
|
magicString.remove( statement.start, statement.next ); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// prevent `var undefined = sideEffectyDefault(foo)`
|
|
|
|
if ( canonicalName === undefined ) { |
|
|
|
if ( !def.isUsed ) { |
|
|
|
magicString.remove( statement.start, statement.node.declaration.start ); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// anonymous functions should be converted into declarations
|
|
|
|
if ( statement.node.declaration.type === 'FunctionExpression' ) { |
|
|
|
magicString.overwrite( statement.node.start, statement.node.declaration.start + 8, `function ${canonicalName}` ); |
|
|
|
magicString.overwrite( statement.node.start, statement.node.declaration.start + 8, `function ${def.name}` ); |
|
|
|
} else { |
|
|
|
magicString.overwrite( statement.node.start, statement.node.declaration.start, `var ${canonicalName} = ` ); |
|
|
|
magicString.overwrite( statement.node.start, statement.node.declaration.start, `var ${def.name} = ` ); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -684,15 +658,4 @@ export default class Module { |
|
|
|
|
|
|
|
return magicString.trim(); |
|
|
|
} |
|
|
|
|
|
|
|
suggestName ( defaultOrBatch, suggestion ) { |
|
|
|
// deconflict anonymous default exports with this module's definitions
|
|
|
|
const shouldDeconflict = this.exports.default && this.exports.default.isAnonymous; |
|
|
|
|
|
|
|
if ( shouldDeconflict ) suggestion = deconflict( suggestion, this.definitions ); |
|
|
|
|
|
|
|
if ( !this.suggestedNames[ defaultOrBatch ] ) { |
|
|
|
this.suggestedNames[ defaultOrBatch ] = makeLegalIdentifier( suggestion ); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|