1 | [Grunt homepage](https://github.com/cowboy/grunt) | [Documentation table of contents](toc.md)
|
2 |
|
3 | # [The grunt API](api.md) / grunt.config
|
4 |
|
5 | Access project-specific configuration data defined in the [grunt.js gruntfile](getting_started.md).
|
6 |
|
7 | See the [config lib source](../lib/grunt/config.js) for more information.
|
8 |
|
9 | ## The config API
|
10 |
|
11 | Note 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 ☃
|
17 | Initialize 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
|
20 | grunt.config.init(configObject)
|
21 | ```
|
22 |
|
23 | Note that any specified `<config>` and `<json>` [directives](api_task.md) will be automatically processed when the config object is initialized.
|
24 |
|
25 | This example contains sample config data for the [lint task](task_lint.md):
|
26 |
|
27 | ```javascript
|
28 | grunt.config.init({
|
29 | lint: {
|
30 | all: ['lib/*.js', 'test/*.js', 'grunt.js']
|
31 | }
|
32 | });
|
33 | ```
|
34 |
|
35 | See 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
|
41 | 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']`.
|
42 |
|
43 | 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.
|
44 |
|
45 | ### grunt.config
|
46 | 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.
|
47 |
|
48 | ```javascript
|
49 | grunt.config([prop [, value]])
|
50 | ```
|
51 |
|
52 | ### grunt.config.get
|
53 | 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.
|
54 |
|
55 | ```javascript
|
56 | grunt.config.get([prop])
|
57 | ```
|
58 |
|
59 | Any `<% %>` 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
|
62 | Set a value into the project's grunt configuration.
|
63 |
|
64 | ```javascript
|
65 | grunt.config.set(prop, value)
|
66 | ```
|
67 |
|
68 | Note 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
|
71 | Escape `.` dots in the given `propString`. This should be used for property names that contain dots.
|
72 |
|
73 | ```javascript
|
74 | grunt.config.escape(propString)
|
75 | ```
|
76 |
|
77 | ### grunt.config.process
|
78 | Behaves like `grunt.config.get`, but additionally recursively processes all `<% %>` templates in the returned data.
|
79 |
|
80 | ```javascript
|
81 | grunt.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 ☆
|
88 | Fail 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
|
91 | grunt.config.requires(prop [, prop [, ...]])
|
92 | ```
|
93 |
|
94 | _This method is also available inside tasks as [this.requiresConfig](api.md)._
|