Skip to content

Laravel 5 5.1 HTML Minifying

Krisan Alfa Timur edited this page Mar 21, 2016 · 6 revisions

I recommend using Gulp, instead of a PHP package, to compress the HTML that gets generated by your Blade template files. It works very well and is very configurable. It uses gulp-htmlmin, which uses html-minifier.

Create a new task in your gulpfile.js

var htmlmin = require('gulp-htmlmin');
var gulp = require('gulp');

gulp.task('compress', function() {
    var opts = {
        collapseWhitespace:    true,
        removeAttributeQuotes: true,
        removeComments:        true,
        minifyJS:              true
    };

    return gulp.src('./storage/framework/views/*')
               .pipe(htmlmin(opts))
               .pipe(gulp.dest('./storage/framework/views/'));
});

Via create your own Elixir task

var elixir  = require('laravel-elixir'),
    gulp    = require('gulp'),
    htmlmin = require('gulp-htmlmin');

elixir.extend('compress', function() {
    new elixir.Task('compress', function() {
        return gulp.src('./storage/framework/views/*')
            .pipe(htmlmin({
                collapseWhitespace:    true,
                removeAttributeQuotes: true,
                removeComments:        true,
                minifyJS:              true,
            }))
            .pipe(gulp.dest('./storage/framework/views/'));
    })
    .watch('./storage/framework/views/*');
});

Be sure you add your task in Elixir task list:

elixir(function(mix) {
    mix.compress();
});

Call the gulp task

$ gulp compress
Clone this wiki locally