gulp - Gulp Combining Tasks - gulp sass - gulp tutorial - learn gulp
gulp tutorials tag - gulp , gulp sass , gulp watch , gulp meaning , gulp js , gulp uglify , concat javascript , eisi gulp , gulp concat , gulp install , gulp tutorial , what is gulp , npm install gulp , gulpjs
How to combine tasks in gulpjs?
- Gulp configuration file consists of task modules.
- The Gulp task will have following structure
gulp tutorials tag - gulp , gulp sass , gulp watch , gulp meaning , gulp js , gulp uglify , concat javascript , eisi gulp , gulp concat , gulp install , gulp tutorial , what is gulp , npm install gulp , gulpjs
gulp single destination code :
gulp tutorials tag - gulp , gulp sass , gulp watch , gulp meaning , gulp js , gulp uglify , concat javascript , eisi gulp , gulp concat , gulp install , gulp tutorial , what is gulp , npm install gulp , gulpjs
gulp multi destination code :
- In order to register a task in gulp file we want to call gulp.task function which accepts two parameters.
- First parameter name-of-task is considered as task name and second parameter is anonymous function block which defines the task actions.
- gulp.task will register function as task within the name. This name is used to specify the dependency on other gulp tasks.
Being able to tell us that it is running is a fine task, but lets get gulp to do some real tasks for us.
- We'll start with simple tasks and work our way up.
- Task enables a modular approach to configure Gulp.
- We need to create task for each dependency, which we would add up as we find and install other plugins.
- The Gulp task will have following structure
- Where “task-name” is a string name and “function()” performs your task.
- The “gulp.task” registers the function as a task within the name and specifies the dependencies on other tasks.
Installing Plugins
- Let us take one plugin called minify-css to merge and minify all CSS scripts. It can be installed by using npm as given in the following command
- To work with “gulp-minify-css plugin”, you should install another plugin called “gulp-autoprefixer” as given in the following command
- To concatenate the CSS files, install the gulp-concat as given in the following command
- After installation of plugins, you should write dependencies in your configuration file as follows
Adding Task to Gulp file
- We need to create task for each dependency, which we would add up as we install the plugins.
- The Gulp task will have following structure
- The ‘concat’ plugin concatenates the CSS files and ‘autoprefix’ plugin indicates the current and the previous versions of all browsers.
- It minifies all CSS scripts from src folder and copies to the build folder by calling ‘dest’ method with an argument, which represents the target directory.
- To run the task, use the following command in your project directory
- Also, we will use another plugin called ‘gulp-imagemin’ to compress the image file, which can be installed using the following command
- You can add dependencies to your configuration file using the following command
- You can create the task for above defined dependency as shown in the following code.
- The images are located in “src/images/**/*” which are saved in the img_srcobject. It is piped to other functions created by the ‘imagemin’ constructor.
- It compresses the images from src folder and copies to the build folder by calling ‘dest’ method with an argument, which represents the target directory.
- To run the task, use the following command in your project directory
Combining Multiple Tasks
- You can run multiple tasks at a time by creating default task in the configuration file.
gulp tutorials tag - gulp , gulp sass , gulp watch , gulp meaning , gulp js , gulp uglify , concat javascript , eisi gulp , gulp concat , gulp install , gulp tutorial , what is gulp , npm install gulp , gulpjs
Code
- Gulp file is set up and ready to execute. Run the following command in your project directory to run the above combined tasks
Output
- On running the task using the above command, you will get the following result in the command prompt