【问题标题】:Grunt not loading external tasksGrunt 不加载外部任务
【发布时间】:2014-09-15 22:29:54
【问题描述】:

我碰壁了,所以我希望有人能帮我解决这个问题...我正在整理一个新的 Grunt 堆栈,我可以在我的所有项目中使用它。但是,无论出于何种原因,它目前都没有加载任务。

这是我的 Gruntfile:

'use strict';

module.exports = function (grunt) {

  var config = {
    pkg: require('package.json')
  };

  // Load all grunt configuration options
  grunt.util._.extend(config, loadConfig('./grunt/modules/'));

  // Configure grunt
  grunt.initConfig(config);

  // Load and register all tasks from devDependencies
  require('jit-grunt')(grunt);

  // Load all custom tasks
  grunt.loadTasks('grunt');

}

/**
 * Load configuration files for Grunt
 * @param   {string}  path  Path to folder with tasks
 * @return  {object}        All options
 *
 * http://thenittygritty.co/shared-grunt-configuration
 */
var loadConfig = function (path) {
  var glob = require('glob');
  var object = {};
  var key;

  glob.sync('*', { cwd: path }).forEach(function (option) {
    key = option.replace(/\.js$/,'');
    object[key] = require(path + option);
  });

  return object;
};

所以,我有一个“grunt”文件夹,其中包含我所有的自定义、别名和第 3 方任务。自定义和别名任务位于主文件夹中,而安装的第 3 方任务(cssmin.js、watch.js 等)位于“grunt/modules”文件夹中。

这是一个自定义的示例和第 3 方的示例...

grunt/modules/connect.js:

'use strict';

// https://github.com/gruntjs/grunt-contrib-connect
module.exports = {

  dev: {
    options: {
      port: 6000,
      base: 'dist',
      livereload: true
    }
  }

}

grunt/serve.js:

'use strict';

module.exports = function (grunt) {

  grunt.registerTask(
    'serve',
    [
      'build:dev',
      'connect',
      'watch'
    ]
  );

}

但是,当我运行“grunt serve”(或其他任何东西)时,我收到一个未找到任务的错误:

Loading "Gruntfile.js" tasks...ERROR
>> TypeError: undefined is not a function
Warning: Task "serve" not found. Use --force to continue.

Aborted due to warnings.

有什么想法吗?

【问题讨论】:

    标签: javascript gruntjs workflow frontend


    【解决方案1】:

    这个问题实际上与我的 Gruntfile 没有任何关系...我的一项任务中存在一个问题,导致 Grunt 失败。一旦我解决了这个问题,一切都会重新开始。

    【讨论】:

    • 很高兴你能弄明白。随意将其标记为结束问题的答案。
    • @BrandonBoone 只需要再等 9 个小时,我才能接受答案......虽然我觉得自己不回答很脏,哈哈。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多