我一直在尝试使用grunt-check-dependencies来检查NPM模块依赖关系,然后再运行Grunt任务。问题是,当其中一个缺少的NPM依赖项是Grunt任务时,这没有帮助,例如,咕噜-的contrib-CONCAT。
由于Grunt显然在“检查依赖关系”之前注册了任务。任务运行,即使' checkDependencies'将成功安装任何缺少的Grunt任务模块,它会做到这一点"太晚了#34; - Grunt已经确定丢失了缺少的模块,因此Grunt构建运行失败。
一种可行的方法是始终使用例如命令行。 " npm install;咕噜......",但我想我会看到是否有人有一个完全' Grunt-internal'溶液
以下是演示此问题的Grunt输出示例。
% rm -rf node_modules/grunt-contrib-concat ; grunt
>> Local Npm module "grunt-contrib-concat" not found. Is it installed?
Running "checkDependencies:this" (checkDependencies) task
>> grunt-contrib-concat: not installed!
Invoking npm install...
npm http GET https://registry.npmjs.org/grunt-contrib-concat
npm http 304 https://registry.npmjs.org/grunt-contrib-concat
grunt-contrib-concat@0.3.0 node_modules/grunt-contrib-concat
Warning: Task "concat:dev" not found. Use --force to continue.
Aborted due to warnings.
以下是导致上述输出/错误的示例gruntfile.js:
module.exports = function(grunt) {
'use strict';
var config = {
concat: {
dev: {
src: ['foo1.js', 'foo2.js'],
dest: 'foo_concat.js'
}
},
checkDependencies: {
this: {
options: {
npmInstall: true
},
},
}
};
// trying to make sure all npm nodules listed in package.json are
// installed before other grunt tasks
grunt.loadNpmTasks('grunt-check-dependencies');
grunt.task.run('checkDependencies');
// load grunt tasks
require('load-grunt-tasks')(grunt);
// alternate approach to loading tasks; exhibits same problem
// grunt.loadNpmTasks('grunt-contrib-concat');
grunt.initConfig(config);
grunt.registerTask('default', ['concat:dev']);
}