Search code examples
typescriptecmascript-6ecmascript-5tsconfig

Compile TypeScript to multiple targets


I want to compile Typescript files to multiple targets like to ES5 and ES6 both. I have the following example directory:

ES5

ES6

test.ts

tsconfig.json

So when I run the compiler I want it to compile test.ts as ES5 to the ES5 folder and as ES6 to the ES6 folder. Is that possible somehow?


Solution

  • A simple solution would be to create two tsconfig.json files with the different targets and output directories.

    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.
    }
    

    Then create a build script that concatenates the building, e.g. for Windows:

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

    The alternative would be to have one tsconfig.json and directly specify the target and outDir parameters in the build script (see Compiler Options).