Gulp – Combining Tasks

Gulp – Combining Tasks ”; Previous Next 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 − gulp.task(”task-name”, function() { //do stuff here }); 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”s take one plugin called minify-css to merge and minify all CSS scripts. It can be installed by using npm as shown in the following command − npm install gulp-minify-css –save-dev To work with “gulp-minify-css plugin”, you need to install another plugin called “gulp-autoprefixer” as shown in the following command − npm install gulp-autoprefixer –save-dev To concatenate the CSS files, install the gulp-concat as shown in the following command − npm install gulp-concat –save-dev After installation of plugins, you need to write dependencies in your configuration file as follows − var autoprefix = require(”gulp-autoprefixer”); var minifyCSS = require(”gulp-minify-css”); var concat = require(”gulp-concat”); 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 − gulp.task(”styles”, function() { gulp.src([”src/styles/*.css”]) .pipe(concat(”styles.css”)) .pipe(autoprefix(”last 2 versions”)) .pipe(minifyCSS()) .pipe(gulp.dest(”build/styles/”)); }); 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 − gulp styles Similarly, we will use another plugin called ‘gulp-imagemin’ to compress the image file, which can be installed using the following command − npm install gulp-imagemin –save-dev You can add dependencies to your configuration file using the following command − var imagemin = require(”gulp-imagemin”); You can create the task for above defined dependency as shown in the following code. gulp.task(”imagemin”, function() { var img_src = ”src/images/**/*”, img_dest = ”build/images”; gulp.src(img_src) .pipe(changed(img_dest)) .pipe(imagemin()) .pipe(gulp.dest(img_dest)); }); 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 − gulp imagemin Combining Multiple Tasks You can run multiple tasks at a time by creating default task in the configuration file as shown in the following code − gulp.task(”default”, [”imagemin”, ”styles”], function() { }); Gulp file is set up and ready to execute. Run the following command in your project directory to run the above combined tasks − gulp On running the task using the above command, you will get the following result in the command prompt − C:work>gulp [16:08:51] Using gulpfile C:workgulpfile.js [16:08:51] Starting ”imagemin”… [16:08:51] Finished ”imagemin” after 20 ms [16:08:51] Starting ”styles”… [16:08:51] Finished ”styles” after 13 ms [16:08:51] Starting ”default”… [16:08:51] Finished ”default” after 6.13 ms [16:08:51] gulp-imagemin: Minified 0 images Print Page Previous Next Advertisements ”;

Gulp – Optimizing CSS and JavaScript

Gulp – Optimizing CSS and JavaScript ”; Previous Next In this chapter, you will learn how to optimize CSS and JavaScript. Optimizing is required to remove unnecessary data (for e.g. spaces and unused characters) from the source files. It reduces the size of the files and allows them to load faster Install Plugins to Optimize CSS and JavaScript Go to “work” directory from your command line and install “gulp-uglify”, “gulp-minify-css” and “gulp-concat” plugins by using the following command − npm install gulp-uglify gulp-minify-css gulp-concat Declare Dependencies and Create Tasks In your configuration file gulpfile.js, first declare the dependencies as shown in the following code. var gulp = require(”gulp”); var concat = require(”gulp-concat”); var uglify = require(”gulp-uglify”); var minify = require(”gulp-minify-css”); Next, you need to create tasks for optimizing CSS and JavaScript as shown in the following code. gulp.task(”js”, function(){ gulp.src(”src/scripts/*.js”) .pipe(concat(”script.js”)) .pipe(uglify()) .pipe(gulp.dest(”build/scripts/”)); }); gulp.task(”css”, function(){ gulp.src(”src/styles/*.css”) .pipe(concat(”styles.css”)) .pipe(minify()) .pipe(gulp.dest(”build/styles/”)); }); gulp.task(”default”,[”js”,”css”],function(){ }); The js task will accepts .js files from src/scripts/ folder. It concatenates and uglifies the js files, then produces build/scripts/script.js file. The CSS task will accept .css files from src/styles/ folder. It concatenates and minifies CSS files, then produces build/styles/styles.css file. Run the Tasks The configuration file is set up and ready to execute. Use the following command to run the task. gulp On running the task using the above command, you will receive the following result in the command prompt. C:work>gulp [13:16:34] Using gulpfile C:workgulpfile.js [13:16:34] Starting ”js”… [13:16:34] Finished ”js” after 24 ms [13:16:34] Starting ”css”… [13:16:34] Finished ”css” after 6.05 ms [13:16:34] Starting ”default”… [13:16:34] Finished ”default” after 5.04 μs Print Page Previous Next Advertisements ”;

Gulp – Home

Gulp Tutorial PDF Version Quick Guide Resources Job Search Discussion Gulp is a task runner that uses Node.js as a platform. It purely uses the JavaScript code and helps to run front-end tasks and large-scale web applications. Gulp builds system automated tasks like CSS and HTML minification, concatenating library files, and compiling the SASS files. These tasks can be run using Shell or Bash scripts on the command line. This tutorial teaches you how to use Gulp to run front-end tasks and large-scale web applications in simple and easy steps. After completing this tutorial, you will find yourself at a moderate level of expertise in using Gulp from where you may take yourself to the next levels. Audience This tutorial has been prepared for beginners to help them understand the basic functionalities of Gulp. Prerequisites For this tutorial, it is assumed that you have prior knowledge of basic software development using Java or any other programming language. It should be of help if you have had some exposure to the software build and deployment process. Print Page Previous Next Advertisements ”;

Gulp – Basics

Gulp – Basics ”; Previous Next In this chapter, you will get acquainted with some basics related to Gulp. What is a Build System? A Build System is referred to as collection of tasks (collectively called as task runners), which automate the repetitive work. Following is a list of some of the tasks that can be handled using the build system − Compilation of preprocess CSS and JavaScript. Minification of files to reduce its size. Concatenation of files into one. Triggering the server for automatic reloading. Creation of deployment builds to store the resulting files in one location. In modern front-end workflow, the build system works with 3 components − Package managers Preprocessors Task runners and build tools Package Managers It is used to automate the installation upgrade, removal of required dependencies, clean libraries, and packages used in the development environment. Example for package managers are bower and npm. Preprocessors Preprocessors are very useful for an efficient modern workflow by adding an optimized syntax and additional features that compiles into its native language. Some of the popular preprocessors are − CSS − SASS, LESS and Stylus. JS − CoffeeScript, LiveScript, TypeScript, etc. HTML − Markdown, HAML, Slim, Jade, etc. Task Runners Task runners automate tasks like SASS to CSS conversion, minify the files, optimize images, and many other tasks used in the development workflow. Gulp is one of the task runner in the modern front-end work environment and it runs on Node. Setting Up Your Project To set your project in your computer, create a folder called “work” for example. The work folder contains following sub-folders and files − Src − Location of pre-processed HTML source files and folders. Images − Contains images which are uncompressed. Scripts − Contains multiple pre-processed script files. Styles − Contains multiple pre-processed CSS files. Build − This folder will be created automatically which contains the production files. Images − Contains compressed images. Scripts − Single script file that contains minified codes. Styles − Single CSS file that contains minified codes. gulpfile.js − It is the configuration file, which is used to define our tasks. Print Page Previous Next Advertisements ”;