Grunt 入门教程 grunt.config

2024-02-25 开发教程 Grunt 入门教程 匿名 3

grunt.config

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

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

初始化配置数据

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

grunt.config.init

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

grunt.config.init(configObject)

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

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

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

grunt.config

从项目的 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.setis called, otherwise grunt.config.getis called.

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

grunt.config.get

Get a value from the project's Grunt configuration. If propis specified, that property's value is returned, ornullif that property is not defined. If propisn't specified, a copy of the entire config object is returned. Templates strings will be recursively processed using the grunt.config.processmethod.

grunt.config.get([prop])

grunt.config.process

Process a value, recursively expanding <% %>templates (via the grunt.template.processmethod) in the context of the Grunt config, as they are encountered. this method is called automatically bygrunt.config.getbut 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 fooor foo.barproperty is a non-string (and not nullor undefined) value, it will be expanded to the actual value. That, combined with grunt's task system automatically flattening arrays, can be extremely useful.

grunt.config.getRaw

Get a raw value from the project's Grunt configuration, without processing <% %>template strings. Ifpropis specified, that property's value is returned, or nullif that property is not defined. If propisn't specified, a copy of the entire config object is returned.

grunt.config.getRaw([prop])

grunt.config.set

给当前项目的 Grunt 配置中的某个属性设置一个值。

grunt.config.set(prop, value)

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

grunt.config.escape

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

grunt.config.escape(propString)

grunt.config.merge

Added in 0.4.5

Recursively merges properties of the specified configObjectinto 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"]
}
});

Requiring Config Data

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

grunt.config.requires

如果需要的配置属性有一个或多个不存在、值为nullundefined,当前task将失败。此方法可以指定一个或多个字符串、配置属性数组作为参数。

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

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