2

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

<!-- 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 -->

有什么想法可以解决这个问题 - 无论是我的代码还是我的理解?谢谢你。

4

1 回答 1

3

这是因为 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 似乎在内部利用了这些信息,并将实际的目标规范移动到第二个冒号之后。

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

于 2015-01-04T18:49:20.070 回答