简体   繁体   English

是否有可能不在grunt中提及所有脚本文件路径?

[英]Is it possible not to mention all the script file paths in grunt?

I am using gruntjs and usemin task. 我正在使用gruntjs和usemin任务。 I added this to my html file: 我将此添加到我的html文件中:

<!-- build:js scripts/all.js -->
<script src="../../core/module.js"></script>
<script src="../../core/base/alerts/alert-item.js"></script>
<script src="../../core/base/alerts/alerts-list.js"></script>
<script src="../../core/base/base_model.js"></script>
<script src="../../core/base/cursor.js"></script>
<script src="../../core/...<another script>...."></script>
<script src="../../core/...<another script>...."></script>
<script src="../../core/...<another script>...."></script>
<script src="../../core/...<another script>...."></script>
<!-- endbuild -->

As you can see, all the scripts are located in ../../core How can I tell usemin to take first ../../core/module.js and then ../../core/**/* without mention each and every file? 如您所见,所有脚本都位于../../core如何告诉usemin首先使用../../core/module.js ,然后是../../core/**/*没有提及每个文件?
Is this possible at all? 这有可能吗?

For grunt task configurations you can use globing patterns to build a file list dynamically. 对于繁琐的任务配置,您可以使用全局模式动态创建文件列表。

Have a look at the two consecutive paragraphs on 'Globbing patterns' and 'Building the files object dynamically' in the gruntjs doc: http://gruntjs.com/configuring-tasks#globbing-patterns 看看gruntjs文档中有关“全局模式”和“动态构建文件对象”的两个连续段落: http ://gruntjs.com/configuring-tasks#globbing-patterns

If you paste your usimin task configuration, it would be possible to help you building the right pattern 如果您粘贴您的usimin任务配置,则可以帮助您建立正确的模式

EDIT: 编辑:

Based upon the comment and the fact you don't control all the code, an alternative to usemin would be to use a 'src' and 'dist' directory structure as demonstrated in the sample gruntfile to handle the bulk move/concat/uglify. 根据注释以及您无法控制所有代码的事实, usemin的替代方法是使用示例gruntfile中演示的'src'和'dist'目录结构来处理批量move / concat / uglify。

  1. In the source HTML, reference the file that will be generated in your 'dist' directory by the grunt tasks. 在源HTML中,引用grunt任务将在“ dist”目录中生成的文件。
  2. Configure grunt-watch to copy , concat and uglify the files from 'src' upon a change. 配置grunt-watch可以在更改后从'src' copyconcatuglify文件。
  3. When you need to test or upload to production, you can do it out of 'dist' without worrying about what scripts may have been added or changed in 'src'. 当您需要测试或上载到生产环境时,可以在“ dist”之外进行操作,而不必担心在“ src”中可能添加或更改了哪些脚本。
  4. If you are just adding new files from the other project, you can create and call a 'build' task that does the same three tasks as grunt-watch does in #3. 如果您只是从另一个项目中添加新文件,则可以创建并调用一个“ build”任务,该任务的执行与#3中的grunt-watch相同。

If you poke around, you can find examples of this approach in several javascript frameworks that you can borrow and modify to your needs. 如果您四处逛逛,可以在几个javascript框架中找到此方法的示例,您可以借用并修改它们以适应需要。 This article might be useful. 本文可能有用。

Original: 原版的:

Based upon this and your other question asked today, it sounds like usemin might not be the right tool for your need. 基于此以及您今天提出的其他问题,听起来usemin可能不是您所需的正确工具。 Something like require.js would cut the number of script tags in your HTML but would shift the need to enumerate them to a different file/location...and your complaint seems to be with writing all of the scripts as a manifest/list in the code. 诸如require.js之类的东西会减少HTML中脚本标记的数量,但会将枚举它们的需求转移到不同的文件/位置...而您的抱怨似乎是将所有脚本作为清单/清单写入。编码。

Grunt offers a way to handle files en masse, but that's best utilized when copying directories or doing other similar low-risk bulk work. Grunt提供了一种整体处理文件的方法,但是在复制目录或执行其他类似的低风险批量工作时,最好使用该方法。 Thus, you can find ways to get usemin to bulk load without logic, but I suggest it is generally bad practice to "load all the javascripts". 因此,您可以找到不用逻辑就可以使usemin批量加载的方法,但是我建议“加载所有javascript”通常是一种不好的做法。 ...but if that's what you want, I would ask you why a bulk concatenate and uglify isn't the answer. ...但是如果那是您想要的,我会问您为什么批量连接和丑陋不是答案。

The benefits of a manifest or explicitly listing the files become more visible as a project grows in size or complexity. 随着项目规模或复杂性的增长,清单或显式列出文件的好处变得更加明显。 An innocent mistake that adds/removes a file from the directory can cause confusion and the cause isn't instantly clear. 从目录中添加/删除文件的无辜错误可能会引起混乱,并且原因无法立即清除。 Listing the files explicitly in the HTML would cause usemin to throw an error if one was missing in the directory and later, you can also audit the directory to see what is still lingering even though it is no longer used. 如果目录中缺少文件,则在HTML中显式列出文件会导致usemin引发错误,以后,您还可以审核目录以查看仍然存在的内容,即使该文件已不再使用。 Globbing them would not offer either of these benefits. 窃取它们不会提供任何这些好处。

Without a manifest, things can be solved through judicious use of a VCS and a review of changes since the last commit...but that's not as easy or as fun as saying "Hey, script.js got deleted and that's causing usemin to barf so somebody owes me a beverage for replacing the file and getting the build working again!" 没有清单,可以通过明智地使用VCS并检查自上次提交以来的更改来解决问题……但这并不像说“嘿, script.js被删除,这导致usemin ”那样简单或有趣。所以有人欠我一杯饮料,以代替文件并让构建重新开始!”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM