Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
build () {
// Phase 1 – discovery. We load the entry module and find which
// modules it imports, and import those, until we have all
// of the entry module's dependencies
return Promise.resolve( this.resolveId( this.entry, undefined ) )
.then( id => this.fetchModule( id, undefined ) )
.then( entryModule => {
this.entryModule = entryModule;
// Phase 2 – binding. We link references to their declarations
// to generate a complete picture of the bundle
this.modules.forEach( module => module.bindImportSpecifiers() );
this.modules.forEach( module => module.bindAliases() );
this.modules.forEach( module => module.bindReferences() );
// Phase 3 – marking. We 'run' each statement to see which ones
// need to be included in the generated bundle
// mark all export statements
entryModule.getExports().forEach( name => {
const declaration = entryModule.traceExport( name );
map: null
};
}
// `result.map` can only be a string if `result` isn't
else if ( typeof result.map === 'string' ) {
result.map = JSON.parse( result.map );
}
sourceMapChain.push( result.map );
ast = result.ast;
return result.code;
});
});
}, Promise.resolve( source.code ) )
.then( code => ({ code, originalCode, ast, sourceMapChain }) );
}
return promise.then( previous => {
return Promise.resolve( transformer( previous, id ) ).then( result => {
if ( result == null ) return previous;
if ( typeof result === 'string' ) {
result = {
code: result,
ast: null,
map: null
};
}
// `result.map` can only be a string if `result` isn't
else if ( typeof result.map === 'string' ) {
result.map = JSON.parse( result.map );
}
sourceMapChain.push( result.map );
ast = result.ast;
fetchModule ( id, importer ) {
// short-circuit cycles
if ( id in this.moduleById ) return null;
this.moduleById[ id ] = null;
return Promise.resolve( this.load( id ) )
.catch( err => {
let msg = `Could not load ${id}`;
if ( importer ) msg += ` (imported by ${importer})`;
msg += `: ${err.message}`;
throw new Error( msg );
})
.then( source => transform( source, id, this.transformers ) )
.then( source => {
const { code, originalCode, ast, sourceMapChain } = source;
const module = new Module({ id, code, originalCode, ast, sourceMapChain, bundle: this });
this.modules.push( module );
this.moduleById[ id ] = module;
const promises = module.dependencies.map( source => {
return Promise.resolve( this.resolveId( source, module.id ) )
.then( resolvedId => {
if ( !resolvedId ) {
if ( isRelative( source ) ) throw new Error( `Could not resolve ${source} from ${module.id}` );
if ( !~this.external.indexOf( source ) ) this.onwarn( `Treating '${source}' as external dependency` );
module.resolvedIds[ source ] = source;
if ( !this.moduleById[ source ] ) {
const module = new ExternalModule( source );
this.externalModules.push( module );
this.moduleById[ source ] = module;
}
}
else {
if ( resolvedId === module.id ) {
throw new Error( `A module cannot import itself (${resolvedId})` );
export function mapSequence ( array, fn ) {
let results = [];
let promise = Promise.resolve();
function next ( member, i ) {
return fn( member ).then( value => results[i] = value );
}
for ( let i = 0; i < array.length; i += 1 ) {
promise = promise.then( () => next( array[i], i ) );
}
return promise.then( () => results );
}