Swaagie / minimize

Minimize HTML
MIT License
162 stars 18 forks source link

Maximum call stack size exceeded #69

Closed kuki13 closed 8 years ago

kuki13 commented 8 years ago

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

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')});
kuki13 commented 8 years ago

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.