This Gist outlines the gulp workflow that will:
- watch for any scss changes, then compiles scss source into css
- watch for any changes in the public directory, and trigger live-reload
- serve static content in
public/
This Gist also assumes you already know how to install npm modules, gitignore, create directories and create files via the command line.
First, make sure you have all the required dependencies. If you do not have npm or gulp installed globally, you need to install them. If you already have these installed, skip to the next step.
to install npm
brew install npm
to install gulp
npm install -g gulp
- run
npm init
to initialize your project with apackage.json
file - set up your
.gitignore
file to ignorenode_modules
- install and save required dependencies:
npm install -D gulp
npm install -D gulp-sass
npm install -D gulp-connect
- set up a
scss
source directory in the root directory. - set up scss source file
styles.scss
and an emptypartials
directory in the scss directory - set up the output directory
public
in the root directory. - set up the compiled
css
directory inside the public directory. - set up root html5 template file
public/index.html
- add the markup for a basic html5 template in
public/index.html
. (Use Emmett and it will take 2 keystrokes!!) - in the index file head, load
/css/styles.css
- include an
h1
element in the body with the content ofHello SCSS!
- set up a Gulpfile
touch gulpfile.js
- set up the Gulpfile tasks. Contents of gulpfile.js:
var gulp = require('gulp');
var sass = require('gulp-sass');
var connect = require('gulp-connect');
gulp.task('connect', function(){
connect.server({
root: 'public',
livereload: true
});
});
// keeps gulp from crashing for scss errors
gulp.task('sass', function () {
return gulp.src('./scss/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./public/css'));
});
gulp.task('livereload', function (){
gulp.src('./public/**/*')
.pipe(connect.reload());
});
gulp.task('watch', function () {
gulp.watch('./scss/**/*.scss', ['sass']);
gulp.watch('./public/**/*', ['livereload']);
});
gulp.task('default', ['connect', 'watch', 'sass']);
-
In the
scss/styles.scss
file, write the "Hello World" of SCSS (we are checking if variables work. If #333333 loads as the background color, variables are working, and thus, sass is working).$dark-color : #333333; body { background: $dark-color; }
-
run
gulp
in your terminal -
open the compiled
styles.css
file in SublimeText (/public/css/styles.css) -
check if it is proper css
-
open a browser
open localhost:8080
-
you should see a dark grey background and your single black h1 text
- while you terminal, browser, and sublime text are all visible, add the following to your
styles.scss
h1 {
color: #F1F1F1;
}
- note livereload will only track files that it sees when you first start the gulp task. including the initial generated css files, after the first time you compile, you'll have to restart gulp!
- once you save this file, your browser should automatically refresh, and your h1 element should be white.
In order for the webpage to render and scale properly on mobile devices, add this meta tag to your public/index.html
file in the the <head>
element
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
Once gulp sass and livereload are wired up correctly, commit your new files
While you work, make sure to keep your "gulp" terminal running and visible, if your sass source is invalid, it will crash the watching gulp process, and you will want to see the error message that will output in that terminal. Then you can restart your gulp
watcher after fixing your errors.
Anytime you add new files to your public/
directory (like images or javascript), you may need to kill then restart your gulp task