Skip to content

Commit 37211fa

Browse files
travmsindresorhus
authored andcommitted
Close GH-24: Initial gulp Documentation.
1 parent f2fb4e9 commit 37211fa

File tree

6 files changed

+263
-6
lines changed

6 files changed

+263
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1,51 @@
1-
# Getting started
1+
# Getting Started
2+
3+
## Installing gulp
4+
5+
First, we must install gulp globally so that we can access `gulp` from the command-line.
6+
7+
```
8+
npm install -g gulp
9+
```
10+
11+
Next, let's navigate to our project directory and we will install gulp locally and add it to our list of devDependencies. This allows our gulpfile, containing the build tasks, to access it.
12+
13+
```
14+
npm install --save-dev gulp
15+
```
16+
17+
## Creating Your Gulpfile
18+
Once gulp is installed, we need to create a file in your projects root directory and name it `gulpfile.js`. A gulpfile is the set of instructions that gulp will use to automate the tasks that you have set for your project. This file is where we will be doing all of our work and setting up our development tasks.
19+
20+
## Running Gulp
21+
Now that you have gulp installed and your `gulpfile.js` has been created. All that is left to do is simply run the gulp command in your command-line application. Type in the following line, and press enter.
22+
23+
```
24+
gulp
25+
```
26+
27+
Now, you should see a few lines of feedback from gulp notifying you of what is happening. This could be the tasks you're running or it could be an error. If you have recieved an error, just jump back into your gulpfile and make sure that you didn't make a typo somewhere.
28+
29+
If you are using the .watch() method in your gulpfile, then gulp will continue to run until you explicitly tell it to stop. This may be confusing to beginners, especially if you are expecting to go back to your command-line and start firing off new commands. To stop gulp at any time, simply press `CTRL+C` and it will stop the execution and allow you to begin writing new commands.
30+
31+
## Extending Gulp
32+
Plugins are a huge component to build systems and they allow you to perform additional actions in your tasks. This will introduce you to the process of finding and installing plugins so that you are comfortable when the time comes to add more functionality to your gulp tasks.
33+
34+
### Finding A Plugin
35+
We can search for plugins using the official gulp plugin search tool or we can search by keyword using the npm package search.
36+
37+
- [Official gulp Search Tool](http://gratimax.github.io/search-gulp-plugins/)
38+
- [gulpplugin on npm](https://npmjs.org/browse/keyword/gulpplugin)
39+
- [gulpfriendly on npm](https://npmjs.org/browse/keyword/gulpfriendly)
40+
41+
> Note: The __gulpfriendly__ keyword is assigned to plugins that do not accept streams of file objects but still work nicely in the gulp ecosystem.
42+
43+
### Installing A Plugin
44+
Installing a gulp plugin requires the same simple process we used when installing gulp. Once you have found the plugin that you would like to use, simply take note of its name and return to your command line to install it via npm. As a quick example, let's install gulp-concat. Open your command-line application and then run the following command:
45+
```
46+
npm install gulp-concat
47+
```
48+
It's that simple. You can pass any of the npm flags such as `--save` and `--save-dev` along with it if you would like to add that plugin to your package.json file as a dependency.
49+
50+
That's all there is to it! Let's start writing tasks.
51+

chapters/build-systems/gulp/gulp-topic-1.md

-1
This file was deleted.
+19-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
# Introduction
1+
# Introduction
2+
Gulp is a _streaming_ build system that allows you to automate tedious development tasks.
3+
4+
## What are Streams?
5+
Streams are designed to perform complex operations when constructed as a sequence of smaller, single purpose applications. These applications would connect from one end-point to a starting-point of another, allowing passage of data to be modified or analyzed at each application. This connection of applications is a concept referred to as piping, and the collection of pipes as a whole is referred to as the pipechain.
6+
7+
If you would like to learn more about streams, take a look at the following resources:
8+
- [Stream Handbook on Github](https://github.com/substack/stream-handbook "Stream Handbook on Github")
9+
- [Video: Harnessing The Awesome Power Of Streams - LXJS 2012](http://www.youtube.com/watch?v=lQAV3bPOYHo "Video: Harnessing The Awesome Power Of Streams - LXJS 2012")
10+
- [Video: AT&T Archives: The UNIX Operating System](http://youtu.be/tc4ROCJYbm0?t=5m32s "Video: AT&T Archives: The UNIX Operating System")
11+
12+
## Why Another Build System?
13+
Compared with other build systems, such as Grunt, gulp uses Node.js streams as a means to automate tasks, thereby removing the need to create intermediate files when transforming source files. In gulp, you would install plugins, that do one thing and do it well, and construct a 'pipeline' by connecting them to each other. Doing it in this manner, source files would be transformed by plugins, and output from one plugin would be an input to the next. This idea is similar to the concept of piplines in *nix systems. Some Node.js applications support pipelining as a feature. For example:
14+
15+
`browserify main.js | uglifyjs > bundle.js`
16+
17+
This can be intuitively translated into a gulp task. And if you can think in *nix pipelines, you will be able to easily construct gulp tasks.
18+
19+
Writing tasks in gulp requires you to use JavaScript (or a language that compiles to JavaScript) to set up the desirable build system. This code-over-configuration style allows the gulp user to be more flexible in setting up their tasks. The requirement placed on the user is knowing how to use Node.js streams and how they typically work.
+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
# References
1+
# References
2+
- [Stream Handbook](https://github.com/substack/stream-handbook "Stream Handbook on Github")
3+
- [Eric Schoffstall's gulp Slides](http://slid.es/contra/gulp "Eric Schoffstall's gulp Slides")
4+
- [Video: Harnessing The Awesome Power Of Streams - LXJS 2012](http://www.youtube.com/watch?v=lQAV3bPOYHo "Video: Harnessing The Awesome Power Of Streams - LXJS 2012")
5+
- [Video: AT&T Archives: The UNIX Operating System](http://youtu.be/tc4ROCJYbm0?t=5m32s "Video: AT&T Archives: The UNIX Operating System")

chapters/build-systems/gulp/toc.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
* [Introduction](introduction.md)
22
* [Getting started](getting-started.md)
3-
registerTask
4-
* [Gulp - Topic 1](gulp-topic-1.md)
3+
* [Writing Tasks](writing-tasks.md)
54
* [Troubleshooting](troubleshooting.md)
65
* [References](references.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
# Writing Tasks
2+
Once you have installed gulp, you can now begin writing the tasks that you would like to automate. These tasks could include (but are not limited to) concatenating files, compiling SASS, minifying JavaScript, or linting your code. In this section, we will identify some common development tasks and walkthrough how to automate them in gulp using pipes. These code examples should give you a solid enough understanding of how gulp works so that once you have completed this section you will be ready to write your own tasks from scratch.
3+
4+
## Concatenating Your Files
5+
Concatenating files is an important performance improvement because it reduces the amount of HTTP requests your project is required to make to display your website or application.
6+
### 1. Install Concat Plugin
7+
```
8+
npm install --save-dev gulp-concat
9+
```
10+
11+
### 2. Include Concat Plugin
12+
Now that we have installed our concat plugin locally, we need to include it in our gulpfile so that we may use it in our tasks.
13+
```
14+
var concat = require('gulp-concat');
15+
```
16+
17+
### 3. Create Concat Task
18+
Now, concatenating is as simple as passing a .pipe(concat('filename')) in your tasks pipechain. Like so:
19+
```
20+
gulp.task('concat', function(){
21+
gulp.src('src/js/*.js') // Targets All JS Files In Our src/ Directory
22+
.pipe(concat('all.js')) // Creates New all.js File With Code From Target Files
23+
.pipe(gulp.dest('dist')); // Places The New File In Our dist/ Directory
24+
});
25+
```
26+
> Reminder! The `.pipe()` is how we connect our smaller, single-purpose applications/libraries together. This collection of pipes is referred to as the _pipechain_.
27+
28+
This task looks for any changes that have been made to our .js files in our `src/js/` directory. It then takes all of those files and concatenates them into a new file named `all.js` and saves it into our compiled `dist/` directory for use in production. To avoid confusion, it is also worth noting that our gulp.dest() parameter is relative to our gulpfile.js.
29+
30+
## Linting Your Code
31+
Linting can save you from spending a lot of time blindly debugging your code by notifying you if you have made simple mistakes as you work on and save your files.
32+
### 1. Install JSHint Plugin
33+
```
34+
npm install --save-dev gulp-jshint
35+
```
36+
### 2. Include JSHint Plugin
37+
```
38+
var jshint = require('gulp-jshint');
39+
```
40+
### 3. Create Lint Task
41+
In your gulpfile add the following code:
42+
```
43+
gulp.task('lint', function(){
44+
gulp.src('src/js/*.js')
45+
.pipe(jshint())
46+
.pipe(jshint.reporter('default'));
47+
});
48+
```
49+
Now, when you run this task it will check for problems in your code and then send those along to the reporter that you have assigned which will output them in your command-line application. In this case we have used the default reporter for the sake of simplicity.
50+
51+
## Minifying Your Code
52+
Minifying your code is another performance improvement like concatenation except instead of reducing the amount of files, it reduces the size of your files. Using both together is a simple way to improve the efficiency and performance of your website or application.
53+
54+
### 1. Install Uglify Plugin
55+
```
56+
npm install --save-dev gulp-uglify
57+
```
58+
59+
### 2. Include Uglify Plugin
60+
Open your gulpfile.js and add the following code to the top.
61+
```
62+
var uglify = require('gulp-uglify');
63+
```
64+
65+
### 3. Create Minify Task
66+
Now, we will write our minify task. Add the following code to your gulpfile.
67+
```
68+
gulp.task('minify', function() {
69+
gulp.src('src/js/*.js')
70+
.pipe(uglify())
71+
.pipe(gulp.dest('dist'));
72+
});
73+
```
74+
75+
## CSS Preprocessing
76+
### 1. Install Preprocessor Plugin
77+
Identify the preprocessor that your project will be using (e.g. Sass, Less, Stylus, Myth) and then locate the correct plugin for gulp.
78+
79+
For instance, to install the gulp-sass plugin:
80+
```
81+
npm install --save-dev gulp-sass
82+
```
83+
84+
### 2. Include Preprocessor Plugin
85+
Now that we have installed the proper plugin, we need to include it at the top of our gulpfile. In our case we're using Sass, but this applies to any of the others as well.
86+
```
87+
var sass = require('gulp-sass');
88+
```
89+
90+
### 3. Create Preprocessing Task
91+
```
92+
gulp.task('styles', function() {
93+
gulp.src('src/scss/*.scss')
94+
.pipe(sass())
95+
.pipe(gulp.dest('dist'));
96+
});
97+
```
98+
99+
## Creating A Default Task & Watching Our Files
100+
The default task is the task that runs when you input `gulp` in your command line tool without passing it a specific task name. This will reference the other tasks that we have created including a new __watch__ task that will check for changes to our files and run our tasks each time we save them.
101+
```
102+
gulp.task('watch', function() {
103+
gulp.watch('src/js/*.js', [scripts]);
104+
gulp.watch('src/scss/*.scss', [styles]);
105+
});
106+
107+
gulp.task('default', ['scripts', 'styles', 'watch']);
108+
```
109+
110+
## Live Reload
111+
Live Reload allows us to refresh our browser window automatically when a file is saved. Implementing this is a little more involved and takes an additional step compared to the tasks that we have setup so far. Although, don't worry, it is still quite simple to do!
112+
113+
### 1. Install Plugins
114+
To get LiveReload working properly we need a couple plugins: tiny-lr and gulp-livereload.
115+
```
116+
npm install --save-dev gulp-livereload
117+
```
118+
The tiny-lr plugin is a _tiny_ implementation of the LiveReload server that allows us to communicate with our browser so that it can refresh our pages when we save our files. The gulp-livereload plugin works as a bridge between gulp and LiveReload and allows us to pipe our file changes to our tiny-lr server so it knows exactly when to refresh our browser.
119+
120+
### 2. Include Plugin In gulpfile
121+
```
122+
var livereload = require('gulp-livereload');
123+
```
124+
125+
### 3. Add LiveReload To Tasks
126+
Now, we need to make a few changes. First we need add an additional pipe to the tasks that we want to reload our browser. Second, we need to start our LiveReload server in our default task and assign it a port to listen to.
127+
```
128+
// Compile/Process Styles
129+
gulp.task('styles', function() {
130+
gulp.src('src/scss/*.scss')
131+
.pipe(sass())
132+
.pipe(gulp.dest('dist'))
133+
.pipe(livereload()); // Just Add This To Your Pipechain
134+
});
135+
136+
// Minify Scripts
137+
gulp.task('scripts', function() {
138+
gulp.src('src/js/*.js')
139+
.pipe(uglify())
140+
.pipe(gulp.dest('dist'))
141+
.pipe(livereload()); // Just Add This To Your Pipechain
142+
});
143+
144+
// Watch Task
145+
gulp.task('watch', function() {
146+
gulp.watch('src/js/*.js', [scripts]);
147+
gulp.watch('src/scss/*.scss', [styles]);
148+
});
149+
150+
// Default Task
151+
gulp.task('default', ['scripts', 'styles', 'watch']);
152+
```
153+
154+
### 4. Add LiveReload Script To Your Page
155+
Now that our gulpfile has been setup we need to add a reference to the livereload.js file on our pages so that our browser can properly communicate with tiny-lr. There are multiple ways of doing this and it's really up to your personal preference. Let's go over a couple, and you can decide which is best for you.
156+
157+
#### Manual
158+
If you wish to manually add the LiveReload script, then open up your HTML file and simply include it just as you would any other script.
159+
```
160+
<body>
161+
...
162+
<script src="http://localhost:35729/livereload.js"></script>
163+
</body>
164+
```
165+
That's it! You're ready to go. Keep in mind that you will have to manually include this on every page that you wish to reload automatically. In many cases that wont be much of a problem, but it is good to keep in mind if you run into issues.
166+
167+
#### Browser Extension
168+
If you prefer to avoid manually adding the script in yourself, you can download a simple browser extension that will add the script for you automatically.
169+
170+
In this example I will be using Chrome, but there are also extensions available for Firefox and Safari. To install, head over to the Chrome Webstore and install the [LiveReload extension](https://chrome.google.com/webstore/detail/livereload/jnihajbhpnppcggbcgedagnkighmdlei "LiveReload on Chrome Webstore"). Once you have it installed it will create a small icon that will allow you to enable or disable it quickly.
171+
172+
That's really all there is to it. For more information on the LiveReload browser extensions, visit the [LiveReload knowledgebase](http://feedback.livereload.com/knowledgebase/articles/86242-how-do-i-install-and-use-the-browser-extensions- "Browser Extensions on the LiveReload Knowledgebase").
173+
174+
## Chaining Actions Together
175+
The examples above are only performing a single action for the sake of simplicity, but you can actually chain many of those actions together into a single, more refined task. Gulp makes this incredibly easy.
176+
177+
For example, we have created both a concat and a minify task separately, but in most cases we would likely need to perform these actions within the same task. Let's take a look at an example of how this can be done inside of our scripts task that we created earlier.
178+
```
179+
// Concat & Minify Scripts
180+
gulp.task('scripts', function() {
181+
gulp.src('src/js/*.js')
182+
.pipe(concat('all.js'))
183+
.pipe(uglify())
184+
.pipe(gulp.dest('dist'));
185+
});
186+
```
187+
Now our scripts task not only minifies our code but also concatenates our JS files as well. By adding a single line to our pipechain we are now able to perform two actions within the same task instead of creating two separate tasks dedicated to a single action.

0 commit comments

Comments
 (0)