|
@ -1,5 +1,5 @@ |
|
|
import { resolve } from 'path'; |
|
|
import { dirname, resolve } from 'path'; |
|
|
import { readFile } from 'sander'; |
|
|
import { readFile, Promise } from 'sander'; |
|
|
import MagicString from 'magic-string'; |
|
|
import MagicString from 'magic-string'; |
|
|
import { keys, has } from './utils/object'; |
|
|
import { keys, has } from './utils/object'; |
|
|
import { sequence } from './utils/promise'; |
|
|
import { sequence } from './utils/promise'; |
|
@ -12,8 +12,9 @@ import { defaultResolver } from './utils/resolvePath'; |
|
|
|
|
|
|
|
|
export default class Bundle { |
|
|
export default class Bundle { |
|
|
constructor ( options ) { |
|
|
constructor ( options ) { |
|
|
this.base = options.base || process.cwd(); |
|
|
this.entryPath = resolve( options.entry ).replace( /\.js$/, '' ) + '.js'; |
|
|
this.entryPath = resolve( this.base, options.entry ).replace( /\.js$/, '' ) + '.js'; |
|
|
this.base = dirname( this.entryPath ); |
|
|
|
|
|
|
|
|
this.resolvePath = options.resolvePath || defaultResolver; |
|
|
this.resolvePath = options.resolvePath || defaultResolver; |
|
|
|
|
|
|
|
|
this.entryModule = null; |
|
|
this.entryModule = null; |
|
@ -23,8 +24,8 @@ export default class Bundle { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
fetchModule ( importee, importer ) { |
|
|
fetchModule ( importee, importer ) { |
|
|
const path = this.resolvePath( importee, importer ); |
|
|
return Promise.resolve( importer === null ? importee : this.resolvePath( importee, importer ) ) |
|
|
|
|
|
.then( path => { |
|
|
if ( !path ) { |
|
|
if ( !path ) { |
|
|
// external module
|
|
|
// external module
|
|
|
if ( !has( this.modulePromises, importee ) ) { |
|
|
if ( !has( this.modulePromises, importee ) ) { |
|
@ -50,11 +51,12 @@ export default class Bundle { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return this.modulePromises[ path ]; |
|
|
return this.modulePromises[ path ]; |
|
|
|
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
build () { |
|
|
build () { |
|
|
// bring in top-level AST nodes from the entry module
|
|
|
// bring in top-level AST nodes from the entry module
|
|
|
return this.fetchModule( this.entryPath ) |
|
|
return this.fetchModule( this.entryPath, null ) |
|
|
.then( entryModule => { |
|
|
.then( entryModule => { |
|
|
this.entryModule = entryModule; |
|
|
this.entryModule = entryModule; |
|
|
|
|
|
|
|
|