Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Target different Javascript versions with TypeScript

TypeScript can globally target different versions of Javascript - you can switch between transpiling ES3, ES5 or ES6.

We have to support IE, so ES3 is our lowest common denominator.

However, good browsers (like Chrome) already support ES6, and will be able to run the significantly smaller ES6 code with optimisations.

So, from the same TypeScript source I want to serve ES3 to IE and ES6 to Chrome.

  • Is there a way to make TypeScript transpile multiple JS files (maybe as *.es3 and *.es6 or something like that) so we can pick which version to serve? (Ideally in VS 2015)

  • Alternatively in C# can I access the TypeScript transpiler to complete the step at run time?

like image 323
Keith Avatar asked Mar 12 '23 10:03

Keith


2 Answers

You can actually specify which version you want to transpile to using the command line (--target ES3).

You can also specify an output directory, so that you can output both ES6 and ES3 transpiled code, and then chose which to reference on the fly (using old style IE ifs).

like image 158
Benjamin Soulier Avatar answered Mar 20 '23 10:03

Benjamin Soulier


How about using different tsconfig.json files?

For example, something like:

 - root  
    - ts-source  
    - js-es3  
       - tsconfig.json  
       - js  
    - js-es5  
       - tsconfig.json  
       - js  

Then the root/js-es3/tsconfig.json:

{
    "compilerOptions": {
        "target": "ES3",
        "outDir": "js",
        "rootDir": "../ts-srouce"
    }
}

And the root/js-es5/tsconfig.json:

{
    "compilerOptions": {
        "target": "ES5",
        "outDir": "js",
        "rootDir": "../ts-srouce"
    }
}

I'm not a visual studio user so I don't know how to support different tsconfig.json files there, but even if you can't, then you can compile it using tsc.

like image 34
Nitzan Tomer Avatar answered Mar 20 '23 10:03

Nitzan Tomer