Web Tools: Gulp
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.
Clean node_modules Folder
- Go to the node_modules folder in conFusion, and delete all the folders/files there. We will not be using the Grunt modules existing there for this exercise.
Initialize package.json File
- Next, update the package.json file in the conFusion folder with the following content:
Installing Gulp
Note: You should already have Node and NPM installed on your computer before you proceed further. Also, those using OSX or Linux should use sudo while installing global packages in node (when you use the -g flag).
- 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
- 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
- Next, we will add the code for the JSHint task, the Clean task and the default task as follows:
- Next, paste in the code for the usemin, imagemin and copyfonts tasks:
- Finally, we add the code for the watch and browserSync tasks:
- Save the Gulp file
Running the Gulp Tasks
- At the command prompt, if you type gulp it will run the default task:
Using BrowserSync and Watch
- We configured the BrowserSync and the Watch tasks in the Gulp file. To use them, type the following at the command prompt:
You may need to reload the page in the browser.
- You can edit the menu.html file in the app folder and see the watch task and BrowserSync action in reloading the updated page.
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.
Комментариев нет:
Отправить комментарий