简体   繁体   English

尝试禁止在Node环境中使用JavaScript或TypeScript文件

[英]Trying to babel either JavaScript OR TypeScript files for use in Node environment

Our node environment requires that our JavaScript files be babeled. 我们的节点环境要求我们的JavaScript文件不可混用。 This is easy enough. 这很容易。

The difficult part is that we have .js, .jsx, .ts, .tsx files and this won't change. 困难的部分是我们有.js, .jsx, .ts, .tsx文件,并且这不会改变。

Is there anything I could do to get the TypeScript files converted into JavaScript, and then have all the .js/.jsx files go through babel? 我有什么办法将TypeScript文件转换为JavaScript,然后让所有.js/.jsx文件通过babel进行?

My current command: 我当前的命令:

nodemon app.js --exec babel-node --presets env,stage-2,react

I know that I need TypeScript to be converted somewhere in there. 我知道我需要在其中的某个地方转换TypeScript。

Any ideas? 有任何想法吗?

Node version: 8.11.1 节点版本:8.11.1

With babel 7 you can use the typescript preset . 使用babel 7,您可以使用打字稿预设 You still need typescript to do the actual type checking, just the compilation to js will now be done by babel. 您仍然需要打字稿来进行实际的类型检查,现在将由babel完成对js的编译。

Here's what a simple setup might look like: 以下是一个简单的设置:

  1. npm install --save-dev @babel/preset-typescript

Update the rest of the toolchain to babel 7 too (eg babel-cli to @babel/cli @babel/core , babel-preset-env to @babel/preset-env etc.) 也将工具链的其余部分也更新到babel 7(例如,将babel-cli更改为@babel/cli @babel/core ,将babel-preset-env更改为@babel/preset-env等)。

  1. Add "noEmit": true to your tsconfig.json . 在您的tsconfig.json添加"noEmit": true This ensures typescript only does type checking and doesn't output js. 这样可以确保打字稿仅进行类型检查,而不输出js。

  2. Add --extensions '.js,.jsx,.ts,.tsx' to your command. 在命令中添加--extensions '.js,.jsx,.ts,.tsx'

So your command becomes: 因此,您的命令变为:

nodemon app.js --exec babel-node --presets @babel/env,@babel/stage-2,@babel/react,@babel/typescript --extensions '.js,.jsx,.ts,.tsx'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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