Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structuring a large complex module in TypeScript

Tags:

typescript

I am creating a complex module in TypeScript (parsing PE file structure, similar to what Mono.Cecil does, but in TS/JS and less sophisticated).

The question is how best to put that functionality in file/folder/module dimensions.

I started with structure similar to C#, with each class in its own file, namespaces (modules) corresponding to large sets of more or less independent functionality and living in subfolders.

Now in TypeScript there are several issues with that. Or maybe the issues are with my being stupid?

  1. Every class in a separate file is good for development, but makes no sense for the compiled output. Nobody wants to carry around 24 files instead of one.
  2. If I compile everything into one file (tsc -out), contents of each separate file is emitted almost independently, even if they all belong to the same module. TypeScript builds that module up using some funky hacky-looking syntax, not at all what I would write by hand (which would be sticking them all in one module-defining immediately-invoked function).
  3. I want my stuff to work in Node.js and browsers. For Node I need to pepper it all with 'export module', but for browser the typical usage is just produce whatever.js and let pages have it in -- which means I should strip 'export module' back off.

What's the best way to deal with that?

like image 562
Oleg Mihailik Avatar asked Nov 17 '12 11:11

Oleg Mihailik


1 Answers

I am currently using require.js and amd modules to structure code. Each class is in a single file, and all that is needed is a require.js bootstrapper. I dont work with node, so cant comment here, but it may be worth having a look. http://blorkfish.wordpress.com/2012/10/23/typescript-organizing-your-code-with-amd-modules-and-require-js/

like image 176
blorkfish Avatar answered Nov 14 '22 07:11

blorkfish