5

问题:在 Grunt 项目的“配置任务”页面上,为任务指定文件的“文件对象格式”方式有效,而“文件数组格式”以及关键的是“动态构建文件对象”方式(参见他们列表的“dynamic_mappings”部分)没有。

问题:为什么清单 #2 不起作用?Grunt 页面上的动态文件构建示例是否错误?

参考:配置任务/动态构建文件对象中的 Grunt 项目页面。

底部是 Gruntfile.js 的两个清单。第一个不起作用,而第二个起作用。它们之间的唯一区别在于它们各自指定“文件”任务的方式:

files: [{
  expand: true,
  cwd: 'views/',
  src: ['**/*.jade'],
  dest: 'html/',
  ext: 'html',
}],

...有效的内容如下:

files: {
  expand: true,
  cwd: 'views/',
  src: ['**/*.jade'],
  dest: 'html/',
  ext: 'html',
},

唯一的区别在于“[”和“]”的存在/不存在。

第二个清单不起作用,但确实遵循 Grunt 项目页面上的示例配置任务/动态构建文件对象

清单#1(不起作用):中止并显示“警告:对象#没有方法'indexOf'使用--force继续。”

module.exports = function(grunt) {

    grunt.initConfig({

        jade: {
            options: { pretty: true,
                data: {
                  debug: true,
                  timestamp: "<%= grunt.template.today() %>"
                }
            },
            files: [{
              expand: true,
              cwd: 'views/',
              src: ['**/*.jade'],
              dest: 'html/',
              ext: 'html',
            }],
        },

        watch: {
            html: {
                files: ['handlers/**/*.js', 'views/**/*.jade', 'app.js'],
                tasks: ['jade']
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-jade');
    grunt.loadNpmTasks('grunt-contrib-watch');

    grunt.registerTask('default', ['jade', 'watch']);

}

清单#2(作品)

module.exports = function(grunt) {

    grunt.initConfig({

        jade: {
            options: { pretty: true,
                data: {
                  debug: true,
                  timestamp: "<%= grunt.template.today() %>"
                }
            },
            files: {
              expand: true,
              cwd: 'views/',
              src: ['**/*.jade'],
              dest: 'html/',
              ext: 'html',
            },
        },

        watch: {
            html: {
                files: ['handlers/**/*.js', 'views/**/*.jade', 'app.js'],
                tasks: ['jade']
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-jade');
    grunt.loadNpmTasks('grunt-contrib-watch');

    grunt.registerTask('default', ['jade', 'watch']);

}
  • "lucky" = 周末阅读了数百页关于解析错误、grunt、jade、express、JavaScript、函数和对象的内容……最终决定,由于所有合理的努力都失败了,唯一可以尝试的就是不合理的。
4

2 回答 2

4

答案(在我发布问题后添加):

CONFIG (grunt.initConfig) 所需的结构是:

 CONFIG
    TASK
       TARGET
          FILES               // MUST be child of a target
          OPTIONS             // MUST be child of a target

所以这不应该工作(但确实..我很幸运*):

 grunt.initConfig
    jade: {
       files: {...           // all info content required for jade...
     options: ...            // is specified in these two elements

......这不应该工作(并且不......哇!):

 grunt.initConfig
    jade: {
       files: [ {...
     options: ...

最后,这应该并且确实有效(哈利路亚):

 grunt.initConfig
    jade: {
      foo: {      // MUST have target, though no addn'l info added. why? just because.
         files: ...
         options: ...
于 2013-09-17T06:17:11.497 回答
1

我遇到了类似的问题,并在此帖子中找到了答案(在我重新安排了此处发布的结构之后)

引用导致警告的 Grunt 目标:Object true has no method 'indexOf'

本质上,答案是将 files 指令的定义部分包装为数组。代替

files:{...}  

利用

files:[{...}]
于 2013-12-04T22:18:45.097 回答