Swaagie/minimize

Maximum call stack size exceeded

kuki13 opened this issue · 1 comments

If the html file has a lot of tags then HTML minifier will fail with 'stackoverflow error'.
Steps to reproduce:

  • just create a html file that contains 1000 <p></p> elements

The root of the problem is probably in async.reduce method where step callback is called synchronously.

Minimize.prototype.traverse = function traverse(data, html, done) {
  var minimize = this;
  async.reduce(data, html, function reduce(html, element, step) {
//... code removed for readability
 function close(error, html) {

      if (error) return step(error);

      html += minimize.helpers.close(element);
      step(null, html);
    }
}

According to async reduce example callback should be called in async context. Documentation doesn't say anything about it but please check Common pitfalls section - possible fix.

// pointless async:
    process.nextTick(function(){
        callback(null, memo + item)
    });

Example of async reduce that will not work.

var async = require('async');
async.reduce(new Array(10000), 0, function(acc,item, callback){ 
    console.log(acc); 
    acc += 1;   
   callback(null,acc)
},function(){console.log('done')});

Example of async reduce that will work.

var async = require('async');
async.reduce(new Array(10000), 0, function(acc,item, callback){ 
    console.log(acc); 
    acc += 1;
    process.nextTick(function(){
        callback(null,acc)
    }); 
},function(){console.log('done')});

Sorry but everything is ok with the newest version of minimize plugin. For some reason minify html grunt task used wrong version of minimize. Everything is ok with the new version.