search
HomePHP FrameworkLaravelHow laravel-mix automatically compresses html template files

The following tutorial column will introduce laravel-mix to automatically compress html template files. I hope it will be helpful to friends in need! laravel-mix automatically compresses html template files


Installation dependencies


cd /var/www/html/laravel-project

npm i -D laravel-mix@^5.x laravel-mix-template-minifier watch shelljs

Compression processing

Open File webpack.mix.js, and add the following content:

mix.minTemplate \= require("laravel-mix-template-minifier");

mix.minTemplate("storage/framework/views/\*.php", "storage/framework/views/", {
 collapseInlineTagWhitespace: true,
 collapseWhitespace: true,
 minifyCSS: true,
 minifyJS: true,
 processConditionalComments: true,
 removeAttributeQuotes: false,
 removeComments: true,
 removeTagWhitespace: false,
 trimCustomFragments: false,
});

Compression parameters, see html-minifier (https://github.com/kangax/html-minifier?spm= a2c6h.14275010.0.0.70f559611yXtvP)

Listen for file changes

Create a new file compress.js in the project root directory and write the following content:

let shell = require("shelljs");
let watch = require("watch");

let precessing = false;

watch.watchTree("./storage/framework/views", function(f, curr, prev) {
  if (!precessing) {
    precessing = true;
    
    shell.exec("npm run prod");
    
    setTimeout(() => {
      precessing = false;
    }, 5000);
  }
});

Auto-start monitoring command at boot

After completing the above steps, execute node compress.js on the command line to automatically monitor the pressure loss Template file, you can open the web page and see that the content in

The above is the detailed content of How laravel-mix automatically compresses html template files. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
Explain Laravel Events and Listeners functionality.Explain Laravel Events and Listeners functionality.Jul 23, 2025 am 03:22 AM

Laravel's Events and Listeners are used to decouple application modules. Events indicate "what happened", such as user registration or order payment; listeners define "what to do", such as sending emails or logging. 1. The event class is stored in app/Events, carrying relevant information; 2. The listener is stored in app/Listeners, responding to events through handle methods; 3. Bind events and listeners in EventServiceProvider, or use automatic discovery mechanism; 4. Use event() or dispatch() to trigger events; 5. The listener can implement asynchronous processing, just add the ShouldQueue interface. This mechanism improves code clearance

How to compile assets with Laravel Mix?How to compile assets with Laravel Mix?Jul 23, 2025 am 03:01 AM

LaravelMixsimplifiesassetcompilationforLaraveldevelopersbyabstractingWebpackcomplexities.Togetstarted,installitvianpminstalllaravel-mix--save-devandcreateawebpack.mix.jsfile.Thendefineyourassetsourcesandoutputpathslikemix.js('resources/js/app.js','pu

Difference between `make()` and dependency injection via type-hinting in Laravel.Difference between `make()` and dependency injection via type-hinting in Laravel.Jul 23, 2025 am 02:56 AM

Thedifferencebetweenmake()andtype-hintedinjectioninLaravelliesintheirusageandimpactoncodedesign.1.make()manuallyresolvesaclassviathecontainer,oftenusedinclosuresorlegacycodeforconditionalinstantiationbutcanleadtotightercouplingandhiddendependencies.2

How to set up Laravel queues with Redis?How to set up Laravel queues with Redis?Jul 23, 2025 am 02:50 AM

TosetupLaravelqueueswithRedis,firstinstallandconfigureRedisonyourserverorlocalenvironment,usingcommandslikesudoaptinstallredisforUbuntu/DebianorbrewinstallredisformacOS,thenstarttheRedisserver.Second,configurethequeuedriverinLaravelbysettingQUEUE_CON

How to group routes in Laravel?How to group routes in Laravel?Jul 23, 2025 am 02:40 AM

When organizing a large number of routes in Laravel, you can use routing groups to classify and manage them by modules, middleware, prefixes, etc. to improve code maintainability. 1. Use prefix to group by prefix, such as classifying background routes to /admin; 2. Use middleware to uniformly add middleware to multiple routes, such as auth or combined middleware; 3. Use namespace to specify the controller directory for easy modular management; 4. You can combine prefix, middleware and namespace to achieve a clear and efficient routing organization method.

How to define an optional route parameter in Laravel?How to define an optional route parameter in Laravel?Jul 23, 2025 am 02:39 AM

The methods for defining optional routing parameters in Laravel are as follows: 1. Add ? after the routing parameters to indicate optional, such as {name?}; 2. Set default values for parameters in the controller or closure to avoid errors; 3. Optional parameters must be at the end of the route to ensure correct resolution; 4. When using named routes, you can generate links by omitting parameters or leaving empty array items. Through these steps, optional parameters can be handled flexibly and avoid common problems such as parameter order errors or type prompt conflicts.

Writing Unit and Feature Tests in Laravel.Writing Unit and Feature Tests in Laravel.Jul 23, 2025 am 02:38 AM

ThemaindifferencebetweenunitandfeaturetestsinLaravelisthatunittestsfocusonisolatedcomponentslikeclassesormethods,whilefeaturetestssimulateuserinteractions.Unittestscheckinternallogicsuchasamethodreturningthecorrectvalue,arefast,anddonotinvolveHTTPreq

How to debug a Laravel application?How to debug a Laravel application?Jul 23, 2025 am 02:28 AM

The key points of debugging Laravel applications include: 1. Turn on debug mode, set APP_DEBUG=true through the .env file to display detailed error information; 2. Use Log::info() and dd() to view variable content; 3. Check storage/logs/laravel.log log file to track exceptions and queries; 4. Enable DB::enableQueryLog() to check SQL query performance problems; 5. Install the LaravelDebugbar plug-in to improve debugging efficiency. These methods can help quickly locate and solve problems in development.

See all articles

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment