Laravel Mix is a simple and elegant wrapper around webpack for the 80% use case. It provides a clean, expressive API for defining basic webpack build steps for your Laravel application. With Laravel Mix, you can easily compile and manage your assets, such as JavaScript, Sass, and images.
Here's a step-by-step guide on how to use Laravel Mix for asset compilation:
Make sure you have Node.js and NPM installed on your machine. Then, navigate to your Laravel project's root directory and install Laravel Mix:
bashnpm install
This command will read your package.json
file and install the necessary dependencies, including Laravel Mix.
Laravel Mix configuration is done in the webpack.mix.js
file in your project root. Here's a simple example:
javascriptconst mix = require('laravel-mix');
mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css');
In this example, mix.js
compiles your JavaScript files, and mix.sass
compiles your Sass files. The first parameter is the source file, and the second parameter is the destination folder.
After configuring Laravel Mix, you can run the compilation process with the following command:
bashnpm run dev
This command compiles your assets for development. For production, you can use:
bashnpm run production
Laravel Mix automatically version your compiled assets by appending a unique hash to the filenames. This helps with cache busting and ensures that users always get the latest version of your assets.
Laravel Mix provides additional features such as browser synchronization, extracting vendor libraries, and more.