【问题标题】:grunt-contrib-htmlmin how to ignore template tagsgrunt-contrib-htmlmin 如何忽略模板标签
【发布时间】:2013-11-19 19:57:23
【问题描述】:

我正在使用 grunt-contrib-htmlmin 来缩小骨干/underscorejs 项目中的 html,但是,当我在具有 的任何 underscorejs 模板上运行 grunt-contrib-htmlmin 时,任务输出解析错误。有没有办法 grunt-contrib-htmlmin 可以忽略 中的文本?

【问题讨论】:

  • 这并不能解决 htmlmin 的实际问题,但至少您可以将分隔符从 documentcloud.github.io/underscore/#template
  • 如何将
  • OP,你找到解决方法了吗?
  • 我没有。我仍在寻找解决方案。

标签: javascript underscore.js gruntjs


【解决方案1】:

自从您发布此问题以来,html-minifier(由grunt-contrib-htmlmin 使用)到ignore the interpolation tags 中引入了一项新功能,导致了问题。

例如,下面的 html 部分:

<div>
    <span><%= variable %></span>
</div>

现在将缩小为:

<div><span><%= variable %></span></div>

在更改之前,它会导致错误。

您可以使用demo on the website 进行测试。如果可行,您可以更新项目以使用新版本。

【讨论】:

【解决方案2】:

这个问题很老,但grunt-contrib-htmlminhtml-minifier 可以采用新的选项。

正如@mckramer 已经提到的,grunt-contrib-htmlmin 位于html-minifier 之上,因此您可以添加additional options

customAttrAssign:&lt;value&gt;

允许支持自定义属性分配表达式的正则表达式数组

customAttrSurround:&lt;value&gt;

允许支持自定义属性环绕表达式的正则表达式数组

解决方案

配置示例(用于双括号{{ }}):

var hbAttrWrapOpen = /\{\{(#|\^)[^}]+\}\}/;
var hbAttrWrapClose = /\{\{\/[^}]+\}\}/;
var hbAttrWrapPair = [hbAttrWrapOpen, hbAttrWrapClose];

htmlmin: {
  blabla: {
    options: {
      ...
      customAttrSurround: [hbAttrWrapPair]
    },
    files: [
      ...
    ]
  }
}

根据documentation,这些是唯一的限制:

...

请注意,这些表达式用于解析个人 属性+值对,因此单个 Handlebars 表达式可能不跨越 多个属性。例如,以下标记不会是 认可:

<img src="logo.svg" {{#if logo_title}}alt="{{logo_title}}" title="{{logo_title}}"{{/if}} />

相反,每个属性都必须单独包装:

<img src="logo.svg" {{#if logo_title}}alt="{{logo_title}}"{{/if}} {{#if logo_title}}title="{{logo_title}}"{{/if}} />

就是这样,只要你留意你的标记,它就会毫无问题地工作。

【讨论】:

    【解决方案3】:

    同样的解决方法,除了 Jekyll 标签的正则表达式:

    var jekyllConditionalWrapOpen = /\{\% if[^}]+\%\}/;
    var jekyllConditionalWrapClose = /\{\%[^}]+endif \%\}/;
    var jekyllConditionalWrapPair = [jekyllConditionalWrapOpen, jekyllConditionalWrapClose];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-02
      • 1970-01-01
      相关资源
      最近更新 更多