【问题标题】:Browserify/Stringify strips Knockout Comment BindingBrowserify/Stringify 去除 Knockout Comment Binding
【发布时间】:2017-03-13 18:33:02
【问题描述】:

我将 Knockout 和 Browserify 与 Stringify 一起使用。我在 cmets 中有一些 Knockout 绑定,以促进组件中迭代 [1] 内的自定义标头。 Stringify 去掉了我在淘汰赛中需要的 cmets;是否有忽略文件中的 cmets 或忽略特定类型的 cmets 的解决方案?

[1] - http://knockoutjs.com/documentation/foreach-binding.html(注 4)

【问题讨论】:

标签: javascript knockout.js stringify


【解决方案1】:

如果你使用 Browserify,你应该使用 Gulp。在您的 Stringify 转换选项中,您应该指定一个自定义 minifyOptions 对象。有一个名为 ignoreCustomComments 的选项,您可以在其中指定要从删除中排除的正则表达式数组。

browserify({
    entries: 'index.js', extensions: ['.js'], watch: config.watching
  })
    .transform(babelify, {presets: ['es2015']})
    .transform(stringify, {
      appliesTo: {includeExtensions: ['.html']},
      minify: true,
      minifyOptions: {
        ignoreCustomComments: [/^(\s*ko)/, /^(\s*\/ko)/]
      }
    })

[/^(\s*ko)/, /^(\s*\/ko)/] 将保留所有具有空格的 cmets,然后是 'ko' 或 '/ko' 注释绑定。但是,通过将 minifyOptions 设置为新对象,所有默认值将被覆盖为undefined;因此,您现在需要指定这些。这些可以找到here

用法:

<!-- Will be removed -->

<!-- ko if: true -->
<h4>This will be shown</h4>
<!-- /ko -->

<!-- ko if: false -->
<h4>This will NOT be shown</h4>
<!-- /ko -->

【讨论】:

  • 有趣。它似乎有效,但我不确定为什么它没有去掉评论关闭语句。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-08
  • 2013-08-09
  • 1970-01-01
  • 2014-05-07
  • 2012-04-13
  • 1970-01-01
相关资源
最近更新 更多