Skip to content
Rich-Harris edited this page Nov 12, 2014 · 4 revisions

You'll often hear people tell you that cyclical dependencies are a symptom of a badly architected codebase. That's rubbish - oftentimes you need cyclical dependencies to model your domain, particularly if you're modeling recursive structures.

So why do people say that? Because with AMD and CommonJS, getting cyclical dependencies to work can be a real challenge, resulting in hacktacular code. It becomes much easier with ES6:

// createObject.js
import createChildren from './createChildren';

export default function createObject ( numChildren ) {
  return {
    children: createChildren( numChildren )
  };
}

// createChildren.js
import createObject from './createObject';

export default function createChildren ( i ) {
  var children = [];

  while ( i-- ) {
    children[i] = createObject( 0 );
  }

  return children;
}

// app.js
import createObject from './createObject';

var contrivedExample = createObject( 42 );

Try doing that with AMD. Go on, I'll wait.

Esperanto will [bundle](Bundling multiple ES6 modules) those three modules (app.js, createObject.js and createChildren.js) into a single AMD/CommonJS/UMD file with no problem at all. You can also [convert them to individual modules](Converting a single module) in strict mode.