Restructured TS map computation

As close to final as it could get today.
Imports of non-optional dependencies go on the top.

This is mostly a convention, but it also means that some weird import issues like circular dependencies show up early.