Skip to content

Laravel 5 5.1 HTML Minifying

Garrett Grimm edited this page Feb 27, 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,
        ignoreCustomFragments: [/<\?php[\s\S]*?\?>/g]
    };

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

Call the gulp task

$ gulp compress
Clone this wiki locally