From 40d6158147e9f991d2d132b314755f824dd6004c Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sat, 9 Jan 2016 09:59:59 -0500 Subject: [PATCH] simple sort --- src/Bundle.js | 70 ++++++------------------------------------------ src/Module.js | 4 +-- src/Statement.js | 4 +-- 3 files changed, 12 insertions(+), 66 deletions(-) diff --git a/src/Bundle.js b/src/Bundle.js index fa53ae6..42e1c41 100644 --- a/src/Bundle.js +++ b/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; } diff --git a/src/Module.js b/src/Module.js index a63687b..38f63e0 100644 --- a/src/Module.js +++ b/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; diff --git a/src/Statement.js b/src/Statement.js index 06d8827..8bdd85c 100644 --- a/src/Statement.js +++ b/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; }