2015-11-01 27 views
10

到現在爲止,我用gulp來構建打字稿和sass文件,但現在由於一些新的構建步驟,我想統一一切並使用節點作爲單個入口點通過npm run taskName運行gulp任務)。VS代碼和任務與節點

tasks.json是相當簡單的,任務build應該運行npm run watch

{ 
    "version": "0.1.0", 
    "command": "npm", 
    "isShellCommand": true, 
    "tasks": [ 
     { 
      "taskName": "build", 
      "isBuildCommand": true, 
      "showOutput": "always", 
      "isWatching": true, 
      "args": [ 
       "run", "watch" 
      ] 
     } 
    ] 
} 

的package.json

"scripts": { 
    "watch": "gulp default", 
} 

和輸出:

gulp default build 
[14:20:54] Using gulpfile PATH_TO/gulpfile.js 
[14:20:54] Task 'build' is not in your gulpfile 
[14:20:54] Please check the documentation for proper gulpfile formatting 
npm ERR! Windows_NT 6.3.9600 
npm ERR! argv "C:\\Program Files (x86)\\nodejs\\\\node.exe" "C:\\Program Files (x86)\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "watch" "build" 
npm ERR! node v0.12.2 
npm ERR! npm v2.7.4 
npm ERR! code ELIFECYCLE 
npm ERR! [email protected] watch: `gulp default build` 
npm ERR! Exit status 1 
npm ERR! 
npm ERR! Failed at the [email protected] watch script 'gulp default build'. 
npm ERR! This is most likely a problem with the 2 package, 
npm ERR! not with npm itself. 
npm ERR! Tell the author that this fails on your system: 
npm ERR!  gulp default build 
npm ERR! You can get their info via: 
npm ERR!  npm owner ls 2 
npm ERR! There is likely additional logging output above. 
npm ERR! Please include the following file with any support request: 

基於輸出,即使認爲在中沒有任何跡象,吞嚥仍然有點用(gulpfile.json存在於根目錄中,並且在搜索解決方案時發現VS Code自動檢測到它,我認爲這可能是問題所在)。此外taskName屬性看起來像自動附加到命令行作爲一個參數是錯誤的。

較小,但工作示例(但它仍然運行一飲而盡因此打字稿是在每次保存編譯兩次):在VS代碼通過NPM

{ 
    "version": "0.1.0", 
    "command": "npm", 
    "isShellCommand": true, 
    "args": [ 
     "run", "watch" 
    ], 
    "showOutput": "always" 
} 

怎樣纔可以有多個任務與?

+0

我正在尋找類似的解決方案。關於追加到命令行的taskName,請查找'suppressTaskName'配置屬性,如[此處]所述(http://scottaddie.com/2015/10/07/harnessing-webpack-with-visual-studio-code/) – superjos

+0

也,看看[這個](https://dlaa.me/blog/post/vscodenodetask)來看看如何運行VS代碼 – superjos

回答

16

正如我在評論中提到,如果你正在尋找從運行VS代碼NPM腳本任務,找this article基本上指示創建.vscode\tasks.json類似如下:

{ 
    "version": "0.1.0", 
    "command": "npm", 
    "isShellCommand": true, 
    "suppressTaskName": true, 
    "tasks": [ 
     { 
     // Build task, Ctrl+Shift+B 
     // "npm install --loglevel info" 
     "taskName": "install", 
     "isBuildCommand": true, 
     "args": ["install", "--loglevel", "info"] 
     }, 
     { 
     // Test task, Ctrl+Shift+T 
     // "npm test" 
     "taskName": "test", 
     "isTestCommand": true, 
     "args": ["test"] 
     }, 
     { 
     // "npm run lint" 
     "taskName": "lint", 
     "args": ["run", "lint"] 
     } 
    ] 
} 

作爲替代,有還有一個來自Microsoft的示例VS代碼擴展,專門用於檢測和運行npm腳本項目:vscode-npm-scripts

+0

的npm腳本任務,但是如何運行tsc編譯器,如果主命令是npm ?每個任務都不能有自己的命令? – Kokodoko

+0

對不起,我不明白。如果你有一個基於grunt/gulp的工具鏈,它應該是grunt/gulp的職責,在其任務/步驟之一中運行tsc編譯器。如果是這樣的話,我想你只是從npm腳本中調用特定的grunt/gulp任務。請澄清更多,如果我得到了錯誤 – superjos

+0

謝謝你的答案。在我寫這個問題的時候,如果我沒有記錯,上面的解決方案不起作用。但現在是正確的答案。 –