You can create an additional task in a seperate folder to that of Magento's dev/tools/grunt/tasks by simply using the --tasks
flag that is available on the grunt cli.
Create a file in a location of your choice, I created my file here:
./dev/tools/grunt/custom/tasks/hello.js
The contents of this were as follows:
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
module.exports = function (grunt) {
'use strict';
var fs = require('fs'),
log = grunt.log.write,
ok = grunt.log.ok,
error = grunt.log.error;
grunt.registerTask('hello', function () {
log('hello!');
});
};
To use this tasks, I had to run my grunt command using the --tasks
flag, which looks like:
grunt --tasks=./dev/tools/grunt/custom/tasks hello
This runs my "hello" task.
What I wanted to was actually create a new task that would run some of the other tasks that I've defined, so I created another file along side hello.js
called development.js
. This task will call hello
The contents of development.js
looks like:
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
module.exports = function (grunt) {
'use strict';
var fs = require('fs'),
_ = require('underscore');
grunt.registerTask('development', function () {
var tasks = [
'hello'
];
grunt.task.run(tasks);
});
};
As you can see, you can populate the "tasks" array with various other tasks you would like to run, such as cssmin
etc.
This coupled with the extending the Grunt file as per this answer: https://magento.stackexchange.com/a/121350/28664 means that we can really extend the core Grunt configuration of Magento 2 without touching any core files at all.