tsconfig.json中模块类型的区别

时间:2020-12-12 23:37:48

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"

“commonjs”,“amd”,“umd”,“system”,“es6”,“es2015”,“none”

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

问题:我应该使用哪个值以及何时使用它?

1 个解决方案

#1


4  

CommonJS pattern ( or nodejs):

CommonJS模式(或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 :

AMD模式:

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.

异步模块定义(AMD)是客户端代码中最常用的,而node.js模块(CommonJS Modules / 1.1的扩展)是服务器端环境中的主要模式。

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.

通用模块定义(** UMD)**是一组样板配方,试图弥合AMD和node.js之间的差异,允许工程师以单一格式编写代码库,而不是以两种格式创作或转换为构建步骤中的另一种格式。

ES5 is the normal javascript that you used to see.

ES5是您以前常见的javascript。

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

您将使用ES6 for Angular2,也称为ECMAScript 2015。

#1


4  

CommonJS pattern ( or nodejs):

CommonJS模式(或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 :

AMD模式:

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.

异步模块定义(AMD)是客户端代码中最常用的,而node.js模块(CommonJS Modules / 1.1的扩展)是服务器端环境中的主要模式。

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.

通用模块定义(** UMD)**是一组样板配方,试图弥合AMD和node.js之间的差异,允许工程师以单一格式编写代码库,而不是以两种格式创作或转换为构建步骤中的另一种格式。

ES5 is the normal javascript that you used to see.

ES5是您以前常见的javascript。

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

您将使用ES6 for Angular2,也称为ECMAScript 2015。