|
@ -8,13 +8,16 @@ import ExternalModule from './ExternalModule'; |
|
|
import finalisers from './finalisers/index'; |
|
|
import finalisers from './finalisers/index'; |
|
|
import replaceIdentifiers from './utils/replaceIdentifiers'; |
|
|
import replaceIdentifiers from './utils/replaceIdentifiers'; |
|
|
import makeLegalIdentifier from './utils/makeLegalIdentifier'; |
|
|
import makeLegalIdentifier from './utils/makeLegalIdentifier'; |
|
|
|
|
|
import { defaultResolver } from './utils/resolvePath'; |
|
|
|
|
|
|
|
|
export default class Bundle { |
|
|
export default class Bundle { |
|
|
constructor ( options ) { |
|
|
constructor ( options ) { |
|
|
this.base = options.base; |
|
|
this.base = options.base || process.cwd(); |
|
|
this.entryPath = resolve( this.base, options.entry ); |
|
|
this.entryPath = resolve( this.base, options.entry ).replace( /\.js$/, '' ) + '.js'; |
|
|
this.entryModule = null; |
|
|
this.entryModule = null; |
|
|
|
|
|
|
|
|
|
|
|
this.resolvePath = options.resolvePath || defaultResolver; |
|
|
|
|
|
|
|
|
this.modulePromises = {}; |
|
|
this.modulePromises = {}; |
|
|
this.modules = {}; |
|
|
this.modules = {}; |
|
|
|
|
|
|
|
@ -28,9 +31,19 @@ export default class Bundle { |
|
|
this.externalModules = []; |
|
|
this.externalModules = []; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
fetchModule ( path, id ) { |
|
|
fetchModule ( importee, importer ) { |
|
|
// TODO currently, we'll get different ExternalModule objects
|
|
|
const path = this.resolvePath( importee, importer ); |
|
|
// depending on where they're imported from...
|
|
|
|
|
|
|
|
|
if ( !path ) { |
|
|
|
|
|
// external module
|
|
|
|
|
|
if ( !has( this.modulePromises, importee ) ) { |
|
|
|
|
|
const module = new ExternalModule( importee ); |
|
|
|
|
|
this.externalModules.push( module ); |
|
|
|
|
|
this.modulePromises[ importee ] = Promise.resolve( module ); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return this.modulePromises[ importee ]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if ( !has( this.modulePromises, path ) ) { |
|
|
if ( !has( this.modulePromises, path ) ) { |
|
|
this.modulePromises[ path ] = readFile( path, { encoding: 'utf-8' }) |
|
|
this.modulePromises[ path ] = readFile( path, { encoding: 'utf-8' }) |
|
@ -43,21 +56,6 @@ export default class Bundle { |
|
|
|
|
|
|
|
|
this.modules[ path ] = module; |
|
|
this.modules[ path ] = module; |
|
|
return module; |
|
|
return module; |
|
|
}, err => { |
|
|
|
|
|
if ( err.code === 'ENOENT' ) { |
|
|
|
|
|
if ( id[0] === '.' ) { |
|
|
|
|
|
// external modules can't have relative paths
|
|
|
|
|
|
throw err; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// most likely an external module
|
|
|
|
|
|
// TODO fire an event, or otherwise allow some way for
|
|
|
|
|
|
// users to control external modules better?
|
|
|
|
|
|
const module = new ExternalModule( id ); |
|
|
|
|
|
|
|
|
|
|
|
this.externalModules.push( module ); |
|
|
|
|
|
return module; |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|