Gruntfile 中获取针对当前项目的配置数据。

注意,任何标记为 ☃ (unicode snowman) 的方法也是可以直接通过 grunt 对象访问的;任何标记为 ☆ (white star) 的方法都可以在task内部通过 this 对象访问的。请知晓。


注意,下面列出的方法也可以通过 grunt 对象访问,访问形式为 grunt.initConfig

grunt.config.init ☃

为当前项目初始化一个配置对象。其中传入的 configObject 参数可以用在后续的task中,可以通过 grunt.config 方法访问。几乎每个项目的 Gruntfile 都会调用此方法。


注意,任何 <% %> 模板字符串只会在取到配置数据后才被处理。

下面的案例展示了针对 grunt-contrib-jshint插件 中的 jshint task的配置数据:

  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 '' 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.


从项目的 Grunt 配置中获取或者设置一个值。这个方法作为其他方法的别名;如果传递两个参数,grunt.config.set被调用,另一方面grunt.config.get也被调用。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, or null 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.



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 by grunt.config.get but not by grunt.config.getRaw.


If any retrieved value is entirely a single '<%= foo %>' or '<%= %>' template string, and the specified foo or 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. If prop 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 配置中的某个属性设置一个值。

grunt.config.set(prop, value)

注意,任何 <% %> 模板字符串只会在取到配置数据后才被处理。


忽略给定的propString中的.点号。这应该用于包含点号的属性名。Escape . dots in the given propString. This should be used for property names that contain dots.



Added in 0.4.5

Recursively merges properties of the specified configObject into the current project configuration.


You can use this method to append configuration options, targets, etc., to already defined tasks, for example:

  watch: {
    files: ["path/to/files"],
    tasks: ["task"]

Requiring Config Data

注意,下面列出的方法都可以在task内部通过 this 对象访问,访问形式为 this.requiresConfig

grunt.config.requires ☆


grunt.config.requires(prop [, prop [, ...]])

此方法在task内部以 this.requiresConfig 形式调用。