【问题标题】:grunt-processhtml remove not working when specifying a target指定目标时,grunt-processhtml 删除不起作用
【发布时间】:2014-12-31 06:36:20
【问题描述】:

我是新来的咕噜声。在使用 grunt 构建我的 prod 环境时,我试图从我的 index.html 页面中删除一个 sn-p 代码。这是我的代码:

<!-- build:remove -->
<base href="/"></base>  
<!-- /build -->

<title>Some App</title>

<!-- build:css css/styles.min.css -->
<link href="/app/css/header.css" rel="stylesheet" />
<link href="/app/css/content.css" rel="stylesheet" />
<!-- /build -->

<!-- build:js js/scripts.head.min.js -->
<script src="/app/lib/myApp.js"></script>
<script src="/app/lib/someApp.js"></script>
<!-- /build -->

这是我的 gruntfile.coffee 代码:

    grunt.task.run("processhtml:build:#{targetEnv}")                        

这是我配置 processhtml 的方式:

_processHtml =
    options: strip: true
    build: files: 'www/index.html': ['app/index.html']

如果我将 prod 目标添加到 index.html 页面中的 build:remove 语句,则不会删除 HTML 代码。但是,如果我关闭目标('prod'),则 HTML 代码将被删除。这对我来说似乎倒退了。

所以,当我输入 grunt build:prod 时,这可以工作 - 'base' 标签已被删除:

<!-- build:remove -->
<base href="/"></base>  
<!-- /build -->

当我输入 grunt build:prod 时这不起作用 - 'base' 标签仍然存在:

<!-- build:remove:prod -->
<base href="/"></base>  
<!-- /build -->

请告诉我如何解决这个问题 - 我的代码或我的理解?谢谢。

【问题讨论】:

  • 您能发布完整的build 任务定义吗?那么grunt.task.run("processhtml:build:#{targetEnv}") 这一行在哪里?这是您在 Gruntfile 中的完整 processHtml 配置吗?

标签: node.js gruntjs grunt-plugins


【解决方案1】:

这是因为 Grunt 目标 build:prod 不存在。

您已经定义了一个build 目标,但您正在调用一个prod 目标。 Grunt 不知道这样的目标,因此什么也不做。您放在processhtml:build 之后的部分(即 processhtml:build:prod)必须对应于 Grunt 目标。

processhtml:
  options: strip: true
  prod: # <- note this target!
    files: 'www/index.html': ['app/index.html']

如果存在未提供目标的情况(由用户等),请确保您有一个默认目标(或考虑到所有目标都将被执行)。

我认为您遇到的主要困惑是这个 Grunt 任务的工作方式与其他 Grunt 任务有点不同 - 通常,任务名称后第一个冒号后指定的任何内容都映射到任务的目标。然而,grunt-processhtml 似乎在内部利用了这些信息,并将实际的目标规范移动到第二个冒号之后。

查看detailed examples 的 grunt-processhtml 任务,我相信您会发现我在上面试图描述的关系。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 2014-08-16
    • 2019-01-18
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    • 2021-06-28
    相关资源
    最近更新 更多