Version
WARNING You're browsing the documentation for an old version of Laravel. Consider upgrading your project to Laravel 10.x.
Compiling Assets [Mix]
Introduction
Laravel Mix, a package developed by Laracasts creator Jeffrey Way, provides a fluent API for defining webpack build steps for your Laravel application using several common CSS and JavaScript pre-processors.
In other words, Mix makes it a cinch to compile and minify your application's CSS and JavaScript files. Through simple method chaining, you can fluently define your asset pipeline. For example:
mix.js['resources/js/app.js', 'public/js']
.postCss['resources/css/app.css', 'public/css'];
If you've ever been confused and overwhelmed about getting started with webpack and asset compilation, you will love Laravel Mix. However, you are not required to use it while developing your application; you are free to use any asset pipeline tool you wish, or even none at all.
{tip} If you need a head start building your application with Laravel and Tailwind CSS, check out one of our application starter kits.
Installation & Setup
Installing Node
Before running Mix, you must first ensure that Node.js and NPM are installed on your machine:
node -v
npm -v
You can easily install the latest version of Node and NPM using simple graphical installers from the official Node website. Or, if you are using Laravel Sail, you may invoke Node and NPM through Sail:
./sail node -v
./sail npm -v
Installing Laravel Mix
The only remaining step is to install Laravel Mix. Within a fresh installation of Laravel, you'll find a
// Run all Mix tasks...
npm run dev
// Run all Mix tasks and minify output...
npm run prod
0 file in the root of your directory structure. The default
// Run all Mix tasks...
npm run dev
// Run all Mix tasks and minify output...
npm run prod
0 file already includes everything you need to get started using Laravel Mix. Think of this file like your
// Run all Mix tasks...
npm run dev
// Run all Mix tasks and minify output...
npm run prod
2 file, except it defines Node dependencies instead of PHP dependencies. You may install the dependencies it references by running:
npm install
Running Mix
Mix is a configuration layer on top of webpack, so to run your Mix tasks you only need to execute one of the NPM scripts that are included in the default Laravel
// Run all Mix tasks...
npm run dev
// Run all Mix tasks and minify output...
npm run prod
0 file. When you run the
// Run all Mix tasks...
npm run dev
// Run all Mix tasks and minify output...
npm run prod
4 or
// Run all Mix tasks...
npm run dev
// Run all Mix tasks and minify output...
npm run prod
5 scripts, all of your application's CSS and JavaScript assets will be compiled and placed in your application's
// Run all Mix tasks...
npm run dev
// Run all Mix tasks and minify output...
npm run prod
6 directory:
// Run all Mix tasks...
npm run dev
// Run all Mix tasks and minify output...
npm run prod
Watching Assets For Changes
The
// Run all Mix tasks...
npm run dev
// Run all Mix tasks and minify output...
npm run prod
7 command will continue running in your terminal and watch all relevant CSS and JavaScript files for changes. Webpack will automatically recompile your assets when it detects a change to one of these files:
npm run watch
Webpack may not be able to detect your file changes in certain local development environments. If this is the case on your system, consider using the
// Run all Mix tasks...
npm run dev
// Run all Mix tasks and minify output...
npm run prod
8 command:
npm run watch-poll
Working With Stylesheets
Your application's
// Run all Mix tasks...
npm run dev
// Run all Mix tasks and minify output...
npm run prod
9 file is your entry point for all asset compilation. Think of it as a light configuration wrapper around webpack. Mix tasks can be chained together to define exactly how your assets should be compiled.
Tailwind CSS
Tailwind CSS is a modern, utility-first framework for building amazing sites without ever leaving your HTML. Let's dig into how to start using it in a Laravel project with Laravel Mix. First, we should install Tailwind using NPM and generate our Tailwind configuration file:
npm install
npm install -D tailwindcss
npx tailwindcss init
The
npm run watch
0 command will generate a
npm run watch
1 file. The
npm run watch
2 section of this file allows you to configure the paths to all of your HTML templates, JavaScript components, and any other source files that contain Tailwind class names so that any CSS classes that are not used within these files will be purged from your production CSS build:
content: [
'./storage/framework/views/*.php',
'./resources/**/*.blade.php',
'./resources/**/*.js',
'./resources/**/*.vue',
],
Next, you should add each of Tailwind's "layers" to your application's
npm run watch
3 file:
@tailwind base;
@tailwind components;
@tailwind utilities;
Once you have configured Tailwind's layers, you are ready to update your application's
// Run all Mix tasks...
npm run dev
// Run all Mix tasks and minify output...
npm run prod
9 file to compile your Tailwind powered CSS:
node -v
npm -v
0
Finally, you should reference your stylesheet in your application's primary layout template. Many applications choose to store this template at
npm run watch
5. In addition, ensure you add the responsive viewport
npm run watch
6 tag if it's not already present:
node -v
npm -v
1
PostCSS
PostCSS, a powerful tool for transforming your CSS, is included with Laravel Mix out of the box. By default, Mix leverages the popular Autoprefixer plugin to automatically apply all necessary CSS3 vendor prefixes. However, you're free to add any additional plugins that are appropriate for your application.
First, install the desired plugin through NPM and include it in your array of plugins when calling Mix's
npm run watch
7 method. The
npm run watch
7 method accepts the path to your CSS file as its first argument and the directory where the compiled file should be placed as its second argument:
node -v
npm -v
2
Or, you may execute
npm run watch
7 with no additional plugins in order to achieve simple CSS compilation and minification:
node -v
npm -v
3
Sass
The
npm run watch-poll
0 method allows you to compile Sass into CSS that can be understood by web browsers. The
npm run watch-poll
0 method accepts the path to your Sass file as its first argument and the directory where the compiled file should be placed as its second argument:
node -v
npm -v
4
You may compile multiple Sass files into their own respective CSS files and even customize the output directory of the resulting CSS by calling the
npm run watch-poll
0 method multiple times:
node -v
npm -v
5
URL Processing
Because Laravel Mix is built on top of webpack, it's important to understand a few webpack concepts. For CSS compilation, webpack will rewrite and optimize any
npm run watch-poll
3 calls within your stylesheets. While this might initially sound strange, it's an incredibly powerful piece of functionality. Imagine that we want to compile Sass that includes a relative URL to an image:
node -v
npm -v
6
{note} Absolute paths for any given
npm run watch-poll
3 will be excluded from URL-rewriting. For example,
npm run watch-poll
5 or
6 won't be modified.
npm run watch-poll
By default, Laravel Mix and webpack will find
npm run watch-poll
7, copy it to your
npm run watch-poll
8 folder, and then rewrite the
npm run watch-poll
3 within your generated stylesheet. As such, your compiled CSS will be:
node -v
npm -v
7
As useful as this feature may be, your existing folder structure may already be configured in a way you like. If this is the case, you may disable
npm run watch-poll
3 rewriting like so:
node -v
npm -v
8
With this addition to your
// Run all Mix tasks...
npm run dev
// Run all Mix tasks and minify output...
npm run prod
9 file, Mix will no longer match any
npm run watch-poll
3 or copy assets to your public directory. In other words, the compiled CSS will look just like how you originally typed it:
node -v
npm -v
9
Source Maps
Though disabled by default, source maps may be activated by calling the
npm install
npm install -D tailwindcss
npx tailwindcss init
3 method in your
// Run all Mix tasks...
npm run dev
// Run all Mix tasks and minify output...
npm run prod
9 file. Though it comes with a compile/performance cost, this will provide extra debugging information to your browser's developer tools when using compiled assets:
./sail node -v
./sail npm -v
0
Style Of Source Mapping
Webpack offers a variety of . By default, Mix's source mapping style is set to
npm install
npm install -D tailwindcss
npx tailwindcss init
5, which provides a fast rebuild time. If you want to change the mapping style, you may do so using the
npm install
npm install -D tailwindcss
npx tailwindcss init
6 method:
./sail node -v
./sail npm -v
1
Working With JavaScript
Mix provides several features to help you work with your JavaScript files, such as compiling modern ECMAScript, module bundling, minification, and concatenating plain JavaScript files. Even better, this all works seamlessly, without requiring an ounce of custom configuration:
./sail node -v
./sail npm -v
2
With this single line of code, you may now take advantage of:
- The latest EcmaScript syntax.
- Modules
- Minification for production environments.
Vue
Mix will automatically install the Babel plugins necessary for Vue single-file component compilation support when using the
npm install
npm install -D tailwindcss
npx tailwindcss init
7 method. No further configuration is required:
./sail node -v
./sail npm -v
3
Once your JavaScript has been compiled, you can reference it in your application:
./sail node -v
./sail npm -v
4
React
Mix can automatically install the Babel plugins necessary for React support. To get started, add a call to the
npm install
npm install -D tailwindcss
npx tailwindcss init
8 method:
./sail node -v
./sail npm -v
5
Behind the scenes, Mix will download and include the appropriate
npm install
npm install -D tailwindcss
npx tailwindcss init
9 Babel plugin. Once your JavaScript has been compiled, you can reference it in your application:
./sail node -v
./sail npm -v
4
Vendor Extraction
One potential downside to bundling all of your application-specific JavaScript with your vendor libraries such as React and Vue is that it makes long-term caching more difficult. For example, a single update to your application code will force the browser to re-download all of your vendor libraries even if they haven't changed.
If you intend to make frequent updates to your application's JavaScript, you should consider extracting all of your vendor libraries into their own file. This way, a change to your application code will not affect the caching of your large
content: [
'./storage/framework/views/*.php',
'./resources/**/*.blade.php',
'./resources/**/*.js',
'./resources/**/*.vue',
],
0 file. Mix's
content: [
'./storage/framework/views/*.php',
'./resources/**/*.blade.php',
'./resources/**/*.js',
'./resources/**/*.vue',
],
1 method makes this a breeze:
./sail node -v
./sail npm -v
7
The
content: [
'./storage/framework/views/*.php',
'./resources/**/*.blade.php',
'./resources/**/*.js',
'./resources/**/*.vue',
],
1 method accepts an array of all libraries or modules that you wish to extract into a
content: [
'./storage/framework/views/*.php',
'./resources/**/*.blade.php',
'./resources/**/*.js',
'./resources/**/*.vue',
],
0 file. Using the snippet above as an example, Mix will generate the following files:
4: The Webpack manifest runtimecontent: [
],'./storage/framework/views/.php', './resources/*/.blade.php', './resources/*/.js', './resources/*/*.vue',
5: Your vendor librariescontent: [
],'./storage/framework/views/.php', './resources/*/.blade.php', './resources/*/.js', './resources/*/*.vue',
6: Your application codecontent: [
],'./storage/framework/views/.php', './resources/*/.blade.php', './resources/*/.js', './resources/*/*.vue',
To avoid JavaScript errors, be sure to load these files in the proper order:
./sail node -v
./sail npm -v
8
Custom Webpack Configuration
Occasionally, you may need to manually modify the underlying Webpack configuration. For example, you might have a special loader or plugin that needs to be referenced.
Mix provides a useful
content: [
'./storage/framework/views/*.php',
'./resources/**/*.blade.php',
'./resources/**/*.js',
'./resources/**/*.vue',
],
7 method that allows you to merge any short Webpack configuration overrides. This is particularly appealing, as it doesn't require you to copy and maintain your own copy of the
content: [
'./storage/framework/views/*.php',
'./resources/**/*.blade.php',
'./resources/**/*.js',
'./resources/**/*.vue',
],
8 file. The
content: [
'./storage/framework/views/*.php',
'./resources/**/*.blade.php',
'./resources/**/*.js',
'./resources/**/*.vue',
],
7 method accepts an object, which should contain any Webpack-specific configuration that you wish to apply.
./sail node -v
./sail npm -v
9
Versioning / Cache Busting
Many developers suffix their compiled assets with a timestamp or unique token to force browsers to load the fresh assets instead of serving stale copies of the code. Mix can automatically handle this for you using the
@tailwind base;
@tailwind components;
@tailwind utilities;
0 method.
The
@tailwind base;
@tailwind components;
@tailwind utilities;
0 method will append a unique hash to the filenames of all compiled files, allowing for more convenient cache busting:
npm install
0
After generating the versioned file, you won't know the exact filename. So, you should use Laravel's global
@tailwind base;
@tailwind components;
@tailwind utilities;
2 function within your views to load the appropriately hashed asset. The
@tailwind base;
@tailwind components;
@tailwind utilities;
2 function will automatically determine the current name of the hashed file:
npm install
1
Because versioned files are usually unnecessary in development, you may instruct the versioning process to only run during
@tailwind base;
@tailwind components;
@tailwind utilities;
4:
npm install
2
Custom Mix Base URLs
If your Mix compiled assets are deployed to a CDN separate from your application, you will need to change the base URL generated by the
@tailwind base;
@tailwind components;
@tailwind utilities;
2 function. You may do so by adding a
@tailwind base;
@tailwind components;
@tailwind utilities;
6 configuration option to your application's
@tailwind base;
@tailwind components;
@tailwind utilities;
7 configuration file:
npm install
3
After configuring the Mix URL, The
@tailwind base;
@tailwind components;
@tailwind utilities;
2 function will prefix the configured URL when generating URLs to assets:
npm install
4
Browsersync Reloading
BrowserSync can automatically monitor your files for changes, and inject your changes into the browser without requiring a manual refresh. You may enable support for this by calling the
@tailwind base;
@tailwind components;
@tailwind utilities;
9 method:
npm install
5
BrowserSync options may be specified by passing a JavaScript object to the
node -v
npm -v
00 method:
npm install
6
Next, start webpack's development server using the
// Run all Mix tasks...
npm run dev
// Run all Mix tasks and minify output...
npm run prod
7 command. Now, when you modify a script or PHP file you can watch as the browser instantly refreshes the page to reflect your changes.
Environment Variables
You may inject environment variables into your
// Run all Mix tasks...
npm run dev
// Run all Mix tasks and minify output...
npm run prod
9 script by prefixing one of the environment variables in your
node -v
npm -v
03 file with
node -v
npm -v
04:
npm install
7
After the variable has been defined in your
node -v
npm -v
03 file, you may access it via the
node -v
npm -v
06 object. However, you will need to restart the task if the environment variable's value changes while the task is running:
npm install
8
Notifications
When available, Mix will automatically display OS notifications when compiling, giving you instant feedback as to whether the compilation was successful or not. However, there may be instances when you would prefer to disable these notifications. One such example might be triggering Mix on your production server. Notifications may be deactivated using the
node -v
npm -v
07 method: