So you just got Webpack all set up, and you're using Vagrant or Docker. Then you
run webpack --watch
. But when you make changes in your favorite text editor,
Webpack doesn't even notice! It does nothing! Why?
First let's discuss how this normally works on Linux:
webpack --watch
, which subscribes to file changes using
inotify.Now let's discuss where this breaks down in your VM:
webpack --watch
inside your VM.But why not? Why doesn't VirtualBox use inotify to send the changes over?
Let's go to wikipedia:
Notification via inotify requires the kernel to be aware of all relevant filesystem events, which is not always possible for networked filesystems such as NFS ...
Okay, so it's hard. But surely VirtualBox will add this feature eventually right?
Nope. Issue filed, and set to wontfix.
Okay but what about VMWare? You could buy that, or maybe you're already using it.
That won't work either. According to the creator of Vagrant, inotify doesn't work with VMWare shared folders, either.
So what do we do??? Do we abandon virtualization altogether??? No.
The answer is polling. This just means that Webpack will check every few hundred milliseconds to see if your files have been updated. It won't be QUITE as snappy as you'd hoped, but it will work.
Here's the relevant portion of your Webpack config:
watchOptions: {
poll: true;
}
For more details, see the Webpack docs on watchOptions.poll.
I've been talking specifically about Webpack, but polling is the solution whenever you want a VM to notice changes made outside that VM.
Using Karma with Webpack? Add this to your karma.conf.js
:
webpackMiddleware: {
watchOptions: {
poll: true;
}
}
If polling causes your
CPU to spike, then set the
polling frequency lower. So instead of poll: true
, use poll: 300
(for 300ms)
or poll: 500
(for 500ms). Try progressively higher numbers until your CPU
usage problem goes away.