我遇到了watchify和gulp.watch之间的一些奇怪的交互问题。当我们使用带轮询的watchify时,我需要这样做,因为它在一个不稳定的环境中运行,那么gulp.watch任务只在文件第一次更改时触发。
gulpfile:
var gulp = require('gulp');
var util = require('gulp-util');
var eslint = require('gulp-eslint');
var browserify = require('browserify');
var watchify = require('watchify');
var source = require('vinyl-source-stream');
function bundle(bundler) {
return bundler
.bundle()
.on('error', (error) => util.log(util.colors.red('Error compiling scripts:'), error.message))
.pipe(source('./app.js'))
.pipe(gulp.dest('./'));
}
gulp.task('default', ['build', 'watch']);
gulp.task('build', ['lint', 'scripts']);
gulp.task('lint', function() {
gulp.src('./src/**/*.js')
.pipe(eslint());
});
gulp.task('scripts', ['lint'], function() {
var bundler = browserify({
entries: ['./src/app.js'],
cache: {},
packageCache: {},
fullPaths: true,
});
var watcher = watchify(bundler, {poll: true});
watcher.on( 'update', () => {
util.log( 'Now Updating Scripts...');
return bundle(watcher);
});
return bundle(watcher);
})
gulp.task('watch', ['build'], function() {
gulp.watch(['./src/**/*.js'], ['lint']);
})示例存储库:https://github.com/mudetroit/watchify-gulp-watch-issue
当您运行gulp并在初始编译后更改文件两次时,您将看到以下内容。
[12:02:49] Using gulpfile ~/repo/watchify-gulp-watch-issue/gulpfile.js
[12:02:49] Starting 'lint'...
[12:02:49] Finished 'lint' after 24 ms
[12:02:49] Starting 'scripts'...
[12:02:49] Finished 'scripts' after 98 ms
[12:02:49] Starting 'build'...
[12:02:49] Finished 'build' after 6.17 μs
[12:02:49] Starting 'watch'...
[12:02:49] Finished 'watch' after 8.41 ms
[12:02:49] Starting 'default'...
[12:02:49] Finished 'default' after 11 μs
[12:03:48] Starting 'lint'...
[12:03:48] Finished 'lint' after 3.87 ms
[12:03:48] Now Updating Scripts...
[12:03:55] Now Updating Scripts...有几点值得注意,如果我不使用watchify并进行完全重新编译,它就可以正常工作,但这会很慢,无法在实践中发挥作用。如果我取消轮询,它可以正常工作,但这会阻止它在使用NFS的漫游环境中工作。我整个上午都在试图了解这里发生了什么,但没有取得任何进展,所以希望我能得到一些帮助。
发布于 2015-12-10 11:04:03
所以在做了大量的挖掘之后,我们找到了the culprit。这似乎是因为吞咽和监视都在引擎盖下使用chokidar。chokidar尝试的优化导致了该问题。通过启动单独的进程暂时解决了这个问题。
https://stackoverflow.com/questions/34184748
复制相似问题