-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Documentation for how to develop using Vagrant #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bebraw
merged 3 commits into
webpack:develop
from
SpaceK33z:feature/add-develop-using-vagrant-page
Aug 28, 2016
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| --- | ||
| title: How to Develop using Vagrant | ||
| --- | ||
|
|
||
| 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. | ||
|
|
||
| ## Configuring the Project | ||
|
|
||
| To start, make sure that the `Vagrantfile` has a static IP; | ||
|
|
||
| ```ruby | ||
| Vagrant.configure("2") do |config| | ||
| config.vm.network :private_network, ip: "10.10.10.61" | ||
| end | ||
| ``` | ||
|
|
||
| Next, install webpack and webpack-dev-server in your project; | ||
|
|
||
| ```shell | ||
| npm install webpack webpack-dev-server --save-dev | ||
| ``` | ||
|
|
||
| Make sure to have a `webpack.config.js` file. If you haven't already, use this as a minimal example to get started: | ||
|
|
||
| ```js | ||
| module.exports = { | ||
| context: __dirname, | ||
| entry: "./app.js" | ||
| } | ||
| ``` | ||
|
|
||
| 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`. | ||
|
|
||
| ```html | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <script src="/bundle.js" charset="utf-8"></script> | ||
| </head> | ||
| <body> | ||
| <h2>Heey!</h2> | ||
| </body> | ||
| </html> | ||
| ``` | ||
|
|
||
| Note that you also need to create an `app.js` file. | ||
|
|
||
| ## Running the Server | ||
|
|
||
| Now, let's run the server: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another place for a potential subtitle here. |
||
|
|
||
| ```shell | ||
| webpack-dev-server --host 0.0.0.0 --public 10.10.10.61:8080 --watch-poll | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| 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. | ||
| 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. | ||
|
|
||
| `--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. | ||
|
|
||
| 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. | ||
|
|
||
| ## Advanced Usage with nginx | ||
|
|
||
| To mimick a more production-like environment, it is also possible to proxy the webpack-dev-server with nginx. | ||
|
|
||
| In your nginx config file, add the following: | ||
|
|
||
| ```nginx | ||
| server { | ||
| location / { | ||
| proxy_pass http://127.0.0.1:8080; | ||
| proxy_http_version 1.1; | ||
| proxy_set_header Upgrade $http_upgrade; | ||
| proxy_set_header Connection "upgrade"; | ||
| error_page 502 @start-webpack-dev-server; | ||
| } | ||
|
|
||
| location @start-webpack-dev-server { | ||
| default_type text/plain; | ||
| return 502 "Please start the webpack-dev-server first."; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| The `proxy_set_header` lines are very important, because they allow the WebSocket to work correctly. | ||
|
|
||
| The command to start webpack-dev-server can then be changed to this: | ||
|
|
||
| ``` | ||
| webpack-dev-server --public 10.10.10.61 --watch-poll | ||
| ``` | ||
|
|
||
| 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. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could add a conclusion to the end. See the writer's guide for a suggested structure. |
||
|
|
||
| ## Conclusion | ||
|
|
||
| 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. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels like this would be a good place for a subtitle right after the intro. Consistency thing.