Yeoman和Grunt,如何在构建中只包含特定内容?

3
我正在使用Yeoman来构建一个Angular应用程序。
在JavaScript中,有一部分内容只想在构建(进入dist文件夹)时包含在页面中。
是否有办法告诉grunt任务在开发期间运行'grunt server'时跳过模板的某个部分,并仅在构建时包含它?
1个回答

5
这似乎是使用grunt-processhtml的完美案例。

它允许您在HTML中放置某些指令。在您的情况下,remove指令可能是您要寻找的内容:

<!-- build:remove -->
<p>This will be removed when any process is done</p>
<!-- /build -->

<!-- build:dist:remove -->
<p>But this one only when doing processhtml:dist</p>
<!-- /build -->

当然,您也可以将代码片段反转,仅在服务器模式下删除它。如果将任务配置为在.tmp/中输出HTML,则connect服务器会自动从那里提供文件。

然后,您可以将processhtml任务添加到服务器的任务列表中,例如:

grunt.task.run([
  'clean:server',
  'concurrent:server',
  'autoprefixer',
  'processhtml:server',
  'connect:livereload',
  'open',
  'watch'
]);

并将其添加到观察部分,以便稍后的更改也会触发文件的更新:
watch: {
  html: {
    files: ['<%= yeoman.app %>/*.html'],
    tasks: ['processhtml:server']
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接