Browse Source

warn on dangerous ordering

gh-438-b
Rich-Harris 9 years ago
parent
commit
b993baff4e
  1. 52
      src/Bundle.js

52
src/Bundle.js

@ -281,24 +281,56 @@ export default class Bundle {
let seen = {};
let ordered = [];
function visit ( module ) {
if ( seen[ module.id ] ) return;
seen[ module.id ] = true;
let stronglyDependsOn = blank();
this.modules.forEach( module => {
stronglyDependsOn[ module.id ] = blank();
});
module.dependencies.forEach( source => {
const resolved = module.resolvedIds[ source ];
const dependency = moduleById[ resolved ];
this.modules.forEach( module => {
function processDependency ( dependency ) {
if ( dependency === module || stronglyDependsOn[ module.id ][ dependency.id ] ) return;
if ( dependency.isExternal ) return;
stronglyDependsOn[ module.id ][ dependency.id ] = true;
dependency.strongDependencies.forEach( processDependency );
}
visit( dependency );
});
module.strongDependencies.forEach( processDependency );
});
const visit = module => {
if ( seen[ module.id ] ) return;
seen[ module.id ] = true;
const dependencies = module.dependencies
.map( source => {
const resolved = module.resolvedIds[ source ];
return moduleById[ resolved ];
})
.filter( dependency => !dependency.isExternal );
dependencies.forEach( visit );
ordered.push( module );
}
};
visit( this.entryModule );
ordered.forEach( ( a, i ) => {
const b = ordered[ i + 1 ];
if ( !b ) return;
if ( stronglyDependsOn[ a.id ][ b.id ] ) {
// somewhere, there is a module that imports b before a. Because
// b imports a, a is placed before b. We need to find the module
// in question, so we can provide a useful error message
const parent = { id: 'TODO' };
throw new Error(
`Module ${a.id} may be unable to evaluate without ${b.id}, but is included first due to a cyclical dependency. Consider swapping the import statements in ${parent.id} to ensure correct ordering`
);
}
});
return ordered;
}
}

Loading…
Cancel
Save