{"id":7029,"date":"2019-05-13T21:10:13","date_gmt":"2019-05-13T21:10:13","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=7029"},"modified":"2019-05-13T21:10:13","modified_gmt":"2019-05-13T21:10:13","slug":"gulp","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/gulp\/","title":{"rendered":"Gulp"},"content":{"rendered":"<p>Whilst package manager&#8217;s such as yarn and npm make things pretty easy, sometimes we&#8217;ll want to extend our build workflow or the likes. Yarn allows the use of scripts, but another alternative would be a build tool such as Gulp. Ofcourse we could also write scripts in bash or Powershell etc. but Gulp offers a DSL in a similar way to tools such as Cake, Nant etc. to extend our various development and build workflows.<\/p>\n<p><strong>Getting started<\/strong><\/p>\n<p>A Gulp file is simply a JavaScript file (.js) in which we have the lines<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nvar gulp = require('gulp');\r\n\/\/ if we want to include yarn support in gulp\r\nvar yarn = require('gulp-yarn');\r\n<\/pre>\n<p>these include the gulp functions.<\/p>\n<p>To create a gulp task we include the following<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngulp.task('yarn-production', function() {\r\n   \/\/ return from the function()\r\n});\r\n<\/pre>\n<p>The string <em>yarn-production<\/em> is the task name, i.e. we can run the following command to execute our task<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngulp yarn-production\r\n<\/pre>\n<p><strong>Api&#8217;s<\/strong><\/p>\n<p><em>gulp.task<\/em><\/p>\n<p>As we&#8217;ve seen <em>gulp.task<\/em> is one of the gulp API&#8217;s which defines a task along with the task name, which can be executed via the gulp command line, followed by the function to run for that task.<\/p>\n<p><em>gulp.src<\/em><\/p>\n<p>The src API allows us to declare an array of filenames\/globs to be passed into the gulp pipeline, for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngulp.task('yarn-production', function() {\r\n    return gulp.src(&#x5B;'.\/package.json', '.\/yarn.lock'])\r\n       \/\/ pipeline\r\n    );\r\n});\r\n<\/pre>\n<p>This command basically creates an array of files etc. that will be used in the subsequent pipeline.<\/p>\n<p><em>gulp.dest<\/em><\/p>\n<p>We&#8217;ve got a src, so obviously we need a dest which denotes (as you&#8217;d expect) where the output from the pipeline goes to. So here&#8217;s an example which copies some files to a production folder<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngulp.task('production', function() {\r\n    return gulp.src(&#x5B;'.\/package.json', '.\/yarn.lock'])\r\n        .pipe(gulp.dest('production\/'));\r\n});\r\n<\/pre>\n<p>The <em>.pipe<\/em> creates the workflow of the gulp task.<\/p>\n<p>The .pipe can also take a function, for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngulp.task('production', function() {\r\n    return gulp.src(&#x5B;'.\/package.json', '.\/yarn.lock'])\r\n        .pipe(log)\r\n        .pipe(gulp.dest('production\/'));\r\n});\r\n<\/pre>\n<p>I&#8217;m not going to cover all the gulp functions as you can visit <a href=\"https:\/\/gulpjs.com\/docs\/en\/api\/concepts\" rel=\"noopener noreferrer\" target=\"_blank\">Gulp\/Concepts<\/a> for a list of the current API.<\/p>\n<p><strong>Writing our own pipe function<\/strong><\/p>\n<p>We&#8217;ve seen some example of using the <em>pipe<\/em> function with gulp functions but what about, if we want to write out own pipe functions.<\/p>\n<p>Here&#8217;s an example which simply logs the files that are to be transformed<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nvar gulp = require('gulp');\r\nvar through = require('through2');\r\n\r\nfunction log() {\r\n    return through.obj((file, encoding, callback) =&gt;\r\n    {\r\n        console.log(file.path);\r\n        return callback(null, file);\r\n    });\r\n}\r\n\r\ngulp.task('production', function() {\r\n    return gulp.src(&#x5B;'.\/package.json', '.\/yarn.lock'])\r\n        .pipe(log());\r\n});\r\n<\/pre>\n<p>In this sample we simply log the file path, but if (for example) we wanted to make some changes to the files (transform them) we&#8217;d use something like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nvar gulp = require('gulp');\r\nvar through = require('through2');\r\n\r\nfunction configure() {\r\n    return through.obj((file, encoding, callback) =&gt;\r\n    {   \r\n        var transformedFile = file.clone();\r\n        transformedFile.contents = new Buffer(&quot;Transformed file contents&quot;);\r\n        callback(null, transformedFile);\r\n    });\r\n}\r\n\r\ngulp.task('production', function() {\r\n    return gulp.src(&#x5B;'.\/package.json'])\r\n        .pipe(configure())\r\n        .pipe(gulp.dest('production\/'));\r\n});\r\n<\/pre>\n<p>This will simply output files with the src names with the context &#8220;Transformed file&#8221; within it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Whilst package manager&#8217;s such as yarn and npm make things pretty easy, sometimes we&#8217;ll want to extend our build workflow or the likes. Yarn allows the use of scripts, but another alternative would be a build tool such as Gulp. Ofcourse we could also write scripts in bash or Powershell etc. but Gulp offers a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[244],"tags":[],"class_list":["post-7029","post","type-post","status-publish","format-standard","hentry","category-gulp"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7029","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=7029"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7029\/revisions"}],"predecessor-version":[{"id":7064,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7029\/revisions\/7064"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=7029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=7029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=7029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}