Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between module type in tsconfig.json

In tsconfig.json

{     "compilerOptions": {      "target": "es5",      "module": "commonjs",      "moduleResolution": "node",      "sourceMap": true,      "emitDecoratorMetadata": true,      "experimentalDecorators": true,      "removeComments": false,      "noImplicitAny": false   } } 

I cannot understand the difference between following module types:

"commonjs", "amd", "umd", "system", "es6", "es2015", "none"

Question: Which value should I use and when should I use it?

like image 827
kokadwarprafulla Avatar asked Dec 26 '16 04:12

kokadwarprafulla


People also ask

What is module option in Tsconfig json?

"module" in tsconfig. json tells the Typescript (TS) compiler what module syntax to use when the files are compiled to Javascript (JS). When you set "module" to "commonjs" in tsconfig. json, this means that the modules in the compiled .

What is module resolution in Tsconfig?

Module resolution is the process the compiler uses to figure out what an import refers to. Consider an import statement like import { a } from "moduleA" ; in order to check any use of a , the compiler needs to know exactly what it represents, and will need to check its definition moduleA .

What are CommonJS modules?

Getting Started. From a structure perspective, a CommonJS module is a reusable piece of JavaScript that exports specific objects made available to any dependent code. Unlike AMD, there are typically no function wrappers around such modules (so we won't see define here, for example).

How do I add a type to Tsconfig?

To do this you should edit the tsconfig. json file, and add the typeRoots property under the compilerOptions property. When the property is excluded, TypeScript is automatically searching for types in the node_modules/@types folder of your project.


1 Answers

CommonJS pattern (or nodejs):

var someOtherFunction = require('./someOtherFunction.js'); exports.add = function() {     var sum = 0, i = 0, args = arguments, l = args.length;     while (i < l) {         sum += args[i++];     }     return sum; } 

ES6 pattern:

import someOtherFunction from './someOtherFunction.js';  export function add() {     var sum = 0, i = 0, args = arguments, l = args.length;     while (i < l) {         sum += args[i++];     }     return sum; } 

AMD pattern:

define(['someOtherFunction'], function () {     return function () {         var sum = 0, i = 0, args = arguments, l = args.length;         while (i < l) {             sum += args[i++];         }         return sum;     }; }); 

Asynchronous Module Definition (AMD) is the most popular for client-side code, while node.js modules (an extension to CommonJS Modules/1.1) is the leading pattern in server-side environments.

Universal Module Definition (UMD) is a set of boilerplate recipes that attempt to bridge the differences between AMD and node.js, allowing engineers to author their code bases in a single format, rather than author in both formats or convert to the other format in a build step.

ES5 is the normal JavaScript that you used to see.

You'd be using ES6 for Angular2, also known as ECMAScript 2015.

like image 155
Milad Avatar answered Sep 22 '22 09:09

Milad