Javascript 使用grunt加载grunt配置和bower.install()
我正在尝试将现有的Gruntfile划分为更小的文件,以便更容易地进行概述。然而,我遇到了一个肿块。我正在使用bower.install安装我的项目依赖项。 我的文件夹结构如下所示:Javascript 使用grunt加载grunt配置和bower.install(),javascript,node.js,gruntjs,bower,Javascript,Node.js,Gruntjs,Bower,我正在尝试将现有的Gruntfile划分为更小的文件,以便更容易地进行概述。然而,我遇到了一个肿块。我正在使用bower.install安装我的项目依赖项。 我的文件夹结构如下所示: package.json bower.json config --install.js --default.js //Empty for now gruntfile.js 'use strict'; module.exports = function(grunt) { var npmDependencies
package.json
bower.json
config
--install.js
--default.js //Empty for now
gruntfile.js
'use strict';
module.exports = function(grunt) {
var npmDependencies = require('./package.json').devDependencies;
require('load-grunt-tasks')(grunt);
var configs = {
config : {
paths : {
"build" : ["grunts/*.js*", "package.json", "bower.json", "Gruntfile.js"],
}
}
};
var loadConfigs = require( 'load-grunt-configs' );
configs = loadConfigs( grunt, configs );
// Project configuration.
grunt.initConfig( configs );
}
我的Grunfile现在看起来像这样:
package.json
bower.json
config
--install.js
--default.js //Empty for now
gruntfile.js
'use strict';
module.exports = function(grunt) {
var npmDependencies = require('./package.json').devDependencies;
require('load-grunt-tasks')(grunt);
var configs = {
config : {
paths : {
"build" : ["grunts/*.js*", "package.json", "bower.json", "Gruntfile.js"],
}
}
};
var loadConfigs = require( 'load-grunt-configs' );
configs = loadConfigs( grunt, configs );
// Project configuration.
grunt.initConfig( configs );
}
My install.js看起来是这样的:
module.exports = function(grunt) {
var done = grunt.async();
var bower = require('bower').commands;
bower.install().on('end', function(data) {
done();
}).on('data', function(data) {
console.log(data);
}).on('error', function(err) {
console.error(err);
done();
});
}
但是,这不起作用,因为我收到了错误:
>> TypeError: Object #<Object> has no method 'async'
我仍然收到以下错误:
>> TypeError: Cannot call method 'hasOwnProperty' of undefined
我对这个主题是相当陌生的,到目前为止(将所有内容都放在一个文件中),它工作得很好。现在的大问题是:我如何回到这一点;) install.js有几个问题: 1) 对象不公开异步函数,这是第一个错误的原因。该函数可用于grunt任务,因此应在任务内调用该函数。
2) load grunt configs需要一个返回对象的函数,例如:
//config/jshint.js
module.exports = function(grunt, options){
return {
gruntfile : {
src : "Gruntfile.js"
}
}
}
在您的情况下,函数不返回任何内容,load grunt configs失败,出现无法调用undefined的方法'hasOwnProperty'的错误。
以下方法应该有效:
module.exports = function(grunt) {
return grunt.registerTask("bower", function() {
var done = this.async();
var bower = require('bower').commands;
bower.install().on('end', function(data) {
done();
}).on('data', function(data) {
console.log(data);
}).on('error', function(err) {
console.error(err);
done();
});
});
}
嘿,很抱歉反馈太晚了。你是对的,有几件事是错的。但是:就我所理解的您链接的文档而言,您在这里显示的可能性只是一种变体。我指的是返回函数部分上方的代码示例,即返回对象部分的示例js文件。无论如何,这对我不起作用。@stiller_leser我在发布之前测试了这个样本。您可以指定哪些不起作用吗?您的代码示例对我来说非常好(除了一个问题,即他找不到bower.json文件-该文件现已修复)。请参阅此处,了解我所指的代码示例:-但我现在已切换到加载grunt配置,这对我来说很好。很有可能我的配置中有一个小错误。谢谢你的帮助!