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.
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 package.json
file in the root of your directory structure. The default package.json
file already includes everything you need to get started using Laravel Mix. Think of this file like your composer.json
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 package.json
file. When you run the dev
or production
scripts, all of your application's CSS and JavaScript assets will be compiled and placed in your application's public
directory:
// Run all Mix tasks...
npm run dev
// Run all Mix tasks and minify output...
npm run prod
Watching Assets For Changes
The npm run watch
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 watch-poll
command:
npm run watch-poll
Working With Stylesheets
Your application's webpack.mix.js
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 init
command will generate a tailwind.config.js
file. The content
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 resources/css/app.css
file:
@tailwind base;
@tailwind components;
@tailwind utilities;
Once you have configured Tailwind's layers, you are ready to update your application's webpack.mix.js
file to compile your Tailwind powered CSS:
mix.js('resources/js/app.js', 'public/js')
.postCss('resources/css/app.css', 'public/css', [
require('tailwindcss'),
]);
Finally, you should reference your stylesheet in your application's primary layout template. Many applications choose to store this template at resources/views/layouts/app.blade.php
. In addition, ensure you add the responsive viewport meta
tag if it's not already present:
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="/css/app.css" rel="stylesheet">
</head>
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 postCss
method. The postCss
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:
mix.postCss('resources/css/app.css', 'public/css', [
require('postcss-custom-properties')
]);
Or, you may execute postCss
with no additional plugins in order to achieve simple CSS compilation and minification:
mix.postCss('resources/css/app.css', 'public/css');
Sass
The sass
method allows you to compile Sass into CSS that can be understood by web browsers. The sass
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:
mix.sass('resources/sass/app.scss', 'public/css');