#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

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:

  1. Make sure Node.js and npm are installed.
  2. Install the package: npm install laravel-mix --save-dev.
  3. Create webpack.mix.js in the project root.
  4. Add build scripts to package.json so npm run dev and friends work.
  5. 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?

SituationRecommendation
Existing project already on Mix and workingKeep Mix until there is a reason to change
New Laravel projectUse Vite, the current default
Slow dev rebuilds on a large front endConsider 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.js in Blade instead of using mix(), 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.

Explore Course