Search code examples
javascriptrequirejstypescriptcircular-dependency

Resolving circular dependencies for requireJS


We have a ton of typescript files that use external modules and we're hitting circular dependencies. (I've looked at other questions like this but none appear to answer my specific question.) This is in typescript so we're constrained to the way it generates the javascript.

  1. First off, is there a tool that will read through all my files (the .ts or the .js) and flag the places where I have circular dependencies? I think we can eliminate most if we can identify them.
  2. We have several cases where the design we must model forces circular dependencies. Is there a way to handle this in requireJS (again as created by typescript)?
  3. Is the answer to have the typescript compiler create a single .js file from all the .ts files? And if so, is there any downside to this approach, both when debugging and in production?

thanks - dave


Solution

  • is there a tool that will read through all my files (the .ts or the .js) and flag the places where I have circular dependencies?

    Atom-TypeScript can do circular dependency analysis : https://github.com/TypeStrong/atom-typescript/blob/master/docs/dependency-view.md#circular

    Also this looks interesting : https://www.npmjs.org/package/madge

    Is there a way to handle this in requireJS

    You get undefined initially but then you can require again : http://requirejs.org/docs/api.html#circular

    Is the answer to have the typescript compiler create a single .js file from all the .ts files? And if so, is there any downside to this approach, both when debugging and in production?

    Yes --out. -ves : slower compile times, must use sourcemaps for debugging. inability to lazy load parts of the codebase at production times.