問題描述
鑒于我的 gulpfile.js 中的以下代碼,每次我保存或更改文件時,任務都會運行兩次而不是一次,這是為什么呢?我只想讓它運行一次.
Given the following piece of code from my gulpfile.js, everytime I save or change a file, the task runs twice instead of one single time, why is that? I just want it to run one time.
var gulp = require('gulp');
gulp.task('default', function() {
gulp.watch('server/**/*.js', function(){
console.log('This runs twice everytime I change/save a javascript file located at server/**/*.js');
});
});
我對 grunt 和名為 grunt-contrib-watch 的插件也有同樣的體驗.
I have also experienced the same with grunt and the plugin called grunt-contrib-watch.
推薦答案
似乎這個問題的答案是使用的編輯器 Coda 2 的一個功能.根據這里的一些評論和多個編輯器的測試,似乎 Coda 2 保存了一個臨時文件或類似文件,這會導致 gulp watch 函數運行兩次.
Seems like the answer to this question is a feature of the editor that was used, Coda 2. Based on some of the comments here and testing with multiple editors, it seems like Coda 2 saves a temporary file or similar and that causes the gulp watch function to be run twice.
我在使用 Coda 2 時沒有找到可行的解決方案,最終切換到 Sublime Text 3.
I have not found a viable solution to this when using Coda 2, ended up with switching to Sublime Text 3.
這篇關于Gulp.js,保存文件時監視任務運行兩次的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!