简体   繁体   English

Watchify更新事件永远不会在Virtualbox中触发

[英]Watchify update event never fires in Virtualbox

I am trying to get watchify working with Gulp but it seems that the 'update' event is never fired. 我试图让Watchify与Gulp合作,但似乎“更新”事件从未被解雇。

Here's my gulpfile.js: 这是我的gulpfile.js:

"use strict";

var gulp = require('gulp');
var browserify = require('browserify');
var source = require("vinyl-source-stream");
var watchify = require('watchify');

var bundler = watchify(browserify({entries: ['./client/app/app.js'], cache: {}, packageCache: {}, fullPaths: true}));

gulp.task('js', bundle); 

function bundle() {
    console.log('bundle');
    return bundler.bundle()
    .pipe(source('bundle.js'))
    .pipe(gulp.dest('./dist'));
}

// this is never fired!
bundler.on('update', bundle);

However, when I explicitly watch the files without watchify it works: 但是,当我明确地观察文件而没有观察时它起作用:

"use strict";

var gulp = require('gulp');
var browserify = require('browserify');
var source = require("vinyl-source-stream");
var watchify = require('watchify');

function bundle() {
    console.log('bundle');
    return browserify('./client/app/app.js')
    .bundle()
    .pipe(source('bundle.js'))
    .pipe(gulp.dest('./dist/'));
}

gulp.task('browserify', bundle);

gulp.task('js', function() {
    bundle();
    gulp.watch(['client/**/*.js'], ['browserify']);
});

I've tried numerous examples but with watchify the bundle never updates. 我已经尝试了很多例子但是看看捆绑从不更新。

I'm running Gulp inside a Vagrant VM, host is OSX Yosemite, guest is Ubuntu 14.04. 我在Vagrant VM中运行Gulp,主机是OSX Yosemite,客户是Ubuntu 14.04。

Any help is greatly appreciated! 任何帮助是极大的赞赏!

Polling was added in 3.0.0. 轮询在3.0.0中添加。

You can activate it with the poll option. 您可以使用poll选项激活它。

var w = watchify(b, {
  poll: true
});

From docs: opts.poll enables polling to monitor for changes. 从docs: opts.poll启用轮询以监视更改。 If set to true, then a polling interval of 100ms is used. 如果设置为true,则使用100ms的轮询间隔。 If set to a number, then that amount of milliseconds will be the polling interval. 如果设置为数字,那么该毫秒数将是轮询间隔。 For more info see Chokidar's documentation on "usePolling" and "interval". 有关更多信息,请参阅Chokidar关于“usePolling”和“interval”的文档 This option is useful if you're watching an NFS volume. 如果您正在观看NFS卷,则此选项很有用。

This is because watchify relies on inotify to detect file changes which works great as long as they are initiated from inside your virtual machine (eg using touch on these files). 这是因为watchify依赖于inotify来检测文件更改,只要它们是从虚拟机内部启动的(例如,使用touch这些文件)就可以正常工作。 However, it won't pick up changes performed outside because of a limitation with VirtualBox that doesn't trigger appropriate events in the guest system when a file is changed in the host system. 但是,由于VirtualBox限制在主机系统中更改文件时不会触发guest 虚拟机系统中的相应事件,因此它不会获取外部执行的更改。 Apparently VMware suffers from the same problem . 显然VMware遇到了同样的问题

There are currently some discussions to fall back to polling for network mounts in watchify and chokidar (which watchify actually uses to watch files) to alleviate this particular problem: 目前有一些讨论回落到轮询网络坐骑watchifychokidar (其中watchify实际使用看文件),以缓解这方面的问题:

I was having the same issue, and I discovered that it's not that the watchify update event never fires, it's that it doesn't output any logging information by default. 我遇到了同样的问题,我发现并不是说watchify更新事件永远不会触发,而是默认情况下它不会输出任何日志记录信息。

If you change your update binding to this, I think you'll discover that it's actually firing: 如果你更新你的更新绑定,我想你会发现它实际上是在解雇:

// this is never fired!
bundler.on('update', function () {
    console.log('update event');
    bundle();
});

That's why in gulp starter they built a custom logger for bundling. 这就是为什么在gulp starter中他们为捆绑构建了一个自定义记录器


Update: Danny Moerkeke (OP) wrote: "I already tried this to verify the handler is fired and it's not. BUT your answer sent me in the right direction cause when I edit the file with vi directly in the VM it works! The workspace is mounted to a shared folder inside vagrant and I run gulp inside this VM as well. When i edit the file in my editor on the host the update event is not fired so apparently something here is not working well with vagrant shared folders. When I run gulp on the host everything works fine. The idea was to keep everything inside the VM but actually this is fine (and faster!)" 更新: Danny Moerkeke(OP)写道:“我已经尝试过这个来验证处理程序是否已被解雇但是它没有。但是你的回答让我朝着正确的方向发送,因为我直接在虚拟机中使用vi编辑文件时工作!工作空间在vagrant中安装到一个共享文件夹,我也在这个VM中运行gulp。当我在主机上的编辑器中编辑文件时,更新事件没有被触发,所以这里显然不适合使用vagrant共享文件夹。当我在主机上运行gulp一切正常。想法是将所有内容保存在VM中但实际上这很好(并且更快!)“

Setting the synced folder system to RSync in the Vagrantfile solved my problem. 在Vagrantfile中将同步文件夹系统设置为 RSync解决了我的问题。

With this feature enabled, when you change files in your editor on the host, it will be copied on the fly via RSync on the guest and trigger the watcher events accordingly. 启用此功能后,当您在主机上的编辑器中更改文件时,它将通过客户端上的RSync动态复制,并相应地触发观察者事件。

To do so, just add the following configuration to your file: 为此,只需将以下配置添加到您的文件中:

Vagrant.configure("2") do |config|
  config.vm.synced_folder ".", "/vagrant", type: "rsync",
end

In your terminal, start a vagrant session with the following command : 在终端中,使用以下命令启动vagrant会话:

vagrant up && vagrant rsync-auto

http://docs.vagrantup.com/v2/synced-folders/rsync.html http://docs.vagrantup.com/v2/synced-folders/rsync.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM