Hello Laravel Friends,
In this blog, I will explain Compiling Assets (Mix) in Laravel 8.
Contents
Laravel Mix is a package developed by Laracasts creator Jeffrey Way. It 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 compile and minify your application’s CSS and JavaScript files.
Before running Mix, you must first ensure that Node.js and NPM are installed on your machine:
node -v npm -v
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 start using Laravel Mix.Â
npm install
The 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
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
Step 1: Take a fresh Laravel project, install all the prerequisites, and set up Laravel. For compiling assets, we install an npm using the npm install command.
Step 2: Now, create a js and stylesheet file in a location resources/js/app.js and resources/css/app.css for compiling.
Step 3: Then add the below code to your webpack.mix.js file.
mix.js('resources/js/app.js', 'public/js') Â Â Â Â .postCss('resources/css/app.css', 'public/css', [ Â Â Â Â Â Â Â Â // Â Â Â Â ]);
Step 4: Then run the development build command to your root of the project.
npm run dev
Now you can check your public folder. 2 files are created: public/css/app.css and public/js/app.js.
You can use this file asset in your blade file:
<link href="{{ mix('/css/app.css') }}” crossorigin="anonymous"> <script src="{{ mix('/js/app.js') }}"></script>
This was all about Compiling Assets (Mix) in Laravel 8. If you have any questions, share them with me through the comment below. Check out more Laravel Tutorials and stay updated with us!
Happy Coding!
In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…
In the world of business, understanding financial metrics is crucial for making informed decisions and…
Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…
In this era, businesses are always on the lookout for ways to engage with their…
Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…
E-commerce has transformed the way consumers shop for products and services and interact with businesses.…