How to Target ES6 for Server and ES5 for Client

人走茶凉 提交于 2019-12-12 12:32:49

问题


I am creating a project with node on the server and Angular 2 on the client. I want to use Typescript across the entire project. I would like to target ES6 for the server, since node supports it, but ES5 for the client. Currently my directory structure looks something like this.

├── app.js
├── something.js
├── tsconfig.json
├── wwwroot
│   ├── index.html
│   ├── main.ts
│   ├── components
│   │   ├── mycomponent.ts

I want everything above wwwroot to target ES6, but everything inside of wwwroot to target ES5. I tried putting a second tsconfig.json inside the wwwroot folder, but that didn't seem to work. I am using Atom's autocompile feature, if that is of any relevance.


回答1:


You can use babel to transpile es6 javascript to whatever flavor of ecmascript you desire.

http://babeljs.io/




回答2:


I have a similar situation, and what I'm doing is:

- project-root
  - scripts
    - tsconfig.json
  - wwwroot
    - scripts
      - tsconfig.json
    - html
      - index.html
    - styles
      - index.css

The scripts/tsconfig.json targets es6 for the server, and the wwwroot/scripts/tsconfig.json targets es5 for the client.




回答3:


You can use Babel to transpile to es5 what nodejs does not already implement, using the preset "babel-preset-es2015-node6".

You can transpile only the directories that you want to es5 using for example a script in your node package.json:

"prestart": "babel server-source --out-dir dist/source"


来源:https://stackoverflow.com/questions/40479782/how-to-target-es6-for-server-and-es5-for-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!