3

Following on from this question: Magento 2 add new theme without changing core files. Grunt

The goal is to register a new Grunt task called "development". To do this, grunt.registerTask is needed, however I can't get access to "grunt" in my Gruntfile.theme.js to then use grunt.registerTask

My files are setup exactly as they are in https://magento.stackexchange.com/a/121350/28664 answer.

If any more information is needed, please let me know.

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.