Share this article:

Blog
Nov 15, 20224 min read

Laravel Mix - a simple and powerful wrapper around Webpack

Stefani Tashkova

Junior Developer

Laravel Mix - a simple and powerful wrapper around Webpack

Laravel Mix

We will talk about Laravel Mix. To better understand it, we will start by explaining what Webpack is.

Webpack is an incredibly powerful module bundler that prepares your JavaScript and assets for the browser and therefore Mix is a thin layer on top of webpack which serves for dynamically constructing your Webpack configuration. Though Laravel mix was originally built for Laravel projects it of course may be used for any type of application.

Overview

The latest version of Laravel Mix is version 6 and requires dependencies that are compatible with Webpack 5 or PostCSS 8. It is a free tool that automatically optimises and minifies your assets when building for production with the npx mix --production command, which we will talk about later.

Laravel Mix also ships with basic typescript and View support, and to assist with long-term caching, mix provides the mix.version() method. With versioning enabled, a unique query string ID will be appended to your assets every time your code is compiled, and upon compilation, we will see the hashed names in the mix-manifest.json file. Another great benefit of the mix API is that we can isolate or extract vendor libraries into their own files, which will of course result in a significantly smaller app.js file. With hot module replacement, we can exchange, add, or remove modules while an application is running without a full reload, and all we should do is run from the command line npx mix watch --hot to boot up a node server and monitor our bundle for changes.

Mix provides the mix.css() command for basic CSS compilation and the iPostCSS plugin ecosystem as part of our compilation, and we also have options for Sass and Less compilation. By default, Mix will pipe all of our CSS through the popular Autoprefixer PostCSS plugin. As such, we are free to use the latest CSS 3 syntax with the understanding that any necessary browser-prefixes will be applied automatically. One key concept to understand is that Mix and Webpack will rewrite any URLs with relative paths within our stylesheets, but the absolute paths will always be excluded from URL rewriting.

Installation steps

Here we can see how to install and set up Laravel Mix.

First, we should install Mix either through npm or yarn.

npm install laravel-mix --save-dev

yarn add laravel-mix

The second step is to create a mix configuration file named webpack.mix.js

touch webpack.mix.js

Then we define our compilation there. In this example, we are setting our public path and using the mix sass compilation.

const mix = require('laravel-mix');
mix.setPublicPath(`dist`);
mix.sass(‘resources/scss/app.scss’, `dist/css`);
mix.js('resources/js/app.js', 'dist/js')
 

Finally, all we have to do is run an npx mix command to trigger the appropriate Webpack build.

Useful CLI commands

  • Compiling in a Local Environment

    npm mix
  • Watch Assets for Changes

    npx mix watch
  • Polling

    npx mix watch--watch-options-poll
  • Hot Module Replacement

    npx mix watch --hot
  • Compiling for Production

    npx mix --production
  • Customize the Mix Configuration Path

    npx mix --mix-config=build/webpack.mix.js –production

Useful features

  • Mix provides some other useful features for example if we have vendor libraries that need to remain separate from our core webpack bundle we can use mix.combine() to merge or concatenate multiple files into a single file called for example merged.js.
  • Another interesting feature is browsersync that will automatically monitor our files for changes and inject any changes into the browser all without requiring a manual refresh.
  • In certain cases, it may prove easier to drop down a level and override the underlining webpack configuration directly and mix provides the mix.webpackConfig() command to allow us to do this.
  • Now let's look at the event hooks. In some scenarios we may need to execute a piece of logic before the compilation begins. For example if we need to copy a directory or move a file the mix.before() function allows this, and Mix will not begin its compilation until all hooks have been fully resolved. On the other hand, we can execute a piece of logic after Webpack has completed its compilation with mix.after() method. For example if you want to log a list of all compiled assets.

For more information, you can visit the official documentation of Laravel Mix.

SUBSCRIBE TO OUR NEWSLETTER

Share this article:

SUBSCRIBE TO OUR NEWSLETTER

Related Blog Articles

    Why startups hesitate to work with a bespoke software development agency – and how we address every concern

    Blog

    Why startups hesitate to work with a bespoke software development agency – and how we address every concern

    Startups often hesitate to work with software agencies due to concerns over cost, control, and flexibility. Discover how Bulcode’s bespoke software solutions address each challenge, ensuring growth and agility.

    Written by Svetoslava Angelova
    Nov 05, 20245 min read
    Building a high-performing Agile team: Our proven approach

    Blog

    Building a high-performing Agile team: Our proven approach

    Discover how we build high-performing Agile teams by defining clear roles, fostering collaboration, and using flexible tools.

    Written by Svetoslava Angelova
    Aug 27, 20248 min read
    Drupal 11: What to expect? Comprehensive guide to new features and enhancements

    Blog

    Drupal 11: What to expect? Comprehensive guide to new features and enhancements

    Drupal 11 is out! In this article, discover it's exciting features and improvements. Upgrade now to redefine your digital strategy with Bulcode's expert support.

    Written by Svetoslava Angelova
    Aug 05, 20247 min read
    Single Directory Components in Drupal core: A comprehensive overview

    Blog

    Single Directory Components in Drupal core: A comprehensive overview

    Explore how Single Directory Components (SDC) in Drupal Core streamline the development process by encapsulating component-related files into a single directory. Learn about the benefits of SDCs and follow a step-by-step guide to implement them in your Drupal projects.

    Written by Nikolay Tsekov
    Aug 07, 20244 min read
    Scrum events

    Blog

    Scrum events

    Scrum defines several events (sometimes called ceremonies) that occur inside each sprint: sprint planning, daily scrum, sprint review, and sprint retrospective.

    Written by Svetoslava Angelova
    Nov 22, 20223 min read
    Scrum artefacts

    Blog

    Scrum artefacts

    In software development, the term “artefact” refers to information that stakeholders and the scrum team use to describe a product that’s being developed.

    Written by Svetoslava Angelova
    Nov 23, 20222 min read
    Headless Drupal with Next.js - simple example walkthrough

    Blog

    Headless Drupal with Next.js - simple example walkthrough

    The trend recently, in web development in general, and consequently in Drupal development is to use the technology headless. The trend recently, in web development in general, and consequently in Drupal development is to use the technology headless.

    Written by Mihail Shahov
    Jan 13, 20237 min read
    Hire dedicated software developers (teams)

    Blog

    Hire dedicated software developers (teams)

    Tired of raising expenses with your in-house development team? Why not get a dedicated team at 40% to 60% of the cost?

    Written by Mihail Shahov
    Jul 17, 20203 min read
    The importance of the right development partner in your software development life cycle

    Blog

    The importance of the right development partner in your software development life cycle

    Unlocking success: The art of choosing the perfect software development partner. Explore the pivotal role of partners in software development, uncover ROI secrets, and stay ahead of industry trends in this must-read article.

    Written by Mihail Shahov
    Sep 26, 20238 min read
    Config split module tutorial for Drupal

    Blog

    Config split module tutorial for Drupal

    Very often we as developers need to work with different environments. This could sometimes lead to (un)expected problems. In Drupal 8 we use the configuration system which works pretty nice, but there are cases where the settings for the local and any other environment should be different.

    Written by Ivaylo Tsandev
    May 20, 20217 min read
    Unveiling the power duo: Next.js as the Headless frontend of Drupal 10

    Blog

    Unveiling the power duo: Next.js as the Headless frontend of Drupal 10

    Discover the dynamic synergy between Drupal 10 and Next.js, as this powerful combination reshapes the landscape of web development. Next.js, an open-source React-based framework, is seamlessly integrated as the headless frontend of Drupal 10, offering a plethora of benefits. From enhanced performance with features like automatic code splitting and server-side rendering to flexible design and SEO-friendly capabilities, this collaboration empowers developers to create high-performing, scalable, and visually appealing web applications. The efficient content management of Drupal 10 coupled with Next.js' adaptability to trends ensures a cutting-edge development approach, positioning this tandem at the forefront of modern web development practices. Embrace the future with the Drupal 10 and Next.js combination, redefining how we approach and craft dynamic online experiences.

    Written by Todor Kolev
    Feb 07, 20245 min read
    Config ignore module tutorial for Drupal

    Blog

    Config ignore module tutorial for Drupal

    Sometimes we don't want our configurations to be shared in the codebase. So what can we do in such cases?

    Written by Ivaylo Tsandev
    Jul 27, 20217 min read

    GET IN TOUCH

    Have a project you'd like to launch?