25.06.2018 Views

Full-Stack Vue.js 2 and Laravel 5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

BrowserSync<br />

Another useful development tool is BrowserSync. Similar to watch mode,<br />

BrowserSync monitors your files for changes, <strong>and</strong> when one occurs inserts the<br />

change into the browser. This saves you from having to do a manual browser<br />

refresh after every build.<br />

To use BrowserSync, you'll need to have the Yarn package manager installed. If<br />

you're running terminal comm<strong>and</strong>s from within the Vagrant Box, you're all set,<br />

as Yarn is pre-installed with Homestead. Otherwise, follow the installation<br />

instructions for Yarn here: https://yarnpkg.com/en/docs/install.<br />

BrowserSync has been integrated with Mix <strong>and</strong> can be used by chaining a call to<br />

the browserSync method in your Mix configuration. Pass an options object with the<br />

app's URL as a proxy property, for example, browserSync({ proxy: http://vuebnb.test<br />

}).<br />

We have the app's URL stored as an environment variable in the .env file, so let's<br />

get it from there rather than hard-coding into our Mix file. First, install the NPM<br />

dotenv module, which reads a .env file into a Node project: $ npm i dotenv --<br />

save-devpm<br />

Require the dotenv module at the top of the Mix configuration file <strong>and</strong> use the<br />

config method to load .env. Any environment variables will then be available as<br />

properties of the process.env object.<br />

We can now pass an options object to the browserSync method with<br />

process.env.APP_URL assigned to proxy. I also like to use the open: false option as well,<br />

which prevents BrowserSync from automatically opening a tab.<br />

webpack.mix.<strong>js</strong>: require('dotenv').config(); let mix = require('laravel-mix'); mix ...<br />

.browserSync({ proxy: process.env.APP_URL, open: false }) ;<br />

BrowserSync runs on its own port, 3000 by default. When you run npm run watch<br />

again, open a new tab at localhost:3000. After you make changes to your code<br />

you'll find they are automatically reflected in this BrowserSync tab!

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!