UNPKG

6.29 kBMarkdownView Raw
1[Grunt homepage](https://github.com/cowboy/grunt) | [Documentation table of contents](toc.md)
2
3# Getting Started
4
5**If you're starting from scratch, [install grunt](../#installing-grunt) and then check out the [init task](task_init.md), which will set up a new grunt-based project for you. Even if you don't ultimately use the files that are generated, you can very quickly learn how grunt works.**
6
7## The grunt.js file, aka "gruntfile"
8Each time grunt is run, it looks in the current directory for a file named `grunt.js`. If this file is not found, grunt continues looking in parent directories until that file is found. This file is typically placed in the root of your project repository, and is a valid JavaScript file comprised of these parts:
9
10* Project configuration
11* Loading grunt plugins or tasks folders
12* Tasks and helpers
13
14This is an example of a very basic sample `grunt.js` gruntfile that does all three of these things.
15
16```javascript
17module.exports = function(grunt) {
18
19 // Project configuration.
20 grunt.initConfig({
21 lint: {
22 all: ['grunt.js', 'lib/**/*.js', 'test/**/*.js']
23 },
24 jshint: {
25 options: {
26 browser: true
27 }
28 }
29 });
30
31 // Load tasks from "grunt-sample" grunt plugin installed via Npm.
32 grunt.loadNpmTasks('grunt-sample');
33
34 // Default task.
35 grunt.registerTask('default', 'lint sample');
36
37};
38```
39
40## A Very Important Note
41Your `grunt.js` gruntfile **must** contain this code, once and **only** once. If it doesn't, grunt won't work. For the sake of brevity, this "wrapper" code has been omitted from all future examples on this page, but it needs to be there. Like in the previous example.
42
43```javascript
44module.exports = function(grunt) {
45 // Your grunt code goes in here.
46};
47```
48
49## Project configuration
50
51Each grunt task relies on configuration information defined in an object passed to the [grunt.initConfig](api.md) method.
52
53For example, this basic config defines a list of files to be linted when the [lint task](task_lint.md) is run on the command line via `grunt lint`.
54
55```javascript
56// Project configuration.
57grunt.initConfig({
58 lint: {
59 all: ['lib/*.js', 'test/*.js', 'grunt.js']
60 }
61});
62```
63
64_Note: the [lint task](task_lint.md) is an example of a [multi task](api.md). You can run all targets of any multi task by just specifying the name of the task. In this case, running `grunt lint` would automatically run the `all` target and any others that might exist under `lint` instead of you having to run `grunt lint:all` explicitly._
65
66In another example, this very simple configuration saved in the root of a [jQuery repository](https://github.com/jquery/jquery) clone allows the jQuery QUnit unit tests to be run via grunt with `grunt qunit`. Note that even though jQuery's unit tests run in grunt doesn't mean they're going to actually pass. QUnit is running headless, after all!
67
68```javascript
69// Project configuration.
70grunt.initConfig({
71 qunit: {
72 index: ['test/index.html']
73 }
74});
75```
76
77You can store any arbitrary information inside of the configuration object, and as long as it doesn't conflict with a property one of your tasks is using, it will be ignored. Also, because this is JavaScript and not JSON, you can use any valid JavaScript here. This allows you to programatically generate the configuration object, if necessary.
78
79See the documentation [table of contents](toc.md) for a list of tasks whose documentation will explain their specific configuration requirements.
80
81_Also note that you don't need to specify configuration settings for tasks that you don't use._
82
83```javascript
84// Project configuration.
85grunt.initConfig({
86 // Project metadata, used by some directives, helpers and tasks.
87 meta: {},
88 // Lists of files to be concatenated, used by the "concat" task.
89 concat: {},
90 // Lists of files to be linted with JSHint, used by the "lint" task.
91 lint: {},
92 // Lists of files to be minified with UglifyJS, used by the "min" task.
93 min: {},
94 // Lists of files or URLs to be unit tested with QUnit, used by the "qunit" task.
95 qunit: {},
96 // Configuration options for the "server" task.
97 server: {},
98 // Lists of files to be unit tested with Nodeunit, used by the "test" task.
99 test: {},
100 // Configuration options for the "watch" task.
101 watch: {},
102 // Global configuration options for JSHint.
103 jshint: {},
104 // Global configuration options for UglifyJS.
105 uglify: {}
106});
107```
108
109## Loading grunt plugins or tasks folders
110
111While you can define [tasks and helpers](api.md) in your project's gruntfile, you can also load tasks from external sources.
112
113```javascript
114// Load tasks and helpers from the "tasks" directory, relative to grunt.js.
115grunt.loadTasks('tasks');
116
117// Load tasks and helpers from the "grunt-sample" Npm-installed grunt plugin.
118grunt.loadNpmTasks('grunt-sample');
119```
120
121_Note: loading externally defined tasks and helpers in this way is preferred to loading them via the analogous `--tasks` and `--npm` command-line options. This is because when tasks are created or loaded in the `grunt.js` gruntfile, the tasks effectively become part of the project and will always be used (provided they are available) whenever `grunt` is run._
122
123## Tasks and helpers
124
125You aren't required to define tasks in your project gruntfile, because grunt provides a number of built-in tasks. That being said, until you define a `default` task, grunt won't know what to do when you run it just as `grunt` without specifying any tasks, because grunt doesn't provide a default `default` task.
126
127The easiest way to define the default task is to create an [alias task](api.md).
128
129In the following example, a default task is defined that, when invoked by specifying `grunt` or `grunt default` on the command line, will execute the `lint`, `qunit`, `concat` and `min` tasks in-order. It behaves exactly as if `grunt lint qunit concat min` was run on the command line.
130
131```javascript
132// Default task.
133grunt.registerTask('default', 'lint qunit concat min');
134```
135
136_Note: choose the default tasks that make the most sense for your project. If you find yourself commonly executing other groups of tasks, create as many named aliases as you need!_
137
138Take a look at the [example gruntfiles](example_gruntfiles.md) or check out the [init task](task_init.md) for more configuration examples.