将 TypeScript 编译为多个目标

Compile TypeScript to multiple targets

我想将 Typescript 文件编译成多个目标,比如 ES5 和 ES6。我有以下示例目录:

ES5

ES6

test.ts

tsconfig.json

因此,当我 运行 编译器时,我希望它将 test.ts 作为 ES5 编译到 ES5 文件夹,并作为 ES6 编译到 ES6 文件夹。这有可能吗?

一个简单的解决方案是创建两个具有不同目标和输出目录的 tsconfig.json 文件。

tsconfig-es5.json

{
  "compilerOptions": {
    "target": "ES5",
    "outDir": "./ES5",
    // Additional configuration like module type etc.
}

tsconfig-es6.json

{
  "compilerOptions": {
    "target": "ES6",
    "outDir": "./ES6",
    // Additional configuration like module type etc.
}

然后创建一个连接建筑物的构建脚本,例如对于 Windows:

tsc --project ./tsconfig-es5.json && tsc --project ./tsconfig-es6.json

另一种方法是使用一个 tsconfig.json 并在构建脚本中直接指定 targetoutDir 参数(参见 Compiler Options)。