andrei
andrei

Reputation: 8582

Grunt task dependencies

I have a grunt task that is installed via npm taskA (not the actual name)

taskA has a dependency: grunt-contrib-stylus and that is specified in taskA's package.json and installed. For some reason when running grunt default from the main Gruntfile.js it gives an error.

Warning: Task "stylus" not found. Use --force to continue.

And the fix is to require grunt-contrib-stylus in the main project. I want to avoid this. What would be the reason that my task is not using the grunt-contrib-stylus in its node_modules/ ?

taskA

module.exports = function(grunt) {
'use strict';

grunt.loadNpmTasks('grunt-contrib-stylus');
...

main Gruntfile.js

...
grunt.loadNpmTasks('taskA');
...

Upvotes: 1

Views: 2411

Answers (2)

Andrew
Andrew

Reputation: 3442

Just found a seemingly simple way to do this using node-isms. Even with kyle-robinson-young's answer, if your task depends on another task via peerDependencies or is in a nested structure you'll still receive the warning.

Here's a way around that!

In your taskA:

module.exports = function(grunt) {
  require('grunt-contrib-stylus/tasks/stylus')(grunt);
  // Other stuff you need to do as part of your task
}

Grunt doesn't appear to care if a task is attached via registerMultiTask or registerTask more than once.

Upvotes: 0

Kyle Robinson Young
Kyle Robinson Young

Reputation: 13762

grunt.loadNpmTasks loads [cwd]/node_modules/[modulename]/tasks/. You can load task as dependenies by changing the cwd:

taskA

module.exports = function(grunt) {
  var parentcwd = process.cwd();
  process.chdir(__dirname);

  grunt.loadNpmTasks('grunt-contrib-stylus');

  process.chdir(parentcwd);
};

Just be sure to set the cwd back to the parent at the end.

Upvotes: 3

Related Questions