Using Laravel Mix for Asset Compilation
Laravel Mix provides a clean, fluent API for defining webpack build steps, so compiling CSS and JavaScript doesn't require hand-writing complex webpack configuration.
Basic Setup
A typical webpack.mix.js file lives at the project root and defines what should be compiled.
const mix = require('laravel-mix');
mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css');
Running the Build
npm run dev
npm run watch
npm run production
watch rebuilds automatically as files change during development; production minifies and optimizes output for deployment.
Versioning Assets
mix.js('resources/js/app.js', 'public/js')
.version();
<script src="{{ mix('js/app.js') }}"></script>
version() appends a hash to filenames so browsers reliably fetch updated assets after a deploy, rather than serving a stale cached copy.
Note: newer Laravel versions default to Vite instead of Mix, though Mix remains supported for existing projects. With assets compiled, the next topic looks at pushing real-time updates from the server to the browser.
Getting a Mix Project Running
Mix is a Node.js tool, so the build happens outside PHP. A typical setup on an existing project looks like this:
- Make sure Node.js and npm are installed.
- Install the package:
npm install laravel-mix --save-dev. - Create
webpack.mix.jsin the project root. - Add build scripts to
package.jsonsonpm run devand friends work. - Reference the output in Blade with the
mix()helper.
"scripts": {
"dev": "mix",
"watch": "mix watch",
"hot": "mix watch --hot",
"production": "mix --production"
}
The hot script enables hot module replacement, which updates the page in the browser without a full reload while you edit JavaScript or styles.
A Realistic webpack.mix.js
Real projects usually need more than one JavaScript and one stylesheet. Mix chains these steps fluently:
const mix = require('laravel-mix');
mix.js('resources/js/app.js', 'public/js')
.js('resources/js/admin.js', 'public/js')
.vue() // compile Vue single-file components
.sass('resources/sass/app.scss', 'public/css')
.postCss('resources/css/print.css', 'public/css', [
require('autoprefixer'),
])
.copy('resources/images', 'public/images')
.extract(['axios', 'lodash']) // shared vendor bundle
.sourceMaps(false, 'source-map');
if (mix.inProduction()) {
mix.version();
}
Here extract() moves libraries into a separate vendor.js, plus a small manifest.js. Libraries change less often than your own code, so browsers can keep them cached across deploys. When you use it, load all three files in your layout, in order: manifest, vendor, then app.
Calling version() only in production keeps development builds fast, while live builds get cache-busting hashes stored in public/mix-manifest.json.
Mix or Vite?
| Situation | Recommendation |
|---|---|
| Existing project already on Mix and working | Keep Mix until there is a reason to change |
| New Laravel project | Use Vite, the current default |
| Slow dev rebuilds on a large front end | Consider migrating to Vite for faster feedback |
Migrating means replacing webpack.mix.js with vite.config.js and swapping mix() calls for the @vite Blade directive, so plan it as a small project of its own.
Common Mistakes
- Hard-coding
/js/app.jsin Blade instead of usingmix(), so versioned files are never picked up. - Committing a development build to production; always run the production script in your deploy step.
- Forgetting
extract()output files in the layout, which causes blank pages. - Editing files in
public/directly; they are overwritten on the next build.
Checking the Output After a Build
After running the production script, open public/mix-manifest.json. Each original path should map to a file name with an id query string. If the page still loads old styles, clear the view cache with php artisan view:clear, confirm that the layout uses the mix() helper, and check that your web server is not caching the old files. Also confirm that your deployment pipeline runs npm ci before the build, so the exact locked package versions are installed.
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.