简体   繁体   中英

How to have Typescript target ES5 with React in Webpack?

We have a React web app written in Typescript that works fine and targets ES6 in the tsconfig.json file.

Now we want to minify it with Uglifyjs after compiling to deploy to production. That fails because Uglify supports only ES5 (even the #harmony branch, I tried that already).

So I need the webpack output to be ES5. But when I set "target": "es5" in our tsconfig file the webpack compilation fails saying: Unexpected token (164:41) You may need an appropriate loader to handle this file type.

How can we target ES5 in Typescript with React so that it works?


Attached a screenshot of the error:

在此处输入图片说明

The answer is, you can't.

Babel can transpile ES7 to ES6. But it can't transpile ES6 to ES5 , for that it uses a polyfill (.js library loaded in the browser).

Thus, because you cannot get ES5 from your source code, you cannot use Uglify.

You'll have to rely on gzipping alone ( which is a substantial size decrease ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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