理解“目标"和“模块"在 tsconfig 中 [英] Understanding "target" and "module" in tsconfig

查看:37
本文介绍了理解“目标"和“模块"在 tsconfig 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的 tsconfig.json 文件,我将目标设置为 es5 并将模块设置为 es6

Below is my tsconfig.json file where I have set target to es5 and module to es6

{
   "compilerOptions": {
   "target": "es5",    
   "module": "es6"
   }

}

我的问题是因为模块 [import/export] 是 es6 而不是 es5 的一部分,所以转译的 javascript 代码不应该有 import/export 语句.但是即使目标是 es5 ,生成的 javascript 代码也有导入/导出语句,这怎么可能?

My question is because modules [import / export ] are part of es6 and NOT es5 , the transpiled javascript code should not be having import / export statements. But the javascript code that is generated is having import / export statements even though the target is es5 , how is it possible ?

推荐答案

模块系统独立于语言实现.ES6 (ES2015) 模块使用 import/export 语法,由模块加载器来解释.

The module system is independent of the language implementation. ES6 (ES2015) modules use the import/export syntax, and it is up to the module loader to interpret that.

这里指定使用 ES2015 模块系统,以便启用 ES6 模块语法.

Here you have specified using the ES2015 module system, so that enables the ES6 module syntax.

JavaScript 本身可能面向 ES5,并且仅使用 ES5 功能,但理论上可以使用模块加载器以及使用 ES2015 模块语法操作的代码.尽管这是可能的,但它不一定是您想要做的事情.在 ES5 JavaScript 中使用 CommonJS 或 AMD 模块更为常见.

The JavaScript itself may target ES5, and use only ES5 features, but it is theoretically possible to use a module loader with that code that operates with ES2015 module syntax. Although it is possible, it is not necessarily something you would want to do. It is more common to use CommonJS or AMD modules with ES5 JavaScript.

显然在 TypeScript 2.0 之前甚至不允许这种组合.在 TypeScript 2.0 发行说明中,它说:

Apparently this combination was not even allowed before TypeScript 2.0. In the TypeScript 2.0 release notes, it says:

"之前被标记为无效的标志组合,现在支持 target: es5 和 ‘module: es6’.这应该有助于使用基于 ES2015 的摇树器,例如 rollup."

"Previously flagged as an invalid flag combination, target: es5 and ‘module: es6’ is now supported. This should facilitate using ES2015-based tree shakers like rollup."

这篇关于理解“目标"和“模块"在 tsconfig 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆