{"id":79,"date":"2020-07-29T18:24:45","date_gmt":"2020-07-29T18:24:45","guid":{"rendered":"https:\/\/datatype.co.in\/blog\/?p=79"},"modified":"2020-08-22T10:33:10","modified_gmt":"2020-08-22T10:33:10","slug":"uglifying-javascript-files-with-gulp-tutorial","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/uglifying-javascript-files-with-gulp-tutorial\/","title":{"rendered":"Uglifying JavaScript Files With Gulp 4 &#8211; Tutorial"},"content":{"rendered":"\n<p>Gulp is a JavaScript (JS) library to automate slow, time consuming repetitive workflows such as minifying or uglifying JS and CSS files, copying files to distribution directory and so on. In this article I will discuss about uglifying or minifying JS files using Gulp 4.<\/p>\n\n\n\n<p><strong>1. Installing Gulp<\/strong>:<\/p>\n\n\n\n<p>Before we install Gulp, we will have to install <strong>nodejs<\/strong> if it is not already installed. You can download and install it from nodejs official website-<\/p>\n\n\n\n<p><a href=\"https:\/\/nodejs.org\/en\/\">https:\/\/nodejs.org\/en\/<\/a><\/p>\n\n\n\n<p>Once installed, run the following command in a terminal. It will output the installed nodejs  and npm versions.  <strong>npm <\/strong>is Nodejs Package Manager. <\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>node -v\nnpm -v<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"678\" height=\"342\" src=\"https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/07\/image-1.png\" alt=\"Installing nodejs\" class=\"wp-image-81\" srcset=\"https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/07\/image-1.png 678w, https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/07\/image-1-300x151.png 300w\" sizes=\"auto, (max-width: 678px) 100vw, 678px\" \/><\/figure>\n\n\n\n<p>Now let&#8217;s install Gulp. To install Gulp, run the following command in the terminal &#8211;<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>npm install -g gulp<\/code><\/pre>\n\n\n\n<p>This will install Gulp globally and we can use Gulp in all projects.<\/p>\n\n\n\n<p><strong>2. Writing Gulp Task:<\/strong><\/p>\n\n\n\n<p>Create a file <strong><em>gulpfile.js<\/em><\/strong>  in your project root directory. We will write all gulp task automation code in this file. Let&#8217;s install few more packages <strong><code>gulp-uglify<\/code> <\/strong>and <strong><code>gulp-rename <\/code><\/strong> which would be used in the uglifying process. Navigate to project to directory, open a terminal and run the following commands. This time we are installing the packages locally in our project.<\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>npm install gulp-uglify\nnpm install gulp-rename<\/code><\/pre>\n\n\n\n<p>Open gulpfile.js and add the following code. <\/p>\n\n\n\n<pre class=\"wp-block-code prettyprint\"><code>\/\/ Gulp version 4\nvar gulp = require(\"gulp\");\nvar uglify = require(\"gulp-uglify\");\nvar rename = require(\"gulp-rename\");\nconst { series } = require(\"gulp\");\n\n\/* \nDefine gulp task.\nThis task will uglify all js files in js directory \nand create uglified or minified files with min.js extension inside the \ndist\/js directory.\n*\/\n \ngulp.task( \"uglify\", gulp.series(async function () {\n         gulp\n\t.src(\"js\/*.js\")\n\t.pipe(uglify())\n\t.pipe(\n\t   rename(function (path) {\n\t     path.extname = \".min.js\";\n\t   })\n\t)\n\t.pipe(gulp.dest(\".\/dist\/js\/\"));\n  })\n);\n\n\/\/ Code to run gulp tasks\ngulp.task(\n\t\"default\",\n\tgulp.series(\"uglify\")\n);<\/code><\/pre>\n\n\n\n<p><strong>3. Running Gulp Task:<\/strong><\/p>\n\n\n\n<p>Now let&#8217;s test our code.  Type following command in the terminal (I am assuming that your terminal is pointing to project root directory).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gulp uglify<\/code><\/pre>\n\n\n\n<p>This command executes all gulp tasks defined in the <strong><em>gulpfile.js<\/em><\/strong> file. Check the <strong>dist <\/strong>folder which was just created by gulp. You will find all uglified js files with <strong>.min.js<\/strong> extension.<\/p>\n\n\n\n<p><strong>Important Update:<\/strong><\/p>\n\n\n\n<p>If one or more js files (to be uglified) contain JavaScript ES6 code, then <code><strong>uglify()<\/strong><\/code> method will fail and may throw following <strong>Unhandled &#8216;error&#8217; event<\/strong> exception. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>events.js:174\n      throw er; \/\/ Unhandled 'error' event\n      ^\nGulpUglifyError: unable to minify JavaScript<\/code><\/pre>\n\n\n\n<p> You can fix this issue by replacing the <strong><code>gulp-uglify<\/code><\/strong> package with <strong><code>gulp-uglify-es<\/code><\/strong>. Install this package and replace <strong><code>require(\"gulp-uglify\")<\/code><\/strong> with <code><strong>require('gulp-uglify-es').default<\/strong><\/code> in the gulp code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install gulp-uglify-es<\/code><\/pre>\n\n\n\n<p> In the <strong><em>gulpfile.js<\/em><\/strong> file, do following changes:  <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ var uglify = require(\"gulp-uglify\");\nvar uglify = require('gulp-uglify-es').default;<\/code><\/pre>\n\n\n\n<p> That&#8217;s all. This should fix the issue. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Gulp is a JavaScript (JS) library to automate slow, time consuming repetitive workflows such as minifying or uglifying JS and CSS files, copying files to distribution directory and so on.&nbsp;[ &hellip; ]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52,2,53,59],"tags":[169,170,54,80,79,76,78,81,56,57,77,58],"class_list":["post-79","post","type-post","status-publish","format-standard","hentry","category-gulp","category-javascript","category-nodejs","category-npm","tag-gulp-4-gulpfile-js","tag-gulp-4-tasks","tag-gulp-4-tutorial","tag-gulp-min-js","tag-gulp-rename","tag-gulp-uglify","tag-gulp-series","tag-gulp-src","tag-minify-js-files","tag-minify-js-npm","tag-npm-gulp","tag-uglify-js-example","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/79","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=79"}],"version-history":[{"count":40,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/79\/revisions"}],"predecessor-version":[{"id":465,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/79\/revisions\/465"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=79"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=79"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}