Home  >  Article  >  PHP Framework  >  How laravel-mix automatically compresses html template files

How laravel-mix automatically compresses html template files

藏色散人
藏色散人forward
2021-03-05 13:53:162667browse

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