Grunt - 配置任务

您可以在 Gruntfile.js文件中为Grunt定义项目特定的配置数据.

Grunt配置

可以使用 grunt.initConfig()方法在Gruntfile中初始化任务配置数据.在 grunt.initConfig()函数内部,从package.json文件中获取配置信息.配置将包含名为 properties 的任务和任意数据.

grunt.initConfig({
   jshint: {
      // configuration for jshint task
   },
   cssmin: {
      // configuration for cssmin task
   },
   // Arbitrary non-task-specific properties
   my_files: ['dir1/*.js', 'dir2/*.js'],
});

任务配置和目标

当您运行任务时,Grunt在任务命名属性下查找配置.我们将使用多个配置和目标选项定义任务,如下所示 :

grunt.initConfig({
   jshint: {
      myfile1: {
         // configuration for "myfile1" target options
      },
      myfile2: {
         // configuration for "myfile2" target options
      },
   },
   cssmin: {
      myfile3: {
         // configuration for "myfile3" target options
      },
   },
});

这里, jshint 任务有 myfile1 myfile2 目标和 cssmin 任务有 myfile3 目标.当您运行 grunt jshint 时,它将遍历任务和目标以处理指定目标的配置.

选项

在任务配置中定义 options 属性,该属性将覆盖任务默认值.每个目标都包含 options 属性,该属性会覆盖任务级选项.它将具有以下格式 :

grunt.initConfig({
   jshint: {
      options: {
         // task-level options that overrides task defaults
      },
      myfile: {
         options: {
            // "myfile" target options overrides task defaults
         },
      },

      myfile1: {
         // there is no option, target will use task-level options
      },
   },
});

文件

Grunt提供了一些想法,用于指定任务应该在哪些文件上运行,并使用不同的方式来指定 src-dest 文件映射.以下是 src dest 映射和减号支持的一些附加属性;

  • 过滤器 : 它是一个指定匹配的 src 文件路径并返回true或false值的函数.

  • nonull  : 去;当它设置为true时,它定义非匹配模式.

  • dot : 它匹配以句号或其他方式开头的文件名.

  • matchBase : 它匹配包含斜杠和路径基本名称的模式.

  • expand : 它处理src-dest文件映射.

压缩格式

它指定了src-每个目标的dest文件映射,可用于只读任务,只需要 src 属性,不需要 dest 属性.

grunt.initConfig({
   jshint: {
      myfile1: {
         src: ['src/file1.js','src/file2.js']
      },
   },
   cssmin: {
      myfile2: {
         src: ['src/file3.js','src/file4.js'],
         dest: 'dest/destfile.js',
      },
   },
});

文件对象格式

它指定每个目标的src-dest文件映射,其中属性名称为 dest 文件及其值为 src 文件.

grunt.initConfig({
   jshint: {
      myfile1: {
         files: {
            'dest/destfile.js':['src/file1.js','src/file2.js'],
            'dest/destfile1.js':['src/file3.js','src/file4.js'],
         },
      },
      myfile2: {
         files: {
            'dest/destfile2.js':['src/file22.js','src/file23.js'],
            'dest/destfile21.js':['src/file24.js','src/file25.js'],
         },
      },
   },
});

文件数组格式

它通过使用每个映射的附加属性指定每个目标的src-dest文件映射.

grunt.initConfig({
   jshint: {
      myfile1: {
         files: [
            {src:['src/file1.js','src/file2.js'],dest:'dest/file3.js'},
            {src:['src/file4.js','src/file4.js'],dest:'dest/file5.js'},
         ],
      },
      myfile2: {
         files: [
            {src:['src/file6.js','src/file7.js'],dest:'dest/file8/', nonull:true},
            {src:['src/file9.js','src/file10.js'],dest:'dest/file11/', filter:'isFalse'},
         ],
      },
   },
});

旧格式

dest-as-target 文件格式之前是存在多任务,其中目标文件路径是目标的名称.不推荐使用以下格式,并且不在代码中使用.

grunt.initConfig({
   jshint: {
      'dest/destfile2.js':['src/file3.js','src/file4.js'],
      'dest/destfile5.js':['src/file6.js','src/file7.js'],
   },
});

自定义过滤器功能

您可以使用帮助详细了解目标文件过滤器属性.以下格式仅在文件与实际文件匹配时清除文件.

grunt.initConfig({
   clean: {
      myfile:{
         src: ['temp/**/*'],
         filter: 'isFile',
      },
   },
});

Globbing Patterns

Globbing意味着扩展文件名. Grunt通过使用内置node-glob minimatch库来支持globbing. globbing模式包括以下几点 :

  • * 匹配任意数量的字符,但不是/.

  • 匹配单个字符,但不匹配/.

  • ** 匹配多个字符,包括/.

  • {} 指定逗号分隔"或"表达式列表.

  • 将在开头否定模式匹配.

例如 :

{src: 'myfile/file1.js', dest: ...} // it specifies the single file

{src: 'myfile/*.js', dest: ...} //it matches all the files ending wth .js

{src: 'myfile/{file1,file2}*.js', dest: ...} //defines the single node glob pattern

{src: ['myfile/*.js', '!myfile/file1.js'], dest: ...} // all files will display in alpha

// order except for file1.js

构建文件对象动态cally

使用单个文件时,可以使用其他属性动态构建文件列表.当您将展开属性设置为true时,它将启用以下某些属性 :

  • cwd 将所有 src 与此路径匹配.

  • src 匹配相对于 cwd 匹配的模式.

  • dest 属性指定目标路径前缀.

  • ext 将用 dest 路径中生成的值替换现有的分机.

  • extDot 表示指示分机的时间段所在的位置.它使用第一个期或最后期;默认情况下,它设置为第一个期间

  • 展平删除

  • 重命名指定包含新目的地和文件名的字符串.

重命名属性

这是一个唯一的JavaScript函数,它返回一个字符串,你不能使用字符串值改名.在以下示例中, copy 任务将创建README.md的备份.

grunt.initConfig({
   copy: {
      backup: {
         files: [{
            expand: true,
            src: ['docs/README.md'],    // creating a backup of README.md
            rename: function () {       // specifies the rename function
               return 'docs/BACKUP.txt'; // returns a string with the complete destination
            }
         }]
      }
   }
});

模板

您可以使用 分隔符指定模板.当从配置中读取它们时,它们将自动扩展.它包括两种类型的属性 :

  • 属性用于在配置中扩展 prop.subprop 的值,该值可以引用字符串值,数组和其他对象.

  • 属性执行用于控制流或循环的内联JavaScript代码.

例如 :

grunt.initConfig({
   concat: {
      myfile: {
         options: {
            banner: '/*  */\n',
         },
         src: ['', 'file3/*.js'],
         dest: 'build/.js',
      },
   },
   // properties used in task configuration templates
   file1: 'c',
   file2: 'bd',
   file3: 'ae',
   myval: ['file1/*.js', 'file2/*.js'],
});

导入外部数据

您可以从 package.json文件导入外部数据. grunt-contrib- uglify 插件可用于缩小源文件,并使用元数据创建横幅注释.您可以使用 grunt.file.readJSON grunt.file.readYAML 导入JSON和YAML数据.

For Example :

grunt.initConfig({
   pkg: grunt.file.readJSON('package.json'),
   uglify: {
      options: {
         banner: '/*!   */\n'
      },
      dist: {
         src: 'src/.js',
         dest: 'dist/.min.js'
      }
   }
});