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 () { sort () {
const moduleById = this.moduleById;
let seen = {}; let seen = {};
let ordered = []; let ordered = [];
let hasCycles;
let strongDeps = {};
let stronglyDependsOn = {};
function visit ( module ) { function visit ( module ) {
if ( seen[ module.id ] ) return; if ( seen[ module.id ] ) return;
seen[ module.id ] = true; seen[ module.id ] = true;
const { strongDependencies, weakDependencies } = module.consolidateDependencies(); module.dependencies.forEach( source => {
const resolved = module.resolvedIds[ source ];
strongDeps[ module.id ] = []; const dependency = moduleById[ resolved ];
stronglyDependsOn[ module.id ] = {};
strongDependencies.forEach( imported => { if ( dependency.isExternal ) return;
strongDeps[ module.id ].push( imported );
if ( seen[ imported.id ] ) { visit( dependency );
// we need to prevent an infinite loop, and note that
// we need to check for strong/weak dependency relationships
hasCycles = true;
return;
}
visit( imported );
}); });
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 ); ordered.push( module );
} }
this.modules.forEach( visit ); visit( this.entryModule );
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 );
}
});
}
return ordered; return ordered;
} }

4
src/Module.js

@ -566,11 +566,11 @@ export default class Module {
return magicString.trim(); return magicString.trim();
} }
run ( safe ) { run () {
let marked = false; let marked = false;
this.statements.forEach( statement => { this.statements.forEach( statement => {
marked = statement.run( this.strongDependencies, safe ) || marked; marked = statement.run( this.strongDependencies ) || marked;
}); });
return 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; if ( ( this.ran && this.isIncluded ) || this.isImportDeclaration || this.isFunctionDeclaration ) return;
this.ran = true; this.ran = true;
if ( run( this.node, this.scope, this, strongDependencies, false, safe ) ) { if ( run( this.node, this.scope, this, strongDependencies, false ) ) {
this.mark(); this.mark();
return true; return true;
} }

Loading…
Cancel
Save