注册代码位于外部JavaScript文件中的Grunt任务 [英] Registering Grunt tasks whose code is located in external JavaScript files

查看:94
本文介绍了注册代码位于外部JavaScript文件中的Grunt任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个我想用作Grunt任务的函数。我可以通过将它添加到Gruntfile中来实现:

  grunt.registerTask('foo',function(){
// code here
});

然而,将函数代码保存在单独的文件中更有意义。我打算定义一堆这些自定义任务,我不想让Gruntfile膨胀。



我不确定注册这些任务的首选方式是。我找到了这个工作:

$ p $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $ g $% ./path/to/foo.js')(grunt);
});

所以,我有像第一个例子那样的内联函数,但是这一次, m加载外部文件并立即调用它。在这个外部文件中,我当然必须写:

  module.exports = function(grunt){
/ / code here
}

这种方法很有效,但是它感觉很糟糕。有没有更正确的方法来做到这一点?

解决方案

简短回答:替代方案

  grunt.registerTask('foo',function(){
require('./ path / to / foo.js')(grunt);
});

http://gruntjs.com/api/grunt#grunt.loadtasks



长答案:



通常情况下,当您在外部文件中有任务时,将其作为其他nodejs模块。所以,如果这是你将在几个项目中使用的东西,你可能需要在注册表中注册它。后来在你的Gruntfile.js中,你将拥有:

  grunt.loadNpmTasks('yout-module-here'); 

grunt的文档说:

 从指定的Grunt插件载入任务。这个插件必须通过npm在本地安装,并且必须相对于Gruntfile 

但是,如果你不不想将任何内容上传到注册表,您应该使用 loadTasks

  grunt.loadTasks('path / to / your / task / directory'); 

因此,一旦加载任务,您可以在配置中使用它。



这是一个放在外部文件中的简单任务:

 'use strict'; 

module.exports = function(grunt){

grunt.registerMultiTask('nameoftask','description',function(){

var self = this;

//这个数据包含你的配置

});
};

后来在Gruntfile.js中

 grunt.initConfig({
nameoftask:{
task:{
// parameters here
}
}
});


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

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?

解决方案

Short answer: the alternative to this

grunt.registerTask('foo', function () {
    require('./path/to/foo.js')(grunt);
});

is 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:

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

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:

'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

grunt.initConfig({
    nameoftask: {
        task: {
            // parameters here
        }
    }
});

这篇关于注册代码位于外部JavaScript文件中的Grunt任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆