我有一个用TypeScript编写并在Node上运行的项目。我真的很难用npm编写脚本来让它运行以进行开发。 我想做的是:
/dist
文件夹.ts
更改,请将其编译为/dist
并重新启动节点这是我的第一次尝试,来自scripts
的{{1}}部分:
package.json
如果我用"clean": "rimraf dist/**/*",
"build": "tsc",
"watch:start": "npm run clean && nodemon -e ts --exec \"npm run start\"",
"start": "npm run build && node dist/index.js"
开始我的项目,它会陷入循环:
npm run watch:start
这是我的第二次尝试,使用npm run watch:start
> nodemon -e ts --exec "npm run start"
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `npm run start`
[nodemon] restarting due to changes...
[nodemon] restarting due to changes...
[nodemon] starting `npm run start`
[nodemon] restarting due to changes...
[nodemon] restarting due to changes...
并行运行多个任务:
npm-run-all
这个工作效果更好,但在启动时仍会多次重启节点。
欢迎提出建议和改进!
答案 0 :(得分:0)
您可以使用tsc-watch,它会省略nodemon的使用,并在任何更改影响应用的打字稿来源时运行。
"scripts": {
"dev": "tsc-watch --onSuccess \"npm start\" ",
"start": "node index.js"
}
它有一个成功处理程序--onSuccess
,每次对打字稿源进行更改时都会重新启动服务器。
npm run dev
index.js
console.log('node run')
setTimeout(() => console.log(Math.random() * 1000.0), 1000);
控制台
npm run dev
> tsc && concurrently "npm run node-tsc:w"
npm WARN invalid config loglevel="notice"
> jsperf.com@0.1.0 node-tsc:w C:\Users\Shane\Desktop\so
> tsc-watch --onSuccess "node index.js"
16:49:31 - Compilation complete. Watching for file changes.
node run
709.2226373633507
16:49:36 - File change detected. Starting incremental compilation...
16:49:37 - Compilation complete. Watching for file changes.
node run
974.6349162351444
16:49:41 - File change detected. Starting incremental compilation...
16:49:41 - Compilation complete. Watching for file changes.
node run
935.9043001938232
更改类型转换源后重新启动节点。