{"id":409,"date":"2020-09-16T12:06:00","date_gmt":"2020-09-16T16:06:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=409"},"modified":"2024-02-08T08:20:18","modified_gmt":"2024-02-08T13:20:18","slug":"getting-started-with-gulp-js","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/getting-started-with-gulp-js\/","title":{"rendered":"Getting started with gulp.js"},"content":{"rendered":"\n<p><a target=\"_blank\" href=\"https:\/\/molecularsciences.org\/web\/content\/www.gulpjs.com\" rel=\"noreferrer noopener\">Gulp.js<\/a>&nbsp;is a great tool for building and enhancing workflows. It is free and open source. It helps you automate repetitive tasks such as automating CSS creation and unit testing. In this article, I will walk you through many simple examples of using Gulp.js but first let&#8217;s look at the gulp API which only consists of 4 functions!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">gulp API<\/h3>\n\n\n\n<p><strong>gulp.dest<\/strong>&nbsp;is where you define you output file. This is where you will store output from a specific gulp task.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gulp.dest('\/output\/filepath');<\/code><\/pre>\n\n\n\n<p><strong>gulp.src<\/strong>&nbsp;is where you define you input file(s). The content of these file(s) will be passed to a specific gulp task as input.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gulp.dest('\/input\/filepath');<\/code><\/pre>\n\n\n\n<p><strong>gulp.task<\/strong>&nbsp;is where you define your tasks.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gulp.task(my'task', function() {\n  \/\/ some code\n});<\/code><\/pre>\n\n\n\n<p><strong>gulp.watch<\/strong>&nbsp;simply returns a eventemitter that triggers when something you are watching it modified.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gulp.watch('myfile.less', &#91;'lesstocss']);<\/code><\/pre>\n\n\n\n<p>This gulp command is watching the file myfile.less. When you save a change in myfile.less, it will call the function lesstocss which will then rebuild CSS files from myfile.less.<\/p>\n\n\n\n<p>See&nbsp;<a target=\"_blank\" href=\"https:\/\/github.com\/gulpjs\/gulp\/blob\/master\/docs\/API.md\" rel=\"noreferrer noopener\">gulp API documentation<\/a>&nbsp;for more information.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Compiling Less and Sass<\/h3>\n\n\n\n<p>CSS is easy when the CSS file is small. A large CSS file is painful to read and modify. LESS and SASS are CSS preprocessors. These languages allow you to write functions which then get compiled in CSS code. The following gulp example shows how to automate LESS to CSS conversion. First we install&nbsp;<a target=\"_blank\" href=\"https:\/\/www.npmjs.org\/package\/gulp-less\" rel=\"noreferrer noopener\">gulp-less<\/a>&nbsp;plugin.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ npm install --save-dev gulp-less<\/code><\/pre>\n\n\n\n<p>gulp code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ include plugins\nvar gulp = require('gulp');\nvar gless = require(\"gulp-less\");\n\n\/\/ compile less to css\ngulp.task('lesstocss', function () {\n  gulp.src('less\/test.less').pipe(gless()).pipe(gulp.dest('css'));\n});<\/code><\/pre>\n\n\n\n<p>Change the value of gulp.src() to the location of your LESS file. Change the location of gulp.dest() to the location of your CSS directory. To run<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ gulp lesstocss<\/code><\/pre>\n\n\n\n<p>Note that the code is very simple. First you download the relevant plugin. Then require it. We create a new function, lesstocss. The function simply takes the LESS file, pipes it to gulp-less and then pipes the output to the CSS directory. In a similar way, we can keep adding functions to automate more tasks. Following example shows how to compile SASS.<\/p>\n\n\n\n<p>First we install&nbsp;<a target=\"_blank\" href=\"https:\/\/www.npmjs.org\/package\/gulp-sass\" rel=\"noreferrer noopener\">gulp-sass<\/a>&nbsp;plugin.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ npm install --save-dev gulp-sass<\/code><\/pre>\n\n\n\n<p>gulp code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ include plugins\nvar gulp = require('gulp');\nvar gsass = require(\"gulp-sass\");\n\n\/\/ compile sass to css\ngulp.task('sasstocss', function () {\n  gulp.src('.\/sass\/filepath').pipe(gsass()).pipe(gulp.dest('cssdirectory'));\n});<\/code><\/pre>\n\n\n\n<p>Change the value of gulp.src() to the location of your LESS file. Change the location of gulp.dest() to the location of your CSS directory. To run<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ gulp sasstocss<\/code><\/pre>\n\n\n\n<p>The following section shows how to automate three tasks in one function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Minifying code<\/h3>\n\n\n\n<p>Minification refers to removing all unnecessary code such as whitespace, tab, \\n, etc. I don&#8217;t like minified code but it you choose to minify, following gulp code will help you. First, install the necessary plugins&nbsp;<a target=\"_blank\" href=\"https:\/\/www.npmjs.org\/package\/gulp-minify-css\" rel=\"noreferrer noopener\">gulp-minify-css<\/a>,&nbsp;<a target=\"_blank\" href=\"https:\/\/www.npmjs.org\/package\/gulp-minify-html\" rel=\"noreferrer noopener\">gulp-minify-html<\/a>, and&nbsp;<a target=\"_blank\" href=\"https:\/\/www.npmjs.org\/package\/gulp-uglify\" rel=\"noreferrer noopener\">gulp-uglify<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ npm install --save-dev gulp-minify-css gulp-minify-html gulp-minify-js<\/code><\/pre>\n\n\n\n<p>gulp code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ include plugins\nvar gulp = require('gulp');\nvar gmcss = require(\"gulp-minify-css\");\nvar gmhtml = require(\"gulp-minify-html\");\nvar gmjs = require(\"gulp-uglify\");\n\n\/\/ minify code\ngulp.task('minify-code', function () {\n  gulp.src('.\/css\/filepath').pipe(gmcss()).pipe(gulp.dest('CSS\/Destination'));\n  gulp.src('.\/htmldirectory\/*.html').pipe(gmhtml()).pipe(gulp.dest('HTML\/Destination'));\n  gulp.src('.\/scripts\/*.js').pipe(gmjs()).pipe(gulp.dest('JS\/Destination'));\n});<\/code><\/pre>\n\n\n\n<p>You need to change the values of gulp.src and gulp.dest to valid paths within your environment.. To run the code, simply type the following in command line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ gulp minify-code<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Compile CoffeeScript<\/h3>\n\n\n\n<p>CoffeeScript is a small language which compiles into JavaScript. It is cleaner to and more readable than JavaScript. First, install the necessary plugin&nbsp;<a target=\"_blank\" href=\"https:\/\/www.npmjs.org\/package\/gulp-coffee\" rel=\"noreferrer noopener\">gulp-coffee<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ npm install --save-dev gulp-coffee<\/code><\/pre>\n\n\n\n<p>gulp code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ include plugins\nvar gulp = require('gulp'), gcoffee = require('gulp-coffee');\n\n\/\/ compile coffee\ngulp.task('compile-coffee', function () {\n  gulp.src('coffee\/file').pipe(gcoffee()).pipe(gulp.dest('coffee\/output\/file'));\n});<\/code><\/pre>\n\n\n\n<p>You need to change the values of gulp.src and gulp.dest to valid paths within your environment.. To run the code, simply type the following in command line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ gulp compile-coffee<\/code><\/pre>\n\n\n\n<p>TypeScript is gaining more popularity than CoffeeScript. Automating TypeScript compilation require a few extra steps. I will cover it in a separate article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Gulp.js&nbsp;is a great tool for building and enhancing workflows. It is free and open source. It helps you automate repetitive tasks such as automating CSS creation and unit testing. In this article, I will walk you through many simple examples of using Gulp.js but first let&#8217;s look at the gulp API which only consists of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":410,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[185],"tags":[54],"class_list":["post-409","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-javascript"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/409","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=409"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/409\/revisions"}],"predecessor-version":[{"id":411,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/409\/revisions\/411"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media\/410"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}