問題描述
我有一個 gulp.js 文件,其中包括:
I have a gulp.js file that includes:
gulp.task('default', ['watch']);
哪個啟動了監視任務
gulp.task('watch', function(){
gulp.watch(productionScripts, ['autoConcat']);
});
然后在 productionScripts 中對文件的任何保存更改,監視任務將連接文件.
Then on any saved changes to files in productionScripts, the watch task will concat the files.
我想做的是在我的 package.json 中,我想在我輸入 npm start 時啟動這個手表(這已經啟動了我的節點服務器).
What I would like to do, is in my package.json, I would like to spool up this watch when I type npm start (this already starts my node server).
package.json
package.json
"start": "node server.js",
更新--------
Ben(b3nj4m.com),我試過你所說的.手表和服務器啟動.但是,一切都運行了兩次(可能是由于編輯器,不相關),但是當我使用 gulp 啟動它時,我確實丟失了我的服務器日志.
Ben(b3nj4m.com), I tried what you stated. The watch and server start up. However, everything runs twice (probably due to the editor, not related), but I do lose my server log when I start it up with gulp.
[15:31:18] Starting 'autoConcat'...
[15:31:18] Finished 'autoConcat' after 147 ms
[15:31:19] Starting 'autoConcat'...
[15:31:19] Finished 'autoConcat' after 138 ms
[15:31:20] Starting 'autoConcat'...
[15:31:20] Finished 'autoConcat' after 127 ms
[15:31:23] Starting 'autoConcat'...
這就像在服務器因更改而重新啟動和級聯文件更改之間存在一個循環.
It's like there is a loop between the server restarting on a change, and the concatenated file changing.
推薦答案
你可以從你的 gulpfile 運行你的服務器:
You could run your server from your gulpfile:
var child = require('child_process');
var fs = require('fs');
gulp.task('default', ['server', 'watch']);
gulp.task('server', function() {
var server = child.spawn('node', ['server.js']);
var log = fs.createWriteStream('server.log', {flags: 'a'});
server.stdout.pipe(log);
server.stderr.pipe(log);
});
gulp.task('watch', function(){
gulp.watch(productionScripts, ['autoConcat']);
});
然后將您的 npm start
定義更改為:
Then change your npm start
definition to look like:
"scripts": {
"start": "gulp"
}
這篇關于當我輸入 npm start 時如何啟動 Gulp watch 任務的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!