|
| 1 | +Using Encore in a Virtual Machine |
| 2 | +================================= |
| 3 | + |
| 4 | +You may encounter some issues when using Encore in a virtual machine, like VirtualBox or VMWare. |
| 5 | + |
| 6 | +Fix watching issues |
| 7 | +------------------- |
| 8 | + |
| 9 | +When using a virtual machine, your project root directory is shared with the virtual machine with `NFS`_. |
| 10 | +This is really useful, but it introduces some issues with files watching. |
| 11 | + |
| 12 | +You must enable `polling`_ option to make it work: |
| 13 | + |
| 14 | +.. code-block:: javascript |
| 15 | +
|
| 16 | + // webpack.config.js |
| 17 | +
|
| 18 | + // ... |
| 19 | +
|
| 20 | + // will be applied for `encore dev --watch` and `encore dev-server` commands |
| 21 | + Encore.configureWatchOptions(watchOptions => { |
| 22 | + watchOptions.poll = 250; // check for changes every 250 ms |
| 23 | + }); |
| 24 | +
|
| 25 | +Fix development server |
| 26 | +---------------------- |
| 27 | + |
| 28 | +Configure public path |
| 29 | +~~~~~~~~~~~~~~~~~~~~~ |
| 30 | + |
| 31 | +.. note:: |
| 32 | + |
| 33 | + You can skip this sub-section if your app is running on ``http://localhost`` |
| 34 | + and not a custom local domain-name like ``http://app.vm``. |
| 35 | + |
| 36 | +When running the development server, you will probably face the following errors in the web console: |
| 37 | + |
| 38 | +.. code-block:: text |
| 39 | +
|
| 40 | + GET http://localhost:8080/build/vendors~app.css net::ERR_CONNECTION_REFUSED |
| 41 | + GET http://localhost:8080/build/runtime.js net::ERR_CONNECTION_REFUSED |
| 42 | + ... |
| 43 | +
|
| 44 | +If your Symfony application is running on ``http://app.vm``, you must configure the public path explicitly |
| 45 | +in your ``package.json``: |
| 46 | + |
| 47 | +.. code-block:: diff |
| 48 | +
|
| 49 | + { |
| 50 | + ... |
| 51 | + "scripts": { |
| 52 | + - "dev-server": "encore dev-server", |
| 53 | + + "dev-server": "encore dev-server --public http://app.vm:8080", |
| 54 | + ... |
| 55 | + } |
| 56 | + } |
| 57 | +
|
| 58 | +After restarting Encore and reloading your web page, you will probably face different issues: |
| 59 | + |
| 60 | +.. code-block:: text |
| 61 | +
|
| 62 | + GET http://app.vm:8080/build/vendors~app.css net::ERR_CONNECTION_REFUSED |
| 63 | + GET http://app.vm:8080/build/runtime.js net::ERR_CONNECTION_REFUSED |
| 64 | +
|
| 65 | +Encore understood our modification but it's still not working. There is still two things to do. |
| 66 | + |
| 67 | +Allow external access |
| 68 | +~~~~~~~~~~~~~~~~~~~~~ |
| 69 | + |
| 70 | +You must configure how you run the `webpack-dev-server`_. |
| 71 | +This can easily be done in your ``package.json`` by adding ``--host 0.0.0.0`` argument: |
| 72 | + |
| 73 | +.. code-block:: diff |
| 74 | +
|
| 75 | + { |
| 76 | + ... |
| 77 | + "scripts": { |
| 78 | + - "dev-server": "encore dev-server --public http://app.vm:8080", |
| 79 | + + "dev-server": "encore dev-server --public http://app.vm:8080 --host 0.0.0.0", |
| 80 | + ... |
| 81 | + } |
| 82 | + } |
| 83 | +
|
| 84 | +.. warning:: |
| 85 | + |
| 86 | + Using ``--host 0.0.0.0`` makes your development server accept all incoming connections. |
| 87 | + Be sure to run the development server inside your virtual machine and not outside, otherwise other computers can have access to it. |
| 88 | + |
| 89 | +Fix "Invalid Host header" issue |
| 90 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 91 | + |
| 92 | +Webpack will respond ``Invalid Host header`` when trying to access files from the dev-server. |
| 93 | +To fix this, add the argument ``--disable-host-check``: |
| 94 | + |
| 95 | +.. code-block:: diff |
| 96 | +
|
| 97 | + { |
| 98 | + ... |
| 99 | + "scripts": { |
| 100 | + - "dev-server": "encore dev-server --public http://app.vm:8080 --host 0.0.0.0", |
| 101 | + + "dev-server": "encore dev-server --public http://app.vm:8080 --host 0.0.0.0 --disable-host-check", |
| 102 | + ... |
| 103 | + } |
| 104 | + } |
| 105 | +
|
| 106 | +.. warning:: |
| 107 | + |
| 108 | + This is usually not recommended to disable host checking, `more information here <https://webpack.js.org/configuration/dev-server/#devserverdisablehostcheck>`_. |
| 109 | + |
| 110 | +.. _`NFS`: https://en.wikipedia.org/wiki/Network_File_System |
| 111 | +.. _`polling`: https://webpack.js.org/configuration/watch/#watchoptionspoll |
| 112 | +.. _`webpack-dev-server`: https://webpack.js.org/configuration/dev-server/ |
0 commit comments