This tutorial is out of date and no longer maintained.
Laravel Mix provides a fluent API for defining webpack build steps for your application using several common CSS and JavaScript pre-processors.
That is the definition taken straight from the documentation. But what does it all mean?
The lovely creators of Laravel Mix, put in the common webpack configurations and you can add more custom configurations if you wish.
This is especially wonderful for people that want to use webpack, but feel like configuring webpack is too difficult. Or maybe they wanted to use ES2016 but saw some complicated article about loaders and modules.
Laravel Mix allows you to use a single line to describe what you want and it’ll use it’s preconfigured settings to process it properly.
If you’re using Laravel 5.4 and above, then mix is already installed. All you have to do is run npm install
.
From the root of your application, run the following commands
Now in the package.json
file, add this.
Now the installation is complete.
Most of our time will be spent in the webpack.mix.js
file.
In the file, you should see this.
It is already preconfigured to compile a file at src/app.js
to dist/app.js
file and src/app.scss
to dist/app.css
.
There are several more Mix methods and you can see all of them in the default webpack.mix.js
file.
The beauty of this is that we can chain as many of these as we want and not worry about the underlying webpack build.
Supports SASS, LESS, Stylus, PostCSS, PlainCss and much more. And all you have to write is a single line.
After configuring your app, there are several commands we can run.
This builds our assets but does not minify or produce a production-ready build.
Similar to npm run dev
but will watch for changes to our assets and automatically re-compile any changed asset
Will not just refresh the page when a piece of JavaScript is changed, but it will also maintain the current state of the component in the browser.
Will compile all our assets and produce a production-ready build.
It will run all Mix tasks, and it will minify the output.
Let’s create a simple HTML “fictional” interface with some little CSS and JS. We want our folder structure to be something like this:
app/
|__public/ #webroot
| |__js/ #folder for JS files
| |__css/ #folder for CSS files
| |__media/ #folder for images and other files
|
|__resorces/
| |__scripts/ #folder for our source JS files
| |__styles/ #folder for our source SASS files
|
|__src/ #folder we want copied "as is" to the public directory.
|
package.json
webpack.mix.js
So our webpack.mix.js
file looks like this.
In the above example, we have a public
directory which serves as our root. We also have an index.html
file which will be the app’s homepage.
We want to keep all our CSS files in public/css
folder. For now, there is just one file there, the app.css
file. Since we are using SASS, we will use Laravel Mix’s sass()
method to compile the app.scss
file to app.css
. We will do the same to compile our resources/scripts/app.js
to public/js/app.js
.
The source code is available here and a demo is shown here.
For another project, we will build several static sites with the same codebase. But the source files compile to different directories. The desired folder structure is like this.
app/
|__public/ #webroot
| |__site1/
| | |__js/ #folder for JS files
| | |__css/ #folder for CSS files
| | |__media/ #folder for images and other files
| | |__index.html
| |
| |__site2/
| |__js/ #folder for JS files
| |__css/ #folder for CSS files
| |__media/ #folder for images and other files
| | |__index.html
|
|__site1/
| |__scripts/ #folder for our source JS files
| |__styles/ #folder for our source SASS files
| |__src/ #folder we want copied "as is" to the webroot
| |__media/ #folder for images and other files
| |__index.html
|
|__site2/
| |__scripts/ #folder for our source JS files
| |__styles/ #folder for our source SASS files
| |__src/ #folder we want copied "as is" to the webroot
| |__media/ #folder for images and other files
| |__index.html
|
|__package.json
|__webpack.mix.js
So we will configure our webpack.mix.js
this way.
Since both of the sites are similar and have the same dependencies, then instead of having a separate setup for each of them, we can Laravel Mix to compile them to different folders which we can then set as separate web roots for their respective sites.
Using this method prevents us from having two separate projects and having to install and maintain the same set of dependencies in both of them.
The structure is very similar to the first demo, but since Laravel Mix allows us to set the compile destination, we can easily compile to different folders which we will then use as the webroot.
We will put all the souce code for site1
in the folder app/site1/
, and site2
in app/site2/
. Inside these folders, we will have the scripts/
folder for the JavaScript files, and the styles/
folder for the SASS files. The src
folder is for the files that we simply want copied to the webroot.
The webroot for the sites will be at public/site1/
and public/site2/
respectively.
The source code is available here.
In there, the names of the sites are Imperium and JustOfada.
Laravel Mix actually has a preconfigured webpack.config.js
file that it references when it runs.
If you need to add some custom config, you can pass your additional webpack configuration to the mix.webpackConfig()
method.
To completely customize your Webpack configuration, copy the node_modules/laravel-mix/setup/webpack.config.js
file to your application’s root directory. Then edit your package.json
file, and point all of the --config
references to the newly copied configuration file.
For example.
As you can see in the demos, Laravel Mix saves a lot of time. There is no more need to worry about webpack configurations. If you have not used webpack before, this is a great entry tool. However, if you have used it before, this helps to simplify the entire process.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!