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

    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
    How we optimised an SSL overall rating from B to A+

    Blog

    How we optimised an SSL overall rating from B to A+

    Optimising the SSL implementation allows all customers to open and browse the site securely without warnings.

    Written by Mihail Shahov
    Feb 15, 20223 min read
    Understand Drupal versions and plan a migration strategy

    Blog

    Understand Drupal versions and plan a migration strategy

    Recognise the various Drupal versions and keep your website up-to-date.

    Written by Svetoslava Angelova
    Mar 21, 20224 min read
    Drupal 9 convert image to WebP format

    Blog

    Drupal 9 convert image to WebP format

    WebP is able to take data compression to a new level thanks to the inclusion of a prediction mode to the JPG process, making it clear to see how it can outperform its JPG-based relative. And we have the results to prove it.

    Written by Vasil Boychev
    Apr 06, 20228 min read
    React overview - Definition, SPA, Components, Hooks

    Blog

    React overview - Definition, SPA, Components, Hooks

    React is a free and open-source front-end JavaScript framework for creating user interfaces based on UI components. It is also known as React.js or ReactJS.

    Written by Mihail Shahov
    May 13, 20226 min read
    What is Agile and why we use it?

    Blog

    What is Agile and why we use it?

    Agile is a time-boxed, iterative method to software delivery that aims to provide software gradually throughout the project rather than all at once near the end.

    Written by Svetoslava Angelova
    Sep 15, 20225 min read
    NVM vs NPM vs Yarn

    Blog

    NVM vs NPM vs Yarn

    Compared to the three technologies, NVM differs from the other two. Node Version Manager (NVM) is used to manage Node.js versions. NPM and Yarn are Node.js package managers. They allow downloading, installing, and managing packages when developing in JavaScript.

    Written by Ventsislav Venkov
    Sep 15, 20225 min read
    Which IT engagement model is right for you?

    Blog

    Which IT engagement model is right for you?

    Fixed price, time and materials, or dedicated teams? Consider carefully all the pros and cons of the engagement model for your project.

    Written by Svetoslava Angelova
    Sep 26, 202210 min read
    Varna and Burgas airports' websites use React components in Drupal

    Blog

    Varna and Burgas airports' websites use React components in Drupal

    Drupal is a modular system whose functions can be adapted to many different requirements, which is particularly important for public administration projects.

    Written by Mihail Shahov
    Nov 04, 20224 min read
    What is Scrum?

    Blog

    What is Scrum?

    Scrum is a part of the Agile methodology. It is the most popular framework for agile development, and it is a simple process framework.

    Written by Svetoslava Angelova
    Nov 20, 20224 min read
    Have a project you'd like to launch?

    GET IN TOUCH

    Privacy settings