Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grunt: custom task development how-to

I need to implement a custom Grunt task and I'm absolutely lost about the development workflow.

  1. How do I develop a custom task and I simulate loading it using npm during development?
  2. Is there any other way of distributing custom tasks instead of using npm? I mean, can I distribute a JavaScript file defining the whole custom Grunt task and import it in the Gruntfile.js directly?

Since the whole task would be in a very early development stage, maybe the effort of publishing it in npm isn't a good idea.

Thanks in advance.

like image 710
Matías Fidemraizer Avatar asked Jun 12 '13 19:06

Matías Fidemraizer


People also ask

How do I register a task with GruntJS?

Alias Tasks For instance, when you define a taskList with jshint, concat, and uglify tasks and specify the taskName as default, all the listed tasks will be run automatically if Grunt is executed without specifying any tasks. grunt. registerTask('dist', ['concat:dist', 'uglify:dist']);

Can we configure Grunt to run one or more tasks by default by defining a default task?

Tasks are grunt's bread and butter. The stuff you do most often, like jshint or nodeunit . Every time Grunt is run, you specify one or more tasks to run, which tells Grunt what you'd like it to do. If you don't specify a task, but a task named "default" has been defined, that task will run (unsurprisingly) by default.

How do I run a Grunt task in Visual Studio code?

For Gulp, Grunt, and Jake, the task auto-detection works the same. Below is an example of the tasks detected for the vscode-node-debug extension. Tip: You can run your task through Quick Open (Ctrl+P) by typing 'task', Space and the command name. In this case, 'task lint'.


1 Answers

custom grunt tasks are basically node-modules which you can publish to the npm registry. take a look at existing ones, and the documentation 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 it easy for you, you should use grunt-init with the grunt-init-gruntplugin which basically sets everything up for you!

if you dont want to publish your module to npm, you can install it in in your project from a 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 into projects package.json.

if you just want to include a single js file in your project with your task, put that in a directory of your choice (i use grunt-tasks here), and include it in your gruntfile like that:

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

that will try to include every js-file in that directory as grunt tasks.

like image 140
hereandnow78 Avatar answered Oct 25 '22 10:10

hereandnow78