Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript 2.1 async/await ES5 __awaiter and __generator is generated for every ts file

I'm experimenting with the new rc-release of TypeScript to get the asnc/await support for ES5.

But I experience a strange behavior with the generated __awaiter and __generator methods, it seems they are generated for every single ts file if the outFile compiler option is not used.

Is there a way to generate only a single instance of the __awaiter and __generator methods while still not using outFile? The reason why I don't want to use the outFile flag is that I currently use webpack for bundling as I need to support importing of HTML template files.

like image 588
Fionn Avatar asked Nov 08 '16 23:11

Fionn


2 Answers

Use the new --importHelpers compile flag. Docs

like image 152
Cine Avatar answered Nov 06 '22 01:11

Cine


  • In tsconfig.json, under "compilerOptions" add:

    `"importHelpers": true`
    
  • install the typescript helper library as a dependency:

    npm install tslib --save
    

Typescript will attempt to import the helpers when needed and Webpack will automatically bundle tslib once. If you are publishing a library you can tell webpack to consider tslib an external with the externals option.

like image 41
Meirion Hughes Avatar answered Nov 06 '22 03:11

Meirion Hughes