|
| 1 | +# Writing the Gulpfile |
| 2 | +It’s in this last step that we write the Gulp task(s). Most times, if not every time, you’ll think of some feature in the development or deployment of your project, Google the terms, then find yourself on either the NPMJS page for the Gulp plugin, or its GitHub page. |
| 3 | + |
| 4 | +In fact, let’s try it. Google “gulp html compressor,” without the inch marks. You’ll likely come across a handful of links to Gulp plugins for minifiying/compressing HTML. Let’s go with [`gulp-htmlmin`](https://www.npmjs.com/package/gulp-htmlmin). |
| 5 | + |
| 6 | +The NPMJS page for a Gulp plugin will usually have an **Install** section, one or more examples in the **Usage** section, and options under the **API** section. |
| 7 | + |
| 8 | +**Note**: Because Gulp 4 was released in January 2018, it’s still considered relatively new. As such, many examples on the NPMJS web site and on the web were written in Gulp 3.9.x. All examples in this tutorial and repo are written in Gulp 4.0.0. |
| 9 | + |
| 10 | +In the `compress-html` folder we’ve been working in during this tutorial, create a folder called `uncompressed-html`, then add an HTML document called `index.html` to it. |
| 11 | + |
| 12 | +Now, create an empty file called `gulpfile.js` in `compress-html`. Recall from step 2 of this tutorial that `gulpfile.js` is the `entry point` in `package.json`. Before continuing, ensure the folder `compress-html` has the following folders/files: |
| 13 | +* `package.json` |
| 14 | +* `uncompressed-html/index.html` |
| 15 | +* `gulpfile.js` |
| 16 | +* `node_modules` (there are too many folders/files to list under `node_modules`, so just ensure it exists.) |
| 17 | + |
| 18 | +Copy and paste the following into `gulpfile.js`. |
| 19 | + |
| 20 | +```javascript |
| 21 | +const { src, dest } = require(`gulp`); |
| 22 | +const htmlCompressor = require(`gulp-htmlmin`); |
| 23 | + |
| 24 | +let compressHTML = () => { |
| 25 | + return src(`uncompressed-html/*.html`) |
| 26 | + .pipe(htmlCompressor({collapseWhitespace: true})) |
| 27 | + .pipe(dest(`compressed-html/`)); |
| 28 | +} |
| 29 | + |
| 30 | +exports.compressHTML = compressHTML; |
| 31 | +``` |
| 32 | + |
| 33 | +Before I explain the syntax, run the following from your command line: |
| 34 | +```bash |
| 35 | +gulp compressHTML |
| 36 | +``` |
| 37 | + |
| 38 | +If there are no problems, then a new folder called `compressed-html` would have been added to the `compress-html` folder, and `index.html` from the folder `uncompressed-html` would have been copied into `compressed-html`, but minified/compressed. Open`index.html` in a text editor and verify that it, indeed, has been minified/compressed. |
| 39 | + |
| 40 | +Now, back to the code. |
| 41 | + |
| 42 | +## Line 1 |
| 43 | +```javascript |
| 44 | +const { src, dest } = require(`gulp`); |
| 45 | +``` |
| 46 | + |
| 47 | +Standing for source and destination, respectively, `src` and `dest` are Gulp functions used to source input (`src`) and to generate output (`dest`). |
| 48 | + |
| 49 | +## Line 2 |
| 50 | +```javascript |
| 51 | +const htmlCompressor = require(`gulp-htmlmin`); |
| 52 | +``` |
| 53 | + |
| 54 | +Recall that we installed the Gulp module/plug-in earlier by running `npm i -D gulp-htmlmin`, which installed `gulp-htmlmin` into the `node_modules` folder. This assignment gives us access to `gulp-htmlmin`’s features. |
| 55 | + |
| 56 | +## Lines 4–8 |
| 57 | +```javascript |
| 58 | +let compressHTML = () => { |
| 59 | + return src(`uncompressed-html/*.html`) |
| 60 | + .pipe(htmlCompressor({collapseWhitespace: true})) |
| 61 | + .pipe(dest(`compressed-html/`)); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +First, a note about task naming. Instead of echoing the name of the Gulp module in the task name, I name tasks based on what they do. This differs somewhat from the approach taken by many in the Gulp community. As an educator, I find this to be easier to understand. Thus, naming the task `compressHTML` is clearer than naming it something like `gulp-htmlmin`. |
| 66 | + |
| 67 | +Now, back to the code on lines 4–8. |
| 68 | + |
| 69 | +We establish a function, which we’ll also refer to as a task, called `compressHTML`, which will do the following: |
| 70 | +1. Source all HTML files in the `uncompressed-html` folder. |
| 71 | +2. Pipe, or send, the outputof all the HTML files to our `htmlCompressor` function, which, in turn, receives an object of options (a common Gulp feature) that collapses whitespace. |
| 72 | +3. Pipe the compressed HTML files, which are copies of the original files in the `uncompressed-html` folder, to the final destination: the `compressed-html` folder. If the folder doesn’t exist, the `dest` function will create it; the compressed files retain the names of the original files copied from the `compressed-html` folder. |
| 73 | + |
| 74 | +You’ve authored your first task. |
| 75 | + |
| 76 | +§ |
0 commit comments