Home PHP Framework Laravel Laravel development: How to use Laravel Mix with CSS and JavaScript?

Laravel development: How to use Laravel Mix with CSS and JavaScript?

Jun 13, 2023 pm 02:54 PM
laravel mix css/javascript

Laravel Mix is ​​part of the Laravel web application development framework, which provides simplified processing of CSS and JavaScript. Laravel Mix is ​​based on Webpack and provides a unified API that allows developers to easily handle CSS, JavaScript and other assets.

This article will introduce the basics of Laravel Mix and show how to use Laravel Mix to handle CSS and JavaScript through examples.

Install Laravel Mix

Before installing Laravel Mix, we must install Node.js and npm. After the installation is complete, use npm to install Laravel Mix and related dependencies.

We can use the following command to install Laravel Mix in the root directory of the Laravel application:

npm install laravel-mix --save-dev
Copy after login

After the installation is complete, we can install it in the package.json file See the version information of Laravel Mix and related dependencies. In addition, we can also find the source code of Laravel Mix in the node_modules/laravel-mix directory.

Configuring Laravel Mix

Laravel Mix is ​​designed to be an easy-to-use tool. The default configuration file of Laravel Mix is ​​webpack.mix.js, where we can write simple code to compile our CSS and JavaScript.

Here is an example of compiling CSS using Laravel Mix:

const mix = require('laravel-mix');

mix.styles([
    'resources/css/app.css',
    'resources/css/extra.css'
], 'public/css/all.css');
Copy after login

What does this example do?

First, we need to use the require function to introduce Laravel Mix. Then, we use the mix constants to call the Laravel Mix API. The mix.styles() method compiles the CSS file and outputs it to public/css/all.css.

We can specify multiple CSS files and merge them into one file. We can also use the mix.sass() method to compile Sass files, the mix.less() method to compile Less files, and so on.

Here is an example of compiling JavaScript using Laravel Mix:

const mix = require('laravel-mix');

mix.js('resources/js/app.js', 'public/js')
   .js('resources/js/extra.js', 'public/js');
Copy after login

This example is derived from resources/js/app.js and resources/js/extra. jsCompiles JavaScript files and outputs them to the public/js directory.

We can also use the mix.react() method to compile ReactJS files, the mix.vue() method to compile Vue.js files, and so on.

Like CSS, we can define multiple JavaScript files in the mix.js() method to merge them into one JS file.

Here is an example of cross-referencing JavaScript and CSS files in Laravel Mix:

const mix = require('laravel-mix');

mix.js('resources/js/app.js', 'public/js')
   .sass('resources/sass/app.scss', 'public/css')
   .styles([
        'public/css/app.css',
        'public/css/extra.css'
    ], 'public/css/all.css')
   .scripts([
        'public/js/app.js',
        'public/js/extra.js'
    ], 'public/js/all.js');
Copy after login

In this example, we first compile using the mix.js() method JavaScript files. Then, we use the mix.sass() method to compile the Sass file and output it to the public/css directory.

Next, we use the mix.styles() method to combine public/css/app.css and public/css/extra.cssMerge into a CSS file and output it to public/css/all.css.

Finally, we use the mix.scripts() method to combine public/js/app.js and public/js/extra.js Merge into a JS file and output it to public/js/all.js.

It should be noted that we should manage CSS and JavaScript files separately as much as possible. This way we can more easily manage and fine-tune our assets without affecting other assets.

Compile Assets

After writing the code in the webpack.mix.js file, we can use the following command to compile the Assets:

npm run dev
Copy after login

The above command Webpack will be run and the compiled CSS and JavaScript files will be output to the public/css and public/js directories.

If we want to build in production mode when compiling Assets, we can use the following command:

npm run production
Copy after login

This command will optimize the size of the Assets file and delete unused Assets.

Conclusion

In this article, we covered the basics of Laravel Mix. We learned how to use Laravel Mix to simplify working with CSS and JavaScript files. Using Laravel Mix, we can easily compile our CSS and JavaScript files and better manage and optimize our Assets.

Fortunately, Laravel Mix is ​​built into the Laravel web application. This makes it easier for us to use Laravel Mix without worrying about the complexity of building pipelines.

The above is the detailed content of Laravel development: How to use Laravel Mix with CSS and JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get the return code when email sending fails in Laravel? How to get the return code when email sending fails in Laravel? Apr 01, 2025 pm 02:45 PM

Method for obtaining the return code when Laravel email sending fails. When using Laravel to develop applications, you often encounter situations where you need to send verification codes. And in reality...

In Laravel, how to deal with the situation where verification codes are failed to be sent by email? In Laravel, how to deal with the situation where verification codes are failed to be sent by email? Mar 31, 2025 pm 11:48 PM

The method of handling Laravel's email failure to send verification code is to use Laravel...

How to implement the custom table function of clicking to add data in dcat admin? How to implement the custom table function of clicking to add data in dcat admin? Apr 01, 2025 am 07:09 AM

How to implement the table function of custom click to add data in dcatadmin (laravel-admin) When using dcat...

Laravel Redis connection sharing: Why does the select method affect other connections? Laravel Redis connection sharing: Why does the select method affect other connections? Apr 01, 2025 am 07:45 AM

The impact of sharing of Redis connections in Laravel framework and select methods When using Laravel framework and Redis, developers may encounter a problem: through configuration...

Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Apr 01, 2025 am 09:09 AM

Custom tenant database connection in Laravel multi-tenant extension package stancl/tenancy When building multi-tenant applications using Laravel multi-tenant extension package stancl/tenancy,...

Laravel Eloquent ORM in Bangla partial model search) Laravel Eloquent ORM in Bangla partial model search) Apr 08, 2025 pm 02:06 PM

LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

How to effectively check the validity of Redis connections in Laravel6 project? How to effectively check the validity of Redis connections in Laravel6 project? Apr 01, 2025 pm 02:00 PM

How to check the validity of Redis connections in Laravel6 projects is a common problem, especially when projects rely on Redis for business processing. The following is...

Laravel database migration encounters duplicate class definition: How to resolve duplicate generation of migration files and class name conflicts? Laravel database migration encounters duplicate class definition: How to resolve duplicate generation of migration files and class name conflicts? Apr 01, 2025 pm 12:21 PM

A problem of duplicate class definition during Laravel database migration occurs. When using the Laravel framework for database migration, developers may encounter "classes have been used...

See all articles