Question

I have a rather typical PHP project in which the header/footer parts of pages are reused and therefore placed in separate files that I simply require from the main file.

This means, however, that my _header.php-file ends in an open <article> tag (which is then "closed" at the beginning of _footer.php).

The problem is that htmlmin interprets this as an error on my part and adds closing article, body and html tags in _header.php. How do I 'disable' that? I've read through the GitHub pages of grunt-contrib-htmlmin and html-minifier without much luck.

My Task Config

htmlmin: {                                      
    dist: {                                     
        options: {   
            minifyJS: true,
            removeComments: true,
            collapseWhitespace: true
        },
        files: {                                
            'staging/index.php': 'index.php',
            'staging/support/index.php': 'support/index.php',
            'staging/inc/_header.php': 'inc/_header.php',
            'staging/inc/_footer.php': 'inc/_footer.php'
        }
    }
}

Bonus brownie points if you can also tell my why minifyJS: true seems to be ignored

Thanks.

Was it helpful?

Solution 2

I searched for a solution, I looked at the documentation, I read several blog posts and I found out that at this time what you are asking is impossible.

Moreover someone asked a similar question on GitHub, Support for html fragments, but it was closed with the answer:

Hm, I don't think there's much we can do about this, since minifier really needs to be context aware and can't understand what to do with arbitrary, partial chunk of html.

So this is not possible and this is (probably) not going to be implemented in the future.

You can't block that process (with grunt-contrib-htmlmin) because their intent is to specifically prevent users from leaving "uncorrect code".

I did not find any other equivalent grunt-plugin that gives you that possibility.

In the end I think you have only two "solutions" left:

1)Not minifying your php fragments;

2)Divide your code into more parts, minify only the one without unclosed tags, and reunite them all using grunt-include-replace, or a similar plugin, in a new php file.

OTHER TIPS

One solution, I know not ideal, would be to use the ignore comments by wrapping your markup in:

<!-- htmlmin:ignore -->

https://github.com/kangax/html-minifier#ignoring-chunks-of-markup

Or perhaps you could use a different tool like HTML Clean:

https://www.npmjs.com/package/grunt-htmlclean

I had the same issue and fixed it with a combination of htmlmin:ignore and includeAutoGeneratedTags

grunt.js

htmlmin: {
    dist: {
      options: {
          removeComments: true,
          includeAutoGeneratedTags: false,
          collapseWhitespace: true,
          conservativeCollapse: true, 
   },
   files: [
        { src: ['<%= meta.deployPath %>/includes/footer.php'], dest: '<%= meta.deployPath %>/includes/footer.php', nonull: true},
      ]
     }
    },

footer.php

<!-- htmlmin:ignore --></div><!-- htmlmin:ignore -->
<footer id="page-footer" role="contentinfo">
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top