Grunt任务陷入无限循环

6

正在为即将开始的项目准备一个基本的Gruntfile.js。我在一台新电脑上开始了工作,所以一切都是全新的。使用Homebrew安装了Node和NPM,然后在全局和本地目录中安装了Grunt。

这是我的package.json文件:

{
  "name": "timespent-prototype",
  "version": "0.1.0",
  "devDependencies": {
    "assemble": "0.4.37",
    "bower": "^1.4.1",
    "grunt": "^0.4.5",
    "grunt-contrib-concat": "^0.5.1",
    "grunt-contrib-sass": "^0.9.2",
    "grunt-contrib-watch": "^0.6.1",
    "grunt-newer": "^1.1.0",
    "grunt-wiredep": "^2.0.0"
  }
}

这里是我的Gruntfile.js文件:

module.exports = function(grunt) {

  // Project configuration.
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),

    concat: {
      vendor: {
       src: ['vendor/**/*.min.js'],
       dest: 'build/javascripts/library.js',
      }
    },

    // Takes your scss files and compiles them to css
    sass: {
      dist: {
        options: {
          style: 'expanded'
        },
        files: {
          'build/stylesheets/application.css': 'app/stylesheets/application/index.scss'
        }
      }
    },

    // Assembles your templates into HTML files
    assemble: {
      options: {
        layoutdir: 'app/views/layouts/',
        partials: ['app/views/partials/*.hbs'],
        flatten: true,
        expand: true
      },
      pages: {
        src: ['app/views/**/*.hbs','!app/views/layouts/*.hbs','!app/views/partials/*.hbs'],
        dest: 'build/'
      }
    },

    wiredep: {
      task: {
        src: ['app/views/layouts/*.hbs']
      }
    },

    // Watches for file changes, runs the default task
    watch: {
      files: ['app/**/*'],
      tasks: ['default'],
      options: {
        // Start another live reload server on port 1337
        livereload: 1337,
      }
    }

  });

  // Load the plugins
  grunt.loadNpmTasks('assemble');
  grunt.loadNpmTasks('grunt-contrib-sass');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-newer');
  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-wiredep');

  // Register the tasks
  grunt.registerTask('default', ['sass','assemble']);
  grunt.registerTask('watch', ['watch']);
  grunt.registerTask('concat', ['concat']);
  grunt.registerTask('wiredep', ['wiredep']);
};

我看到的问题是,当有多个任务时,特别是使用wiredepconcat时,任务会陷入循环并永远不会结束。运行grunt concat时,可以像这样输出详细信息:
Registering "grunt-contrib-concat" local Npm module tasks.
Reading /Users/jacksonlynch/projects/timespent-prototype/node_modules/grunt-contrib-concat/package.json...OK
Parsing /Users/jacksonlynch/projects/timespent-prototype/node_modules/grunt-contrib-concat/package.json...OK
Loading "concat.js" tasks...OK
+ concat

Running tasks: concat

Running "concat" task

Running "concat" task

Running "concat" task

Running "concat" task

Running "concat" task

Running "concat" task

当我运行“concat”任务时,会一直打印输出,除非我停止它。由于我在多个插件和任务中都遇到了这个问题,这可能是我的NPM或Grunt安装出了问题,但我很难进行调试。如果有人以前遇到过这个问题,请让我知道您是如何解决的!

谢谢!


编辑:回应Alireza Ahmadi的评论,这是我的文件结构:

.
|
|_ app/
  |_ assets/
  |_ javascript/
  |_ stylesheets/
  |_ views/
|
|_ build/
  |_stylesheets/
  |_javascripts/
|
|_ vendor/
|_ bower.json
|_ Gruntfile.js
|_ package.json

为什么一些任务的目标以 build 开头,而 concat 任务的目标以 dist 开头?您能分享至少两层深度的文件夹结构吗? - Alireza Ahmadi
1
谢谢回复!那只是一个糟糕的复制粘贴,目标应该始终是“build”,我已经纠正了并添加了所请求的文件结构。 - jacksonthatsme
1个回答

19
在你的 Gruntfile.js 的最后 2 行中,你重新声明了 concatwiredep 任务,当 grunt 尝试运行你的代码时,它会陷入无限循环,因为 concat 引用了未定义的 concat 任务,所以你应该删除这些行:
grunt.registerTask('concat', ['concat']);
grunt.registerTask('wiredep', ['wiredep']);

通常情况下,当你使用 grunt.initConfig 定义一个名为 foobar 的任务时,它已经被定义了,不需要使用 registerTask 进行注册,并且可以通过 grunt foobar 命令进行访问。


1
这就是答案。谢谢Alireza!我没有意识到initConfig也会为其注册一个任务。非常感谢! - jacksonthatsme
1
对我而言,问题在于我的任务名称与注册配置名称相同。 - joncodo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接