UNPKG

3.95 kBMarkdownView Raw
1[Grunt homepage](https://github.com/cowboy/grunt) | [Documentation table of contents](toc.md)
2
3# [The grunt API](api.md) / grunt.config
4
5Access project-specific configuration data defined in the [grunt.js gruntfile](getting_started.md).
6
7See the [config lib source](../lib/grunt/config.js) for more information.
8
9## The config API
10
11Note that any method marked with a ☃ (unicode snowman) is also available directly on the `grunt` object, and any method marked with a ☆ (white star) is also available inside tasks on the `this` object. Just so you know. See the [API main page](api.md) for more usage information.
12
13## Initializing Config Data
14_Note that the method listed below is also available on the `grunt` object as [grunt.initConfig](api.md)._
15
16### grunt.config.init ☃
17Initialize a configuration object for the current project. The specified `configObject` is used by tasks and helpers and can also be accessed using the `grunt.config` method. Nearly every project's [grunt.js gruntfile](getting_started.md) will call this method.
18
19```javascript
20grunt.config.init(configObject)
21```
22
23Note that any specified `<config>` and `<json>` [directives](api_task.md) will be automatically processed when the config object is initialized.
24
25This example contains sample config data for the [lint task](task_lint.md):
26
27```javascript
28grunt.config.init({
29 lint: {
30 all: ['lib/*.js', 'test/*.js', 'grunt.js']
31 }
32});
33```
34
35See the [configuring grunt](getting_started.md) page for more configuration examples.
36
37_This method is also available as [grunt.initConfig](api.md)._
38
39
40## Accessing Config Data
41The 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']`.
42
43Note 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.
44
45### grunt.config
46Get 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.
47
48```javascript
49grunt.config([prop [, value]])
50```
51
52### grunt.config.get
53Get 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.
54
55```javascript
56grunt.config.get([prop])
57```
58
59Any `<% %>` templates in returned values will not be automatically processed, but can be processed afterwards using the [grunt.template.process](api_template.md) method. If you want to do both at once, the `grunt.config.process` method can be used.
60
61### grunt.config.set
62Set a value into the project's grunt configuration.
63
64```javascript
65grunt.config.set(prop, value)
66```
67
68Note that any specified `<config>` and `<json>` [directives](api_task.md) will be automatically processed when the config data is set.
69
70### grunt.config.escape
71Escape `.` dots in the given `propString`. This should be used for property names that contain dots.
72
73```javascript
74grunt.config.escape(propString)
75```
76
77### grunt.config.process
78Behaves like `grunt.config.get`, but additionally recursively processes all `<% %>` templates in the returned data.
79
80```javascript
81grunt.config.process([prop])
82```
83
84## Requiring Config Data
85_Note that the method listed below is also available inside tasks on the `this` object as [this.requiresConfig](api.md)._
86
87### grunt.config.requires ☆
88Fail the current task if one or more required config properties is missing. One or more string or array config properties may be specified.
89
90```javascript
91grunt.config.requires(prop [, prop [, ...]])
92```
93
94_This method is also available inside tasks as [this.requiresConfig](api.md)._