Browse Source

simple sort

gh-438-b
Rich-Harris 9 years ago
parent
commit
40d6158147
  1. 70
      src/Bundle.js
  2. 4
      src/Module.js
  3. 4
      src/Statement.js

70
src/Bundle.js

@ -276,82 +276,28 @@ export default class Bundle {
}
sort () {
const moduleById = this.moduleById;
let seen = {};
let ordered = [];
let hasCycles;
let strongDeps = {};
let stronglyDependsOn = {};
function visit ( module ) {
if ( seen[ module.id ] ) return;
seen[ module.id ] = true;
const { strongDependencies, weakDependencies } = module.consolidateDependencies();
strongDeps[ module.id ] = [];
stronglyDependsOn[ module.id ] = {};
module.dependencies.forEach( source => {
const resolved = module.resolvedIds[ source ];
const dependency = moduleById[ resolved ];
strongDependencies.forEach( imported => {
strongDeps[ module.id ].push( imported );
if ( dependency.isExternal ) return;
if ( seen[ imported.id ] ) {
// we need to prevent an infinite loop, and note that
// we need to check for strong/weak dependency relationships
hasCycles = true;
return;
}
visit( imported );
visit( dependency );
});
weakDependencies.forEach( imported => {
if ( seen[ imported.id ] ) {
// we need to prevent an infinite loop, and note that
// we need to check for strong/weak dependency relationships
hasCycles = true;
return;
}
visit( imported );
});
// add second (and third...) order dependencies
function addStrongDependencies ( dependency ) {
if ( stronglyDependsOn[ module.id ][ dependency.id ] ) return;
stronglyDependsOn[ module.id ][ dependency.id ] = true;
strongDeps[ dependency.id ].forEach( addStrongDependencies );
}
strongDeps[ module.id ].forEach( addStrongDependencies );
ordered.push( module );
}
this.modules.forEach( visit );
if ( hasCycles ) {
let unordered = ordered;
ordered = [];
// unordered is actually semi-ordered, as [ fewer dependencies ... more dependencies ]
unordered.forEach( module => {
// ensure strong dependencies of `module` that don't strongly depend on `module` go first
strongDeps[ module.id ].forEach( place );
function place ( dep ) {
if ( !stronglyDependsOn[ dep.id ][ module.id ] && !~ordered.indexOf( dep ) ) {
strongDeps[ dep.id ].forEach( place );
ordered.push( dep );
}
}
if ( !~ordered.indexOf( module ) ) {
ordered.push( module );
}
});
}
visit( this.entryModule );
return ordered;
}

4
src/Module.js

@ -566,11 +566,11 @@ export default class Module {
return magicString.trim();
}
run ( safe ) {
run () {
let marked = false;
this.statements.forEach( statement => {
marked = statement.run( this.strongDependencies, safe ) || marked;
marked = statement.run( this.strongDependencies ) || marked;
});
return marked;

4
src/Statement.js

@ -130,11 +130,11 @@ export default class Statement {
});
}
run ( strongDependencies, safe ) {
run ( strongDependencies ) {
if ( ( this.ran && this.isIncluded ) || this.isImportDeclaration || this.isFunctionDeclaration ) return;
this.ran = true;
if ( run( this.node, this.scope, this, strongDependencies, false, safe ) ) {
if ( run( this.node, this.scope, this, strongDependencies, false ) ) {
this.mark();
return true;
}

Loading…
Cancel
Save