- What is Gulp:
- CSS Preprocessing.
- Unit Testing.
- Image optimization.
- Minification of JS and CSS files.
- Concatenating files.
- Reloading the browser etc.
- Installing Gulp:
- It’s a command line utility. It uses Node.js and installed via npm so it requires node and npm.
- Command to install Gulp:
npm install -g gulp
(-g is if you want to use it as a command line tool)
On successful installation, it will put the gulp command in your system path allowing it to be run from any directory.
- You can check gulp version and installation confirmation by running:
- Gulp setup requires two file that is package.json and a gulpfile named gulpfile.js.
- Gulp setup needs: package.json:
- This file is used by npm to store metadata for projects published as npm modules.
- This JSON file enables us to track and install all of our development dependencies.
- Below is a sample package.json having dependencies as gulp plugins which we will use for our demo so create a package.json in your root project directory and write below code in it and save.
- Now run
npm installto install gulp and gulp plugins.
- Gulp setup needs: gulpfile.js:
- This file is named gulpfile.js and is used to configure or define tasks and load gulp plugins.
- It is comprised of the following parts:
- task: used for defining a task.
- pipe: takes in a text stream and sends it to a function.
- watch: run a function when a file changes.
- src: get a stream from a source file.
- dest: send the stream to a new folder.
- Setting up our demo files:
- package.json: This file you must have already created from above post at point 4 and running ‘npm install’ to install all required packages/plugins for gulp.
- gulpfile.js: Below is the gulpfile.js code which we will use for our demo and it uses below gulp plugins:
- concat: To concat all files.
- rename: To make sure HTML is written correctly that is validating HTML page with htmlhint.
- uglify: To minify js file.
- watch: To watch mentioned path/files for modifications and then apply concat/rename/uglify if files are changed or new files are created at a specified path.
Create a gulpfile.js in your root project directory and write below code in it and save.
- For testing gulp, we are using two simple js file: js/test_one.js and js/test_two.js
- That’s all for our demo files so the structure of files should look like below:
- Using gulp: (Run all below mentioned commands from command line from the root path of your project)
gulpand you will see that all js file content will be concatenated to a file new global.js, then renamed it to global.min.js, then minified it and store at build/js.
- Run gulp watch and modify any js file and see in your command line that gulp will again process your file as it keeps watching for the new file or changes to an existing file and the file global.min.js will be re-created at build directory.
- So this is how you can start with gulp.js and learn how to use it.
There are many more Gulp plugins available which you can check at http://gulpjs.com/plugins/ and start using it in your project as per your need.