如何同时执行 typescript watch 和 运行 server?
How do I execute typescript watch and running server at the same time?
我正在用 nodejs 开发我的项目。我发现如果我需要编码和测试 api,我会 运行 两个控制台,一个是执行 typescript watch,另一个是执行服务器。
我觉得好麻烦。我发现 github 上的其他开发人员已经在 package.json
上编写了脚本。很容易调用任何命令。它吸引了如何编写脚本和简化我的开发工作流程。
简而言之,typescript watch的命令是tsc -w
,运行ning server的命令是node app.js
。我的想法是将命令合并为 tsc -w & node app.js
但我不能同时使用这两个命令。我该怎么做?谢谢。
My idea is merge the commands as tsc -w & node app.js but I can't work the two commands at the same time. How do I do
您有几个选择。最简单的是使用 ts-node-dev
: https://github.com/whitecolor/ts-node-dev
另一个选择是使用 nodemon:
tsc -w & nodemon app.js
从 Typescript 3.4 开始,编译速度更快,因为您可以使用 incremental
compiler option and they keep improving (including interesting changes for large projects in 3.8).
更新:
我也动用了concurrently as HerberthObregon says
第 1 步
安装concurrently
,使用npm
或yarn
yarn add concurrently -D
第 2 步
使用此命令创建脚本
"scripts": {
"run": "tsc && concurrently \"tsc -w\" \"nodemon dist/app.js\"",
}
run tsc first so that your directory has something at the time of running node
然后你将拥有 运行 你的 Typescript 应用程序
TLDR,如果您喜欢 nodemon 这是获取文件监视、编译和执行的直接方法:
nodemon --ext ts --exec 'tsc && node dist/index.js'
可选择将 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",
...
},
如果需要,请安装 source-map-support 作为依赖项,咳咳……源映射支持!否则,从上面的 serve
脚本中删除 --require source-map-support/register
。
tsconfig.json
{
"compilerOptions": {
...
"sourceMap": true,
"outDir": "dist",
}
}
顺便说一句,这是一个使用 ts-node-dev
和 concurrently
的解决方案,类似于 @HerberthObregon 提供的解决方案,但使用 ts-node-dev
而不是 [=14] =]:
"scripts": {
"start": "npm run build && concurrently \"npm run build:watch\" \"npm run dev\"",
"dev": "tsnd --respawn src/main.ts",
"build": "tsc -p tsconfig.release.json",
"build:watch": "tsc -w -p tsconfig.release.json"
}
奖励:如果您在计算 tsc
和 tsconfig.json
时需要帮助,我会使用此 node typescript starter.
中的合理默认值
基于 herberthObregon 的回答
第 1 步:安装包
npm install concurrently typescript nodemon --save-dev
第 2 步:在 package.json
中创建这些脚本
"scripts": {
"build": "tsc",
"build:watch": "tsc -w",
"dev": "npm run build && concurrently \"npm run build:watch\" \"npm run serve:watch\"",
"serve": "node dist/index.js",
"serve:watch": "nodemon dist/index.js"
},
- 构建运行标准打字稿构建
- build:watch 在 watch 模式下运行 typescript build
- serve 为您的节点项目提供服务(假设您的 tsconfig 输出为 dest/index/js)
- serve:watch 使用 nodemon 在 js 输出改变时重启节点服务器
- dev 将它们放在一起
我正在用 nodejs 开发我的项目。我发现如果我需要编码和测试 api,我会 运行 两个控制台,一个是执行 typescript watch,另一个是执行服务器。
我觉得好麻烦。我发现 github 上的其他开发人员已经在 package.json
上编写了脚本。很容易调用任何命令。它吸引了如何编写脚本和简化我的开发工作流程。
简而言之,typescript watch的命令是tsc -w
,运行ning server的命令是node app.js
。我的想法是将命令合并为 tsc -w & node app.js
但我不能同时使用这两个命令。我该怎么做?谢谢。
My idea is merge the commands as tsc -w & node app.js but I can't work the two commands at the same time. How do I do
您有几个选择。最简单的是使用 ts-node-dev
: https://github.com/whitecolor/ts-node-dev
另一个选择是使用 nodemon:
tsc -w & nodemon app.js
从 Typescript 3.4 开始,编译速度更快,因为您可以使用 incremental
compiler option and they keep improving (including interesting changes for large projects in 3.8).
更新:
我也动用了concurrently as HerberthObregon says
第 1 步
安装concurrently
,使用npm
或yarn
yarn add concurrently -D
第 2 步
使用此命令创建脚本
"scripts": {
"run": "tsc && concurrently \"tsc -w\" \"nodemon dist/app.js\"",
}
run tsc first so that your directory has something at the time of running node
然后你将拥有 运行 你的 Typescript 应用程序
TLDR,如果您喜欢 nodemon 这是获取文件监视、编译和执行的直接方法:
nodemon --ext ts --exec 'tsc && node dist/index.js'
可选择将 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",
...
},
如果需要,请安装 source-map-support 作为依赖项,咳咳……源映射支持!否则,从上面的 serve
脚本中删除 --require source-map-support/register
。
tsconfig.json
{
"compilerOptions": {
...
"sourceMap": true,
"outDir": "dist",
}
}
顺便说一句,这是一个使用 ts-node-dev
和 concurrently
的解决方案,类似于 @HerberthObregon 提供的解决方案,但使用 ts-node-dev
而不是 [=14] =]:
"scripts": {
"start": "npm run build && concurrently \"npm run build:watch\" \"npm run dev\"",
"dev": "tsnd --respawn src/main.ts",
"build": "tsc -p tsconfig.release.json",
"build:watch": "tsc -w -p tsconfig.release.json"
}
奖励:如果您在计算 tsc
和 tsconfig.json
时需要帮助,我会使用此 node typescript starter.
基于 herberthObregon 的回答
第 1 步:安装包
npm install concurrently typescript nodemon --save-dev
第 2 步:在 package.json
中创建这些脚本"scripts": {
"build": "tsc",
"build:watch": "tsc -w",
"dev": "npm run build && concurrently \"npm run build:watch\" \"npm run serve:watch\"",
"serve": "node dist/index.js",
"serve:watch": "nodemon dist/index.js"
},
- 构建运行标准打字稿构建
- build:watch 在 watch 模式下运行 typescript build
- serve 为您的节点项目提供服务(假设您的 tsconfig 输出为 dest/index/js)
- serve:watch 使用 nodemon 在 js 输出改变时重启节点服务器
- dev 将它们放在一起