|
| 1 | +--- |
| 2 | +title: How to Develop using Vagrant |
| 3 | +--- |
| 4 | + |
| 5 | +If you have a more advanced project and use [Vagrant](https://www.vagrantup.com/) to run your development environment in a Virtual Machine, you'll often want to also run webpack in the VM. |
| 6 | + |
| 7 | +## Configuring the Project |
| 8 | + |
| 9 | +To start, make sure that the `Vagrantfile` has a static IP; |
| 10 | + |
| 11 | +```ruby |
| 12 | +Vagrant.configure("2") do |config| |
| 13 | + config.vm.network :private_network, ip: "10.10.10.61" |
| 14 | +end |
| 15 | +``` |
| 16 | + |
| 17 | +Next, install webpack and webpack-dev-server in your project; |
| 18 | + |
| 19 | +```shell |
| 20 | +npm install webpack webpack-dev-server --save-dev |
| 21 | +``` |
| 22 | + |
| 23 | +Make sure to have a `webpack.config.js` file. If you haven't already, use this as a minimal example to get started: |
| 24 | + |
| 25 | +```js |
| 26 | +module.exports = { |
| 27 | + context: __dirname, |
| 28 | + entry: "./app.js" |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +And create a `index.html` file. The script tag should point to your bundle. If `output.filename` is not specified in the config, this will be `bundle.js`. |
| 33 | + |
| 34 | +```html |
| 35 | +<!DOCTYPE html> |
| 36 | +<html> |
| 37 | + <head> |
| 38 | + <script src="/bundle.js" charset="utf-8"></script> |
| 39 | + </head> |
| 40 | + <body> |
| 41 | + <h2>Heey!</h2> |
| 42 | + </body> |
| 43 | +</html> |
| 44 | +``` |
| 45 | + |
| 46 | +Note that you also need to create an `app.js` file. |
| 47 | + |
| 48 | +## Running the Server |
| 49 | + |
| 50 | +Now, let's run the server: |
| 51 | + |
| 52 | +```shell |
| 53 | +webpack-dev-server --host 0.0.0.0 --public 10.10.10.61:8080 --watch-poll |
| 54 | +``` |
| 55 | + |
| 56 | +By default the server will only be accessible from localhost. We'll be accessing it from our host PC, so we need to change `--host` to allow this. |
| 57 | + |
| 58 | +webpack-dev-server will include a script in your bundle that connects to a WebSocket to reload when a change in any of your files occurs. |
| 59 | +The `--public` flag makes sure the script knows where to look for the WebSocket. The server will use port `8080` by default, so we should also specify that here. |
| 60 | + |
| 61 | +`--watch-poll` makes sure that webpack can detect changes in your files. By default webpack listens to events triggered by the filesystem, but VirtualBox has many problems with this. |
| 62 | + |
| 63 | +The server should be accessible on `http://10.10.10.61:8080` now! If you make a change in `app.js`, it should live reload. |
| 64 | + |
| 65 | +## Advanced Usage with nginx |
| 66 | + |
| 67 | +To mimick a more production-like environment, it is also possible to proxy the webpack-dev-server with nginx. |
| 68 | + |
| 69 | +In your nginx config file, add the following: |
| 70 | + |
| 71 | +```nginx |
| 72 | +server { |
| 73 | + location / { |
| 74 | + proxy_pass http://127.0.0.1:8080; |
| 75 | + proxy_http_version 1.1; |
| 76 | + proxy_set_header Upgrade $http_upgrade; |
| 77 | + proxy_set_header Connection "upgrade"; |
| 78 | + error_page 502 @start-webpack-dev-server; |
| 79 | + } |
| 80 | +
|
| 81 | + location @start-webpack-dev-server { |
| 82 | + default_type text/plain; |
| 83 | + return 502 "Please start the webpack-dev-server first."; |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +The `proxy_set_header` lines are very important, because they allow the WebSocket to work correctly. |
| 89 | + |
| 90 | +The command to start webpack-dev-server can then be changed to this: |
| 91 | + |
| 92 | +``` |
| 93 | +webpack-dev-server --public 10.10.10.61 --watch-poll |
| 94 | +``` |
| 95 | + |
| 96 | +This makes the server only accessible on `127.0.0.1`, which is fine, because nginx takes care of making it available on your host PC. |
| 97 | + |
| 98 | +## Conclusion |
| 99 | + |
| 100 | +We made the Vagrant box accessible from a static IP, and then made webpack-dev-server publicly accessible so it is reachable from a browser. We then tackled a common problem that VirtualBox doesn't send out filesystem events, causing the server to not reload on file changes. |
0 commit comments