Reputation: 709
Update: Thank you very much for Daniel's reply below. I've however found rollup.js to be answer for me. It can handle necessary circular dependencies between classes in projects well (webpack struggled with that), works straight on the ES6 module format and very easily outputs code both for node and browsers.
Hi (: What is good practice for Typescript coding when having large amounts of classes that might be used both in browser or in node? Do you put multiple classes in single files and treat them as namespaces, or is there another way?
I'm a mainly C# using programmer looking into using Typescript for a new project. I see that I can compile internal modules into a large .js file using references and --outFile, or use external modules with commonjs or amd.
Traditionally I've place each class in an individual file, and this feels natural in Typescript as well. However, let's say I have 200 classes in my project, this yields 200+ files to download when running in browser, which seems... excessive.
Upvotes: 2
Views: 2025
Reputation: 2053
Yes, you would try to write small classes/modules with typescript with each in their own file.
But when you serve your app, you don't want the browser to load every file. So you need something to bundle your files and put them in one javascript file. Build tools help you with these (they also remove blank lines/unnecessary spacing and comments).
Popular build tools are
https://github.com/webpack/webpack
When you come from a c# background, grunt might seem more familiar than webpack to you.
I use webpack.
Upvotes: 4