Ton*_*ony 4 command node.js npm typescript package.json
我在nodejs中开发我的项目.我发现如果我需要代码和测试api,我会运行两个控制台,一个是执行typescript watch,另一个是执行server.
我觉得这太麻烦了.我发现github上的其他开发人员已编写过脚本package.json.调用任何命令都很容易.它吸引了如何编写脚本以及我的开发工作流程.
简而言之,打字稿表tsc -w的命令就是运行服务器的命令node app.js.我的想法是合并命令,tsc -w & node app.js但我无法同时处理这两个命令.我该怎么办?谢谢.
安装concurrently、使用npm或yarn
yarn add concurrently -D
Run Code Online (Sandbox Code Playgroud)
使用此命令创建脚本
"scripts": {
"run": "tsc && concurrently \"tsc -w\" \"nodemon dist/app.js\"",
}
Run Code Online (Sandbox Code Playgroud)
首先运行 tsc 以便您的目录在运行节点时有一些东西
这样你就可以运行你的 Typescript 应用程序了
TLDR,如果您喜欢nodemon,这是获取文件监视、编译和执行的直接方法:
nodemon --ext ts --exec 'tsc && node dist/index.js'
Run Code Online (Sandbox Code Playgroud)
可以选择将 tsc 替换为 babel 以加快编译速度。
下面是 package.json 中更完整的示例(带有源映射):
"scripts": {
"develop": "nodemon --ext ts --exec 'yarn build --incremental && yarn serve'",
"build": "tsc",
"serve": "node --require source-map-support/register dist/index.js",
...
},
Run Code Online (Sandbox Code Playgroud)
安装如果需要,source-map-support作为依赖项,咳咳...源映射支持!否则,--require source-map-support/register从serve上面的脚本中删除。
tsconfig.json
{
"compilerOptions": {
...
"sourceMap": true,
"outDir": "dist",
}
}
Run Code Online (Sandbox Code Playgroud)
我的想法是将命令合并为tsc -w和node app.js,但我无法同时处理这两个命令.我该怎么办
你有几个选择.最简单的方法是使用ts-node:https://github.com/TypeStrong/ts-node
| 归档时间: |
|
| 查看次数: |
4977 次 |
| 最近记录: |