【问题标题】:Grunt task dependenciesGrunt 任务依赖
【发布时间】:2013-07-09 10:26:24
【问题描述】:

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

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');
...

【问题讨论】:

    标签: javascript npm gruntjs


    【解决方案1】:

    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 设置回父级。

    【讨论】:

      【解决方案2】:

      刚刚找到了一种使用节点主义的看似简单的方法。即使有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 多次附加任务。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-19
        • 1970-01-01
        • 2013-09-09
        • 2012-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多