Gruntfile.js动态构建文件对象

His*_*Dog 5 gruntjs

问题:在Grunt项目的"配置任务"页面上,"文件对象格式"指定任务的文件工作方式,而"文件阵列格式"和关键的"动态构建文件对象"方式(参见其列表中的"dynamic_mappings"部分没有.

问题:为什么列表#2不起作用?是Grunt页面上的动态文件构建示例错了吗?

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

底部是Gruntfile.js的两个列表.第一个不起作用,而第二个起作用.它们之间的唯一区别在于每个指定"文件"任务的方式:

files: [{
  expand: true,
  cwd: 'views/',
  src: ['**/*.jade'],
  dest: 'html/',
  ext: 'html',
}],
Run Code Online (Sandbox Code Playgroud)

......工作的那个:

files: {
  expand: true,
  cwd: 'views/',
  src: ['**/*.jade'],
  dest: 'html/',
  ext: 'html',
},
Run Code Online (Sandbox Code Playgroud)

唯一不同之处在于存在/不存在的"["和"]".

第二个列表不起作用,但是按照动态配置任务/构建文件对象的 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']);

}
Run Code Online (Sandbox Code Playgroud)

清单#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']);

}
Run Code Online (Sandbox Code Playgroud)
  • "幸运"=周末花了数百页关于解析错误,咕噜声,玉石,快递,JavaScript,功能和对象...最后决定,因为每一次合理的努力都失败了,唯一剩下的就是不合理.

His*_*Dog 4

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

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

 CONFIG
    TASK
       TARGET
          FILES               // MUST be child of a target
          OPTIONS             // MUST be child of a target
Run Code Online (Sandbox Code Playgroud)

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

 grunt.initConfig
    jade: {
       files: {...           // all info content required for jade...
     options: ...            // is specified in these two elements
Run Code Online (Sandbox Code Playgroud)

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

 grunt.initConfig
    jade: {
       files: [ {...
     options: ...
Run Code Online (Sandbox Code Playgroud)

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

 grunt.initConfig
    jade: {
      foo: {      // MUST have target, though no addn'l info added. why? just because.
         files: ...
         options: ...
Run Code Online (Sandbox Code Playgroud)