注册代码位于外部 JavaScript 文件中的 Grunt 任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18637827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Registering Grunt tasks whose code is located in external JavaScript files
提问by ?ime Vidas
I've written a function which I'd like to use as a Grunt task. I can do this by adding this to the Gruntfile:
我已经编写了一个函数,我想将其用作 Grunt 任务。我可以通过将它添加到 Gruntfile 来做到这一点:
grunt.registerTask('foo', function () {
// code here
});
However, it makes more sense to keep the function code in a separate file. I plan to define a bunch of these custom tasks and I don't want to bloat the Gruntfile.
但是,将函数代码保存在单独的文件中更有意义。我计划定义一堆这些自定义任务,我不想让 Gruntfile 膨胀。
I'm not sure what the preferred way of registering such tasks is. I have found this to work:
我不确定注册此类任务的首选方式是什么。我发现这有效:
grunt.registerTask('foo', function () {
require('./path/to/foo.js')(grunt);
});
So, I'm having the inline function like in the fist example, but this time, I'm loading an external file and invoking it immediately. In that external file, I of course have to write:
所以,我使用了第一个示例中的内联函数,但这一次,我正在加载一个外部文件并立即调用它。在那个外部文件中,我当然必须写:
module.exports = function (grunt) {
// code here
}
This works, but it feels hackish. Is there a more proper way of doing this?
这有效,但感觉很hackish。有没有更合适的方法来做到这一点?
回答by Krasimir
Short answer: the alternative to this
简短回答:替代方案
grunt.registerTask('foo', function () {
require('./path/to/foo.js')(grunt);
});
is http://gruntjs.com/api/grunt#grunt.loadtasks
是http://gruntjs.com/api/grunt#grunt.loadtasks
Long answer:
长答案:
Normally when you have tasks in external files there are served as other nodejs modules. So, if that is something that you will use in several projects you may want to register it in the registry. Later inside your Gruntfile.js you will have:
通常,当您在外部文件中有任务时,它会用作其他 nodejs 模块。因此,如果您将在多个项目中使用它,您可能希望在注册表中注册它。稍后在您的 Gruntfile.js 中,您将拥有:
grunt.loadNpmTasks('yout-module-here');
The grunt's documentation says:
咕噜的文档说:
Load tasks from the specified Grunt plugin. This plugin must be installed locally via npm, and must be relative to the Gruntfile
However, if you don't want to upload anything to the registry you should use loadTasks
但是,如果您不想将任何内容上传到注册表,则应使用loadTasks
grunt.loadTasks('path/to/your/task/directory');
So, once the task is loaded you may use it in your configuration.
因此,一旦加载了任务,您就可以在配置中使用它。
Here is a simple grunt task placed in external file:
这是放置在外部文件中的一个简单的 grunt 任务:
'use strict';
module.exports = function(grunt) {
grunt.registerMultiTask('nameoftask', 'description', function() {
var self = this;
// this.data here contains your configuration
});
};
And later in Gruntfile.js
后来在 Gruntfile.js
grunt.initConfig({
nameoftask: {
task: {
// parameters here
}
}
});
回答by 0gust1
I had a similar problem.
我有一个类似的问题。
I wanted to modularize my grunt config and custom tasks by functionnalities (big UX/UI blocks) rather than by technical features. AND I wanted to keep the config files next to task files... (better when working on a large legacy codebase with an varied team - 5 persons with varying JS knowledge)
我想通过功能(大的 UX/UI 块)而不是技术特性来模块化我的 grunt 配置和自定义任务。而且我想将配置文件保留在任务文件旁边......(在与不同团队一起处理大型遗留代码库时更好 - 5 个人具有不同的 JS 知识)
So I externalized my tasks like Krasimir did.
所以我像 Krasimir 那样将我的任务具体化。
In the gruntfile, I wrote :
在 gruntfile 中,我写道:
//power of globbing for loading tasks
var tasksLocations = ['./grunt-config/default_tasks.js', './grunt-config/**/tasks.js'];
var taskFiles = grunt.file.expand({
filter: "isFile"
}, tasksLocations);
taskFiles.forEach(function(path) {
grunt.log.writeln("=> loading & registering : " + path);
require(path)(grunt);
});
You will find the whole boilerplate gruntfile here (external config and tasks loading) :https://gist.github.com/0gust1/7683132
你会在这里找到整个样板 gruntfile(外部配置和任务加载):https ://gist.github.com/0gust1/7683132