fitztrev / laravel-html-minify

Minifies the HTML output of Laravel 4 applications
https://github.com/fitztrev/laravel-html-minify/wiki/Laravel-5---5.1-HTML-Minifying
MIT License
415 stars 76 forks source link

Workings for laravel 5+ #60

Open thatportugueseguy opened 8 years ago

thatportugueseguy commented 8 years ago

Hi @fitztrev ,

Just to be clear, for laravel 5+ should I only use the gulp package? So it's a minification directly on the blade files before they are compiled, i.e., they are compiled after being minified? What about all the blade control logic and extends? no problem there?

What about caching the minified files? Is it done through laravel instead?

Sorry for leaving this here as an issue, but I think this can help other people that have the same doubt.

Thank you!

thatportugueseguy commented 8 years ago

So I decided to try it myself :)

This should help others: apparently laravel caches the files after blade compilation on the storage folder, so you just have to run the minification on the storage folder files, and they will be minified after they get compiled :D works like a charm.

Laravel changed the elixir extentions api, if I get that right: https://laravel.com/docs/5.2/elixir#writing-elixir-extensions

What I've done on the elixir-extensions.js file (using standardjs style):

const gulp = require('gulp')
const htmlmin = require('gulp-htmlmin')
const Elixir = require('laravel-elixir')
const Task = Elixir.Task

Elixir.extend('compressHtml', function(message) {
    new Task('compressHtml', function() {
        const opts = {
            collapseWhitespace:    true,
            removeAttributeQuotes: true,
            removeComments:        true,
            minifyJS:              true
        }

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

Then, on your gulpfile, after requiring elixir, you just have to:

require('./elixir-extensions')

and inside the elixir callback

mix.compressHtml()

This allows the task to be used with the gulp watch if you want. Just gulp here, no need for php intervention :)

Thanks for your help with this matter!

wallynm commented 8 years ago

+1 Worked perfectly, thanks for the explanation @thatportugueseguy!

rappasoft commented 8 years ago

+1 Works perfectly!

martianoff commented 8 years ago

I do not recommend this method. It causes random purge of cached views on high traffic websites. With empty views your website will show empty pages instead of content