1

我有一个通过 npm 安装的 grunt 任务taskA(不是实际名称)

taskA 有一个依赖项:grunt-contrib-stylus,它在 taskA 的 package.json 中指定并安装。由于某种原因,当从主 Gruntfile.js 运行grunt default时,它会给出错误。

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

解决方法是在主项目中要求 grunt-contrib-stylus。我想避免这种情况。我的任务没有在其 node_modules/ 中使用 grunt-contrib-stylus 的原因是什么?

任务A

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

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

主要的 Gruntfile.js

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

2 回答 2

3

grunt.loadNpmTasks负载[cwd]/node_modules/[modulename]/tasks/。您可以通过更改以下内容将任务加载为依赖项cwd

任务A

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

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

  process.chdir(parentcwd);
};

请务必在cwd最后设置回父级。

于 2013-07-11T15:46:28.753 回答
0

刚刚找到了一种看似简单的方法来使用节点主义来做到这一点。即使有kyle-robinson-young的回答,如果您的任务通过 peerDependencies 依赖于另一个任务或者处于嵌套结构中,您仍然会收到警告。

这是一种解决方法!

在你的taskA 中

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

Grunt 似乎并不关心任务是否通过registerMultiTaskregisterTask多次附加。

于 2014-01-23T23:00:03.620 回答