Skip to content

Commit 556ebe4

Browse files
committed
Initial commit
0 parents commit 556ebe4

28 files changed

+18154
-0
lines changed

.gitignore

Lines changed: 637 additions & 0 deletions
Large diffs are not rendered by default.

01--installation-instructions.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Installation
2+
3+
## 1 — Node and NPM
4+
Download Node from [https://nodejs.org/en/download/](https://nodejs.org/en/download/).
5+
6+
### Check the Node, NPM, and NPX Installation Versions
7+
8+
```bash
9+
node -v
10+
```
11+
12+
```bash
13+
npm -v
14+
```
15+
16+
```bash
17+
npx -v
18+
```
19+
20+
## 2 — Gulp
21+
Install (`i`) the Gulp command line interface (`gulp-cli`) globally (`-g`) via NPM (`npm`).
22+
23+
```bash
24+
npm i -g gulp-cli
25+
```
26+
27+
### Check the Gulp Installation Version
28+
```bash
29+
gulp -v
30+
```
31+
32+
§
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## Creating `package.json`
2+
Gulp requires a manifest file called `package.json` that configures its tasking environment. We’ll cover the the basics that most people use, but you can look at [https://docs.npmjs.com/files/package.json](https://docs.npmjs.com/files/package.json) for more.
3+
4+
Create a folder anywhere on your computer that isn’t this repo and name it `compress-html`. (A folder in this repo called `compress-html` already exists. Ignore it.) Navigate into the folder and initialize the creation of `package.json`:
5+
```bash
6+
npm init
7+
```
8+
You are now in interactive mode with NPM to populate `package.json` with some basic entries.
9+
10+
The first entry you’re required to interact with is for the package name. The default, rendered in parentheses, is the name of the folder in which `npm init` was run. Items in parentheses become the default value if the user hits `return`. Items without parentheses enter blank values if the user hits `return`.
11+
```bash
12+
package name: (compress-html)
13+
```
14+
15+
Take the default. As your projects improve, you would update its version number.
16+
```bash
17+
version: (1.0.0)
18+
```
19+
20+
Taking a moment to describe your project will help you to distinguish one project from another.
21+
```bash
22+
description:
23+
```
24+
25+
If you were authoring a Node program, `index.js` might be the program that initiates your project. However, because our project is a Gulp task running project, we use `gulpfile.js` (or `Gulpfile.js`) as the entry point.
26+
```bash
27+
entry point: (index.js): gulpfile.js
28+
```
29+
30+
This setting allows for tests to be run using `npm run-script SCRIPT`, where `SCRIPT` is one or more command(s)/script(s) you might use to test your project before deployment. At the outset, we won’t need it. Hit `return`.
31+
```bash
32+
test command
33+
```
34+
35+
If your project is being synced with a remote, that would go here. Leave it blank at outset.
36+
```bash
37+
git repository:
38+
```
39+
40+
These keywords are used by `npmjs` to search. Leave these empty for now.
41+
```bash
42+
keywords:
43+
```
44+
45+
Enter your name
46+
```bash
47+
author:
48+
```
49+
50+
This option takes an SPDX-formatted license initialism, listed at [https://spdx.org/licenses/](https://spdx.org/licenses/). Take the default.
51+
```bash
52+
license: (ISC)
53+
```
54+
55+
And, finally, a report of all your entries is given to you for verification. If it all looks correct, hit `return`.
56+
```bash
57+
Is this OK? (yes)
58+
```
59+
One last note. If you want to stand up a Gulp project and you’re willing to take all the defaults for your `package.json` file, simply run `npm init --yes`.
60+
61+
§

03--installing-gulp-modules.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Installing Gulp Modules
2+
With `package.json` in place, we can now choose the Gulp module(s) we need in order to auto-task our development work. This is usually done with the command `npm i -D MODULE`: `i`nstall `MODULE` and save it for use during `-D`evelopment. (The long form of `npm i -D MODULE` is `npm install --save-dev MODULE`.)
3+
4+
`MODULE` is the name of a Gulp module we’d invoke in a task. As an example, let’s use `gulp-htmlmin`, which is a module that compresses HTML files. In the same folder in which `package.json` was created, let’s run:
5+
```bash
6+
npm i -D gulp-htmlmin
7+
```
8+
9+
This will search for `gulp-htmlmin` in the [NPM registry](https://registry.npmjs.org/), then, if found, download the `gulp-htmlmin` module into a folder called `node_modules`. If `node_modules` doesn’t exist, this module will create it. (Every first module fetched from the registry will create `node_modules` if `node_modules` isn’t found.)
10+
11+
If you open `package.json`, you’ll note that a new object called `devDependencies` was added with a member whose name is `"gulp-htmlmin"` and whose value is `"^5.0.1"` (`"gulp-htmlmin": "^5.0.1"`). This means that the versions of `gulp-htmlmin` that will be used range from `5.0.1``6.0.0`, inclusive and exclusive, respectively. (Read more at [https://docs.npmjs.com/misc/semver#caret-ranges-123-025-004](https://docs.npmjs.com/misc/semver#caret-ranges-123-025-004).)
12+
13+
Let’s also install a local version of Gulp:
14+
```bash
15+
npm i -D gulp
16+
```
17+
18+
Note the new member object in `devDependencies`.
19+
20+
§

04--writing-the-gulpfile.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
§

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Getting Started
2+
Gulp is a stream-based, JavaScript task runner that is derived from Node via NPM. It automates many of the tasks used in front end development, such as compressing files, linting syntax, and creating production versions of development files. Before we start, we need to install Node, which is packaged with NPM, and Gulp.
3+
4+
## Installation the Required Software
5+
[01--installation-instructions.md](01--installation-instructions.md)
6+
7+
## Creating the `package.json` Manifest File
8+
[02--creating-package-json-instructions.md](02--creating-package-json-instructions.md)
9+
10+
## Installing Gulp Modules/Plug-Ins
11+
[03--installing-gulp-modules.md](03--installing-gulp-modules.md)
12+
13+
## Writing the Gulpfile
14+
[04--writing-the-gulpfile.md](04--writing-the-gulpfile.md)
15+
16+
There are other useful Gulp recipes in this repo’s `gulp-recipes` folder. Study them; make them your own.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { src, dest } = require(`gulp`);
2+
const sass = require(`gulp-sass`);
3+
4+
let compileCSS = () => {
5+
return src(`sass/main.scss`)
6+
.pipe(sass({
7+
outputStyle: `expanded`,
8+
precision: 10
9+
}).on(`error`, sass.logError))
10+
.pipe(dest(`css/`));
11+
};
12+
13+
exports.compileCSS = compileCSS;

0 commit comments

Comments
 (0)