With Node.js and Gulp globally installed on our machine, let's start our first gulp project
First, we need to make our project (root) directory. Make the directory like you normally do and give it a name. Let's keep it simple and make a new directory on the Desktop.
Let's navigate to it using the command line in Terminal.
Type cd
(with a space afterwards) and then drag the project directory from the Desktop onto the Terminal window. cd
stands for "change directory".
I've named my project directory "my-gulp-project". Here's the Terminal command after I dragged the project directory onto the Terminal window
cd /Users/Soren/Desktop/my-gulp-project
Once we hit ENTER, we're inside our project's root directory.
Let's make an index.html file, a CSS directory and a main.scss file inside the CSS directory.
We can make them with a text editor or with one command in Terminal.
// inside project root directory
touch index.html && mkdir css && cd $_ && touch main.scss && cd -
Here's what this command does
touch index.html // make index.html file
mkdir css // make directory named "css"
cd $_ // change to directory just made, "css"
touch main.scss // make main.scss file inside "css" directory
cd - // go back to previous directory, our project root
Once we've made the index.html file, CSS directory and main.scss file, this is our project structure
my-gulp-project/
css/
main.scss
index.html
It's time to add a new file: package.json
I'd try and compose an introductory paragraph about this file but others have said it better
Know that it is a file that is required and we add it to the project root directory, so let's make it!
Make package.json file the copy/paste way or via the command line.
A very basic package.json file will contain a name, a version number and a description. We need "devDependencies", too, which will automatically list the gulp packages when we install them in step 3 and step 4.
Copy and paste this code snippet and save it as a file called package.json inside our project root directory
{
"name": "my-gulp-project",
"version": "0.1.0",
"description": "My first Gulp project",
"devDependencies": {
}
}
A better alternative to the copy and paste technique, in my opinion, is to use the command line to make the file for us
// inside project root directory
npm init
The command line will walk us step-by-step through the data that is required for our package.json file. If we don't yet know the answer for a step, hit ENTER to skip to the next step. We can edit the package.json file with our text editor at any time if we wish to include or remove data.
To understand what data we can include inside the package.json file, visit package.json, An Interactive Guide by browsenpm.org
Our project structure after making package.json
my-gulp-project/
css/
main.scss
index.html
package.json
It's now time to install Gulp inside our project directory!
Let's use the command line to install Gulp locally inside our project directory. Type this command
// inside project root directory
npm install gulp --save-dev
--save-dev
means the Gulp packages we install will appear as "devDependencies" inside the package.json file.
This install will make a new directory for us named "node_modules" where the "gulp" package will be saved.
You may notice while Gulp is being installed locally the command line displays a few "warnings"
npm WARN package.json test@1.0.0 No repository field.
npm WARN package.json test@1.0.0 No README data
Remember, these are warnings not errors!
Our project structure after local Gulp install
my-gulp-project/
css/
main.scss
node_modules/
gulp
index.html
package.json
We're now ready to install our project specific Gulp packages!
Similar to how we installed the main Gulp package in step 3, let's install the other packages that will help run our project tasks.
Remember, we want to use Sass with our project, to minify our CSS and to automatically reload the browser whenever we hit save on a .html or .scss file. With this in mind, here is a list of the Gulp packages we're going to install
We can install packages one at a time or install all packages at the same time.
To find the command for a package, visit the package's web page which can usually be found at npmjs.com or github or with a quick search of a search engine. I've linked you to them above.
// example: install gulp-sass package
npm install gulp-sass --save-dev
npm install browser-sync gulp-autoprefixer gulp-minify-css gulp-rename gulp-sass --save-dev
Be patient! Some packages may take longer to install than others.
Our project structure after packages install
my-gulp-project/
css/
main.scss
node_modules/
browser-sync
gulp
gulp-autoprefixer
gulp-minify-css
gulp-rename
gulp-sass
index.html
package.json
With our project specific packages installed, there's one last file we need to make: gruntfile.js
The gulpfile.js file is where we make use of the packages we've installed in step 4. We'll be writing the gulpfile.js tasks in Part 2 so let's get prepared and make the file now.
Like before, We can make this file with a text editor or quickly via the command line
// inside project root directory
touch gulpfile.js
Our project structure after making gulpfile.js
css/
main.scss
node_modules/
browser-sync
gulp
gulp-autoprefixer
gulp-minify-css
gulp-rename
gulp-sass
gulpfile.js
index.html
package.json
Part 2 on its way