Can any Typescript experts clarify when and why you would choose AMD vs Common JS for module creation when using Typescript?
In fact, AMD was split from CommonJS early in its development. The main difference between AMD and CommonJS lies in its support for asynchronous module loading. "The main difference between AMD and CommonJS lies in its support for asynchronous module loading."
TypeScript supports export = to model the traditional CommonJS and AMD workflow. The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum.
RequireJS implements the AMD API (source). CommonJS is a way of defining modules with the help of an exports object, that defines the module contents. Simply put, a CommonJS implementation might work like this: // someModule. js exports.
Alright, I understand. cjs (commonJS) is the module system used by e.g. Node, and umd (Universal Module Definition) is the type of modules that strive to work everywhere. If you want to include any of these files in the browser as standalone script tags without a build step like e.g. Webpack, you will want to use umd.
AMD is used in the browser (e.g RequireJS) : reason is it allows parallel download of files as network latency is a major bottleneck.
CommonJS is used in the server (e.g. nodejs) where files can be read from disk upfront, but you don't want to read a file till you try to use the code it contains.
Here is a video on the subject that further explains this : http://www.youtube.com/watch?v=KDrWLMUY0R0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With