2016-01-31 42 views
0

存在我運行簡單的配置:後「HTML」任務index.html文件仍然沒有咕嘟咕嘟

gulp.task('html', function() { 
    gulp.src(config.paths.html) 
     .pipe(gulp.dest(config.paths.dist)) 
     .pipe(connect.reload()); 
}); 

gulp.task('connect', function() { 
    server.listen(config.port); 
    lrserver.listen(config.livereloadport); 
}); 

gulp.task('open', ['connect'], function() { 
    gulp.src('dist/index.html') 
     .pipe(open({ uri: config.devBaseUrl + ':' + config.port + '/'})); 
}); 

gulp.task('default', ['html', 'open']); 

但是,在第一時間對「開放」的任務文件仍然沒有index.html的存在,只有第二它創建的時間和「開放」任務將成功執行。我的配置有什麼問題?

我已經添加控制檯日誌:

D:\Projects\demo>gulp 
[11:01:13] Using gulpfile D:\Projects\demo\gulpfile.js 
[11:01:13] Starting 'connect'... 
[11:01:13] Finished 'connect' after 4.33 ms 
[11:01:13] Starting 'html'... 
[11:01:13] Finished 'html' after 9.03 ms 
[11:01:13] Starting 'open'... 
[11:01:13] Finished 'open' after 3.14 ms 
[11:01:13] Starting 'default'... 
[11:01:13] Finished 'default' after 9.82 μs 

回答

0

你的任務依賴於對方。你應該讓「開放」取決於「HTML」,所以OT將等待它完成:

gulp.task('html', function() { 
    gulp.src(config.paths.html) 
     .pipe(gulp.dest(config.paths.dist)) 
     .pipe(connect.reload()); 
}); 

gulp.task('connect', function() { 
    server.listen(config.port); 
    lrserver.listen(config.livereloadport); 
}); 

gulp.task('open', ['connect', 'html'], function() { 
    gulp.src('dist/index.html') 
     .pipe(open({ uri: config.devBaseUrl + ':' + config.port + '/'})); 
}); 

gulp.task('default', ['open']); 
+0

不幸的是,它不適合我。請查看控制檯日誌。在這兩種情況下,訂單都是正確的。怎麼了? – Sergiy

+0

嘗試將./添加到打開的src路徑的開頭。也嘗試只運行html,並確保文件存在dist/index.html –

+0

./沒有幫助。我添加了console.log(fs.existsSync('./ dist/index.html'));到'開放'任務,它在第一時間輸出錯誤,但第二次輸出錯誤。因此,文件./dist/index.html只在'默認'任務後創建。 – Sergiy

0

於是,我找到了解決辦法。默認情況下,所有gulp任務都是並行運行的。所以,你需要的任務相關,如果你想串聯運行它們,因爲回答eburger前:

gulp.task('open', ['connect', 'html'], function() { 
    gulp.src('dist/index.html') 
     .pipe(open({ uri: config.devBaseUrl + ':' + config.port + '/'})); 
}); 

但是,這在你身邊也需要得到一飲而盡知道,當先前的任務已經完成(加回報):

gulp.task('html', function() { 
    return gulp.src(config.paths.html) 
     .pipe(gulp.dest(config.paths.dist)) 
     .pipe(connect.reload()); 
}); 

這解決了我的問題。