Skip to content

Commit fd21551

Browse files
committed
Documentation for how to develop using Vagrant
1 parent 1808be8 commit fd21551

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
To start, make sure that the `Vagrantfile` has a static IP;
8+
9+
```ruby
10+
Vagrant.configure("2") do |config|
11+
config.vm.network :private_network, ip: "10.10.10.61"
12+
end
13+
```
14+
15+
Next, install webpack and webpack-dev-server in your project;
16+
17+
```
18+
npm install webpack webpack-dev-server --save-dev
19+
```
20+
21+
Make sure to have a `webpack.config.js` file. If you haven't already, use this as a minimal example to get started:
22+
23+
```js
24+
module.exports = {
25+
context: __dirname,
26+
entry: "./app.js"
27+
}
28+
```
29+
30+
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`.
31+
32+
```html
33+
<!DOCTYPE html>
34+
<html>
35+
<head>
36+
<script src="/bundle.js" charset="utf-8"></script>
37+
</head>
38+
<body>
39+
<h2>Heey!</h2>
40+
</body>
41+
</html>
42+
```
43+
44+
Note that you also need to create an `app.js` file.
45+
46+
Now, let's run the server:
47+
48+
```
49+
webpack-dev-server --host 0.0.0.0 --public 10.10.10.61:8080 --watch-poll
50+
```
51+
52+
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.
53+
54+
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.
55+
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.
56+
57+
`--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.
58+
59+
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.
60+
61+
## Advanced usage with nginx
62+
63+
To mimick a more production-like environment, it is also possible to proxy the webpack-dev-server with nginx.
64+
65+
In your nginx config file, add the following:
66+
67+
```
68+
server {
69+
location / {
70+
proxy_pass http://127.0.0.1:8080;
71+
proxy_http_version 1.1;
72+
proxy_set_header Upgrade $http_upgrade;
73+
proxy_set_header Connection "upgrade";
74+
error_page 502 @start-webpack-dev-server;
75+
}
76+
77+
location @start-webpack-dev-server {
78+
default_type text/plain;
79+
return 502 "Please start the webpack-dev-server first.";
80+
}
81+
}
82+
```
83+
84+
The `proxy_set_header` lines are very important, because they allow the websocket to work correctly.
85+
86+
The command to start webpack-dev-server can then be changed to this:
87+
88+
```
89+
webpack-dev-server --public 10.10.10.61 --watch-poll
90+
```
91+
92+
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.

0 commit comments

Comments
 (0)