2017-07-31 72 views
0

我有一個用TypeScript編寫並在節點上運行的項目。我真的很努力地用npm編寫腳本來讓它開發。 我試圖做的是:使用npm構建我的打字稿/節點項目

  • 清潔/dist文件夾
  • 如果.ts變化,它編譯成/dist並重新啓動節點

這是我第一次嘗試,從的scripts節我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 
> 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並行運行多個任務:

"clean": "rimraf dist/**/*", 
"build": "tsc", 
"watch:start": "npm-run-all clean build --parallel --race watch:build watch:serve --print-label", 
"watch:build": "tsc -w", 
"watch:serve": "nodemon dist/index.js" 

這一個效果更好,但它仍然重新啓動節點幾個時間啓動。

建議和改進歡迎!

+0

您應該首先運行tsc來編譯您的打字稿應用程序,並且同時運行tsc -w和您的服務器。 –

+0

這就是我在第二次嘗試中所做的。我在第一次嘗試「乾淨」之後添加了「構建」部分,並獲得了相同的結果。 「tsc -w」在啓動時建立所有.ts文件。 –

回答

0

您可以使用tsc-watch,它省略了nodemon的使用,並在任何更改影響應用程序的typescript源時運行。

"scripts": { 
    "dev": "tsc-watch --onSuccess \"npm start\" ", 
    "start": "node index.js" 
} 

它有一個成功處理程序--onSuccess其重新啓動服務器每次的改變,以打字稿源發。

NPM運行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" 

> [email protected] 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 

節點正在改變typecript源後重新啓動。

+0

就像一個魅力 –