Skip to content

Instantly share code, notes, and snippets.

@sgnl
Last active November 9, 2022 21:48
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save sgnl/2937a3f5767a7f1b765c to your computer and use it in GitHub Desktop.
Save sgnl/2937a3f5767a7f1b765c to your computer and use it in GitHub Desktop.
Setting up SASS (SCSS) files with gulp, gulp-sass, and Browser Sync!

Create a new temp project

  1. $ mkdir sass_gulp_workshop
  2. cd into the new directory
  3. Initialize NPM: $ npm init --yes
  4. Install gulp and gulp-sass packages: $ npm install -D gulp gulp-sass browser-sync
  5. Update package.json's scripts section with this key-value pair: "scripts": { "dev": "gulp" }
  6. Recreate this file structure in this directory:
  • public (directory)
    • css (directory)
    • index.html (file)
  • scss (directory)
    • partials (directory)
    • styles.scss (file)
  • gulpfile.js (file)
  • package.json (created by npm init --yes)
  1. Add the following code into the gulpfile.js file:
var gulp        = require('gulp');
var browserSync = require('browser-sync').create();
var sass        = require('gulp-sass');

// Static Server + watching scss/html files
gulp.task('serve', function() {

  browserSync.init({
      server: "./public"
  });

  gulp.watch("scss/**/*.scss", ['sass']);
  gulp.watch("public/*.html").on('change', browserSync.reload);
});

// Compile sass into CSS & auto-inject into browsers
gulp.task('sass', function() {
  return gulp.src("scss/styles.scss")
    .pipe(sass().on('error', sass.logError))
    .pipe(gulp.dest("public/css"))
    .pipe(browserSync.stream());
});

gulp.task('default', ['sass', 'serve']);
  1. Add the following code into the file located at scss/styles.scss:
body {
  background-color: red;
  
  h1 {
    color: purple;
  }
}

 note: this is just a test, dont ever actually nest starting with body element. 😬

  1. Add the following code into the public/index.html file:
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Sass Gulp Workshop</title>
  <link rel="stylesheet" href="/css/styles.css">
</head>
<body>
  <h1>Hello Syntatically Awesome Style Sheets!</h1>
</body>
</html>
  1. start developing with the command: npm run dev
@sgnl
Copy link
Author

sgnl commented Jul 23, 2016

To add this to a Project running Express you must use the proxy option

@woat
Copy link

woat commented Jan 11, 2018

great stuff, thanks!

@geovra
Copy link

geovra commented Feb 10, 2018

thank you!

@pierbotteroweb
Copy link

the "partials" folder is missign at the gulp.src.
return gulp.src("scss/partials/styles.scss")

The problem is that there is no error when the task runs... and the console.log also doesn't report the missing file.
Wierd...

@kimclerckx
Copy link

I think you import the partials into the styles.scss so it only needs to compile that one.
in styles.scss
@import "partials/yourpartialfilename"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment