Grunt: custom task development

I need to implement a custom Grunt task, and I absolutely lost it in the development workflow.

  • How do I create a custom task and I simulate loading it using npm during development?
  • Is there any other way to distribute custom tasks instead of using npm ? I mean, can I distribute a JavaScript file defining the entire Grunt user task and import it directly into Gruntfile.js ?

Since the whole task will be at a very early stage of development, perhaps trying to publish it to npm not a good idea.

Thanks in advance.

+7
source share
1 answer

custom grunt tasks are basically node modules that you can publish to the npm registry. Take a look at the existing ones and the documentation on how to build them here:

http://gruntjs.com/api/grunt.task

basically you just do something like this:

 module.exports = function (grunt) { // or use grunt.registerMultiTask grunt.registerTask('your-taskname', 'your task description', function () { }); }; 

To make your work easier, you should use grunt-init with grunt-init-gruntplugin , which basically installs everything for you!

If you do not want to publish your module before npm, you can install it in your project from the git repository (for example, using github):

 $ npm install git+https://github.com/your-user/your-repository --save 

the -save option saves it automatically as a dependency in package.json projects.

if you just want to include one js file in your project, put it in the directory of your choice (here I use grunt-tasks) and include it in your grunt file:

 grunt.loadTasks("./grunt-tasks"); 

which will try to include every js file in this directory as grunt jobs.

+21
source

All Articles