繁体   English   中英

是否有可能在compilerOptions中有多个目标?

[英]Is it possible to have multiple targets in compilerOptions?

很抱歉,如果这是一个nobb问题,但我正在构建一个Angular应用程序,而我当前的tsconfig.json文件在'compilerOptions'中将'es6'作为'target':

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "types" : []
  },
  "exclude": [
    "node_modules"
  ]
}

是否有可能有多个目标? 例如,我可以添加es5吗? 我之所以要问,因为我正在阅读为es5编写的教程,但由于我项目的当前状态,我需要使用es6 - 即,我无法降级。 我的所有代码都必须符合es6 - 如果两者都包括在内,我会想象我的代码可以符合es5和es6。

请记住,您还可以创建多个tsconfig文件,并使用extends功能创建一个覆盖大多数元素的基本tsconfig,然后创建两个覆盖目标的目标tsconfig。

基本tsconfig:

{
    "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "types" : []
   },
   "exclude": [
     "node_modules"
   ]
}

ES5 tsconfig:

{
    "extends": "./base.json"
    "compilerOptions": {
        "target": "es5"
    }
}

ES6 tsconfig:

{
    "extends": "./base.json"
    "compilerOptions": {
        "target": "es6"
    }
}

然后你只需要运行你需要的任何目标: tsc -p ./es6.json

这并不意味着您的代码将同时符合两者; 但是,您可以选择将outDir属性添加到扩展基础的每个tsconfig,并将输出编译为单独的目录。

您只能为配置指定一个 目标 compilerOptionstarget属性接受单个字符串值。

ES5指定为目标不会限制您在ES6或更高版本中编写源代码,它只是确保编译的代码与仅了解ES5或更低版本的客户端兼容。 lambda表达式等功能将编译为函数表达式。

表达式如const foo = (bar) => bar.replace('\\t', ''); 将被编译为var foo = function (bar) { return bar.replace('\\t', ''); }; var foo = function (bar) { return bar.replace('\\t', ''); }; 目标设置为ES5

如果您需要将目标编译为ES5或ES6,那将取决于将使用您的应用程序的设备/浏览器,ES5在兼容性方面肯定会是一个更“安全”的选择。 这是确定ES6浏览器可比性的不错资源。

希望这有助于澄清事情!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM