0

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

Community
  • 1
  • 1
David Thielen
  • 28,723
  • 34
  • 119
  • 193
  • For 3.: If you use `--out FILE` as a compiler flag, all files will be concatenated into the specified file. – Ingo Bürk Jun 04 '14 at 14:25
  • Suggest you to try a webpack with ts-loader for production builds. And you can look here for an intresting plugin https://www.npmjs.com/package/circular-dependency-plugin – FDisk May 26 '17 at 19:11

1 Answers1

0

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.

basarat
  • 261,912
  • 58
  • 460
  • 511
  • On the circular issue with require link - the .js created by typescript follows the suggestions, but requireJS is still hitting an error trying to load on a circular reference. Is something else required for the typescript generated .js? – David Thielen Jun 04 '14 at 15:24
  • And Madge looks perfect, but it requires sudo and I'm on Windows :( – David Thielen Jun 04 '14 at 15:26
  • On the circular issue with require link - the .js created by typescript follows the suggestions, but requireJS is still hitting an error trying to load on a circular reference. Is something else required for the typescript generated - no. Can you share the TS? – basarat Jun 04 '14 at 22:17
  • I am trying to pull out a simple example. The problem is as soon as I simplify it, the problem goes away. On the plus side, one of those simplifications works for us, so we're good now. But still, I want to find out what is going on here. – David Thielen Jun 06 '14 at 15:26
  • @DavidThielen I wrote this just for you, Atom TypeScript now does circular dependency analysis : https://github.com/TypeStrong/atom-typescript/blob/master/docs/dependency-view.md#circular – basarat Apr 06 '15 at 03:14