Task configuration is specified in your Gruntfile
via the grunt.initConfig
method. This configuration will mostly be under task-named properties, but may contain any arbitrary data. As long as properties don't conflict with properties your tasks require, they will be otherwise ignored.
Also, because this is JavaScript, you're not limited to JSON; you may use any valid JavaScript here. You may even programmatically generate the configuration if necessary.
grunt.initConfig({
concat: {
// concat task configuration goes here.
},
uglify: {
// uglify task configuration goes here.
},
// Arbitrary non-task-specific properties.
my_property: 'whatever',
my_src_files: ['foo/*.js', 'bar/*.js'],
});
对grunt的配置一般是写在grunt.initConfig()函数里的。由于就是普通的javascript,所以甚至可以用编程的方式自定义配置。也可以定义自定义变量,上面的例子就包含了2个自定义变量
When a task is run, Grunt looks for its configuration under a property of the same name. Multi-tasks can have multiple configurations, defined using arbitrarily named "targets." In the example below, theconcat
task has foo
and bar
targets, while the uglify
task only has a bar
target.
grunt.initConfig({
concat: {
foo: {
// concat task "foo" target options and files go here.
},
bar: {
// concat task "bar" target options and files go here.
},
},
uglify: {
bar: {
// uglify task "bar" target options and files go here.
},
},
});
Specifying both a task and target like grunt concat:foo
or grunt concat:bar
will process just the specified target's configuration, while running grunt concat
will iterate over all targets, processing each in turn. Note that if a task has been renamed with grunt.renameTask, Grunt will look for a property with the new task name in the config object.
在运行一个Task时,Grunt会搜索同名的配置项,所以配置项的名字必须和task匹配,Target则可以自定义。Grunt还提供了renameTask()函数,可以重命名task,可能是为了解决不同的plugin,task命名重复的问题
Inside a task configuration, an options
property may be specified to override built-in defaults. In addition, each target may have an options
property which is specific to that target. Target-level options will override task-level options.
The options
object is optional and may be omitted if not needed.
grunt.initConfig({
concat: {
options: {
// Task-level options may go here, overriding task defaults.
},
foo: {
options: {
// "foo" target options may go here, overriding task-level options.
},
},
bar: {
// No options specified; this target will use task-level options.
},
},
});
插件定义的Task一般都有默认配置,可以使用options参数,覆盖默认的配置。如果不需要的话,则options不是必须的。针对不同的Target,也可以配置不同的options
由于Grunt大部分的Task都是对文件进行操作,因此Grunt框架对文件的支持特别强大,这部分内容太多了,就不贴了,详见:Configuring tasks
只看这个例子,可以大致看出grunt对文件操作的灵活性:
Or create your own filter
function and return true
or false
whether the file should be matched. For example the following will only clean folders that are empty:
grunt.initConfig({
clean: {
foo: {
src: ['tmp/**/*'],
filter: function(filepath) {
return (grunt.file.isDir(filepath) && require('fs').readdirSync(filepath).length === 0);
},
},
},
});
Templates specified using <% %>
delimiters will be automatically expanded when tasks read them from the config. Templates are expanded recursively until no more remain.
The entire config object is the context in which properties are resolved. Additionally, grunt
and its methods are available inside templates, eg. <%= grunt.template.today('yyyy-mm-dd') %>
.
<%= prop.subprop %>
Expand to the value of prop.subprop
in the config, regardless of type. Templates like this can be used to reference not only string values, but also arrays or other objects.<% %>
Execute arbitrary inline JavaScript code. This is useful with control flow or looping. Given the sample concat
task configuration below, running grunt concat:sample
will generate a file named build/abcde.js
by concatenating the banner /* abcde */
with all files matching foo/*.js
+bar/*.js
+ baz/*.js
.
grunt.initConfig({
concat: {
sample: {
options: {
banner: '/* <%= baz %> */\n', // '/* abcde */\n'
},
src: ['<%= qux %>', 'baz/*.js'], // [['foo/*.js', 'bar/*.js'], 'baz/*.js']
dest: 'build/<%= baz %>.js', // 'build/abcde.js'
},
},
// Arbitrary properties used in task configuration templates.
foo: 'c',
bar: 'b<%= foo %>d', // 'bcd'
baz: 'a<%= bar %>e', // 'abcde'
qux: ['foo/*.js', 'bar/*.js'],
});
类似jsp里的java脚本
In the following Gruntfile, project metadata is imported into the Grunt config from a package.json
file, and the grunt-contrib-uglify plugin uglify
task is configured to minify a source file and generate a banner comment dynamically using that metadata.
Grunt has grunt.file.readJSON
and grunt.file.readYAML
methods for importing JSON and YAML data.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
dist: {
src: 'src/<%= pkg.name %>.js',
dest: 'dist/<%= pkg.name %>.min.js'
}
}
});
pkg不是某个Task的配置,是一个自定义变量,可以通过grunt.file.readJSON()函数,导入外部的json对象