Objectives and Outcomes
In this exercise, you will learn to use Gulp, the task runner. You will install Gulp CLI and install Gulp plugins using NPM. Thereafter you will configure a Gulp file with a set of tasks to build and serve your web project. At the end of this exercise, you will be able to:
- Install Gulp CLI and Gulp plugins in your project
- Configure a Gulp file with a set of tasks to build a web project from a source, and serve the built project using a server.
Installing Gulp
- At the command prompt, type the following to install Gulp command-line interface (CLI) globally:
This will install the Gulp globally so that you can use it in all projects.
- Next install Gulp to use within your project. To do this, go to the conFusion folder and type the following at the prompt:
This will install local per-project Gulp to use within your project.
Install Gulp Plugins for SASS and Browser-Sync
- Install all the Gulp plugins that you will need for this exercise. To do this, type the following at the command prompt:
Creating a Gulp File
- Next you need to create a Gulp file containing the tasks to be run when you use Gulp. To do this, create a file named gulpfile.js in the conFusion folder.
Loading Gulp Plugins
- Load in all the Gulp plugins by including the following code in the Gulp file:
Adding Gulp Tasks for SASS and Browser-Sync
- Next, we will add the code for the SASS task, the Browser-Sync task and the default task as follows:
- Save the Gulp file
Running the Gulp Tasks
- At the command prompt, if you type gulp it will run the default task:
- Do a Git commit with the message "Gulp Part 1".
Conclusions
In this exercise, you learnt to use Gulp, install Gulp plugins, configure the gulpfile.js and then use Gulp to automate the web development tasks.
Gulp Part 2
Objectives and Outcomes
In this exercise, you will continue to learn to use Gulp. Thereafter you will configure a Gulp file with a set of tasks to build and serve your web project. At the end of this exercise, you will be able to:
- Configure the Gulp file with a set of tasks to build the distribution folder for the web project.
Copying the Files and Cleaning up the Dist Folder
- We will now create the tasks for copying the font files and cleaning up the distribution folder. To do this we will first install the del Node module and require it in the Gulp file as follows:
- Next, we will add the code for the Clean task and the copyfonts task as follows:
Compressing and Minifying Images
- We will now install the gulp-imagemin plugin and configure the imagemin task. To do this we install the plugin and require it as follows:
- Next, we create the imagemin task as follows:
Preparing the Distribution Folder and Files
- We now install the gulp-usemin and other related Gulp plugins and require them as follows:
- We configure the usemin and the build task as follows:
- Save the Gulp file
Running the Gulp Tasks
- At the command prompt, if you type gulp build it will run the build task:
- Do a Git commit with the message "Gulp Part 2"
Conclusions
In this exercise, you learnt to use Gulp, install Gulp plugins, configure the gulpfile.js and then use Gulp to automate the web development tasks.
Grunt Resources
- Grunt
- Writing an Awesome Build Script with Grunt
- Clean Grunt
- File Globbing
- The Command Line for Web Design: Automation With Grunt
Grunt Plugins
- grunt-contrib-jshint
- jshint-stylish
- grunt-contrib-copy
- grunt-contrib-clean
- grunt-usemin
- grunt-contrib-concat
- grunt-contrib-cssmin
- grunt-contrib-htmlmin
- grunt-contrib-uglify
- grunt-filerev
Gulp Resources
- Gulp
- An Introduction to Gulp.js
- Getting started with gulp
- Building with Gulp
- The Command Line for Web Design: Automation with Gulp
Gulp Plugins
- gulp
- gulp-sass
- browser-sync
- del
- gulp-imagemin
- gulp-uglify
- gulp-usemin
- gulp-rev
- gulp-clean-css
- gulp-flatmap
- gulp-htmlmin
Tasks
General Resources
- Node, Grunt, Bower and Yeoman - A Modern web dev's Toolkit
- The Advantages of Using Task Runners
- Gulp vs Grunt. Why one? Why the Other?
- Why we should stop using Grunt & Gulp
- Why I Left Gulp and Grunt for npm Scripts
更多专业前端知识,请上 【猿2048】www.mk2048.com