禁用(grunt-contrib-)htmlmin中的标记完成

时间:2014-04-23 10:17:05

标签: gruntjs grunt-contrib-htmlmin

我有一个相当典型的PHP项目,其中页面的页眉/页脚部分被重用,因此放在单独的文件中,我只是从主文件中require

但是,这意味着我的_header.php - 文件以打开的<article>标记结尾(然后在_footer.php开头“关闭”)。

问题在于htmlmin将此解释为我的错误,并在article中添加了结束bodyhtml_header.php标记。我怎么'禁用'那个?我已经阅读了grunt-contrib-htmlmin html-minifier的GitHub页面而没有太多运气。

我的任务配置

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'
        }
    }
}

如果您还可以告诉我为什么minifyJS: true似乎被忽略了,那么会给我们带来分红点

感谢。

3 个答案:

答案 0 :(得分:0)

我搜索了一个解决方案,我查看了文档,看了几篇博客文章,我发现此时你所要求的是不可能

此外,有人在GitHub上问了一个类似的问题,Support for html fragments,但答案是关闭的:

  

嗯,我认为我们无法做到这一点,因为minifier确实需要具有上下文感知能力,并且无法理解如何处理任意的,部分html。

所以这是不可能的,而且(可能)将来不会实施

无法阻止该过程(使用grunt-contrib-htmlmin),因为他们的目的是专门阻止用户留下“不正确的代码”。

我没有找到任何其他等效的grunt-plugin来为你提供这种可能性。

最后我认为你只剩下两个“解决方案”了:

1)不要缩小你的php片段;

2)将您的代码划分为更多部分,仅缩小没有未关闭标记的部分,并在新的php文件中使用grunt-include-replace 或类似的插件重新统一它们。< / p>

答案 1 :(得分:0)

我知道不太理想的一个解决方案是将标记包装在:

中来使用忽略注释
<!-- htmlmin:ignore -->

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

或许您可以使用其他工具,例如HTML Clean:

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

答案 2 :(得分:0)

我遇到了同样的问题,并结合使用htmlmin:ignoreincludeAutoGeneratedTags

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">
...
相关问题