laravel-html-minify icon indicating copy to clipboard operation
laravel-html-minify copied to clipboard

Workings for laravel 5+

Open thatportugueseguy opened this issue 9 years ago • 4 comments

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 avatar Feb 23 '16 16:02 thatportugueseguy

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!

thatportugueseguy avatar Feb 23 '16 18:02 thatportugueseguy

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

wallynm avatar Mar 17 '16 04:03 wallynm

+1 Works perfectly!

rappasoft avatar Mar 25 '16 18:03 rappasoft

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

martianoff avatar Aug 24 '16 03:08 martianoff