Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript module creation AMD vs Common JS

Can any Typescript experts clarify when and why you would choose AMD vs Common JS for module creation when using Typescript?

like image 550
user2005121 Avatar asked Dec 05 '13 19:12

user2005121


People also ask

What is the difference between CommonJS and AMD?

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."

Does TypeScript use CommonJS?

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.

What is CommonJS and RequireJS?

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.

What is AMD CommonJS and UMD?

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.


Video Answer


1 Answers

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

like image 141
basarat Avatar answered Sep 18 '22 11:09

basarat