從 Gruntfile
中獲取針對(duì)當(dāng)前項(xiàng)目的配置數(shù)據(jù)。
注意,任何標(biāo)記為 ? (unicode snowman) 的方法也是可以直接通過 grunt
對(duì)象訪問的;任何標(biāo)記為 ☆ (white star) 的方法都可以在task內(nèi)部通過 this
對(duì)象訪問的。請(qǐng)知曉。
注意,下面列出的方法也可以通過 grunt
對(duì)象訪問,訪問形式為 grunt.initConfig
。
為當(dāng)前項(xiàng)目初始化一個(gè)配置對(duì)象。其中傳入的 configObject
參數(shù)可以用在后續(xù)的task中,可以通過grunt.config
方法訪問。幾乎每個(gè)項(xiàng)目的 Gruntfile
都會(huì)調(diào)用此方法。
grunt.config.init(configObject)
注意,任何 <% %>
模板字符串只會(huì)在取到配置數(shù)據(jù)后才被處理。
下面的案例展示了針對(duì) grunt-contrib-jshint插件 中的 jshint
task的配置數(shù)據(jù):
grunt.config.init({
jshint: {
all: ['lib/*.js', 'test/*.js', 'Gruntfile.js']
}
});
查看 Getting started 指南可以獲取更多的配置案例。
此方法還可以以 grunt.initConfig
的形式訪問。
The following methods allow Grunt configuration data to be accessed either via dot-delimited string like'pkg.author.name'
or via array of property name parts like ['pkg', 'author', 'name']
.
Note that if a specified property name contains a .
dot, it must be escaped with a literal backslash, eg.'concat.dist/built\\.js'
. If an array of parts is specified, Grunt will handle the escaping internally with the grunt.config.escape
method.
從項(xiàng)目的 Grunt 配置中獲取或者設(shè)置一個(gè)值。這個(gè)方法作為其他方法的別名;如果傳遞兩個(gè)參數(shù),grunt.config.set
被調(diào)用,另一方面grunt.config.get
也被調(diào)用。Get or set a value from the project's grunt configuration. This method serves as an alias to other methods; if two arguments are passed, grunt.config.set
is called, otherwise grunt.config.get
is called.
grunt.config([prop [, value]])
Get a value from the project's Grunt configuration. If prop
is specified, that property's value is returned, ornull
if that property is not defined. If prop
isn't specified, a copy of the entire config object is returned. Templates strings will be recursively processed using the grunt.config.process
method.
grunt.config.get([prop])
Process a value, recursively expanding <% %>
templates (via the grunt.template.process
method) in the context of the Grunt config, as they are encountered. this method is called automatically bygrunt.config.get
but not by grunt.config.getRaw
.
grunt.config.process(value)
If any retrieved value is entirely a single '<%= foo %>'
or '<%= foo.bar %>'
template string, and the specified foo
or foo.bar
property is a non-string (and not null
or undefined
) value, it will be expanded to the actual value. That, combined with grunt's task system automatically flattening arrays, can be extremely useful.
Get a raw value from the project's Grunt configuration, without processing <% %>
template strings. Ifprop
is specified, that property's value is returned, or null
if that property is not defined. If prop
isn't specified, a copy of the entire config object is returned.
grunt.config.getRaw([prop])
給當(dāng)前項(xiàng)目的 Grunt 配置中的某個(gè)屬性設(shè)置一個(gè)值。
grunt.config.set(prop, value)
注意,任何 <% %>
模板字符串只會(huì)在取到配置數(shù)據(jù)后才被處理。
忽略給定的propString
中的.
點(diǎn)號(hào)。這應(yīng)該用于包含點(diǎn)號(hào)的屬性名。Escape .
dots in the givenpropString
. This should be used for property names that contain dots.
grunt.config.escape(propString)
Added in 0.4.5
Recursively merges properties of the specified configObject
into the current project configuration.
grunt.config.merge(configObject)
You can use this method to append configuration options, targets, etc., to already defined tasks, for example:
grunt.config.merge({
watch: {
files: ["path/to/files"],
tasks: ["task"]
}
});
注意,下面列出的方法都可以在task內(nèi)部通過 this
對(duì)象訪問,訪問形式為 this.requiresConfig
。
如果需要的配置屬性有一個(gè)或多個(gè)不存在、值為null
或 undefined
,當(dāng)前task將失敗。此方法可以指定一個(gè)或多個(gè)字符串、配置屬性數(shù)組作為參數(shù)。
grunt.config.requires(prop [, prop [, ...]])
此方法在task內(nèi)部以 this.requiresConfig
形式調(diào)用。
更多建議: