gpt4 book ai didi

javascript - 使用 grunt 加载 grunt-configs 和 Bower.install()

转载 作者:太空宇宙 更新时间:2023-11-04 02:29:25 25 4
gpt4 key购买 nike

我正在尝试将现有的 Gruntfile 分成更小的文件,以便更轻松地进行概述。然而我遇到了一个颠簸。我正在使用 Bower.install 来安装我的项目依赖项。我的文件夹结构如下所示:

package.json
bower.json
config
--install.js
--default.js //Empty for now
gruntfile.js

我的 Gruntfile 现在看起来像这样:

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

我的 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'

如果我删除所有异步困惑(我最终想保留),那么文件看起来会简单得多:

module.exports = function(grunt) {
var bower = require('bower').commands;
bower.install();
}

我仍然收到以下错误:

>> TypeError: Cannot call method 'hasOwnProperty' of undefined

我对这个主题相当陌生,到目前为止(所有内容都在一个文件中)它工作得很好。现在的大问题是:我如何回到那个点;)

最佳答案

install.js 有几个问题:

1) grunt对象没有公开异步函数,这是第一个错误的原因。 async函数可用于 grunt 任务,因此应该从任务内调用它。
2) 当 using node modules for configurationload-grunt-configs需要一个返回对象的函数,例如:

//config/jshint.js
module.exports = function(grunt, options){
return {
gruntfile : {
src : "Gruntfile.js"
}
}
}

在您的情况下,函数不返回任何内容,并且 load-grunt-configs 失败,并出现 Cannot call method 'hasOwnProperty' of undefined 错误。

以下应该有效:

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();
});
});
}

关于javascript - 使用 grunt 加载 grunt-configs 和 Bower.install(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28138061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com