簡體   English   中英

使用grunt加載grunt-configs和bower.install()

[英]load-grunt-configs and bower.install() using grunt

我正在嘗試將現有的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'

如果刪除所有的async-clutter(最終希望保留),那么文件看起來會簡單很多:

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

我仍然收到以下錯誤:

>> 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失敗, Cannot call method 'hasOwnProperty' of undefined錯誤的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();
      });
  });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM