Thursday, September 1, 2016

Up & Running With Gulp

Check Older version if installed

$ npm rm --global gulp
  Install Gulp Globally
$ npm install --global gulp-cli
Initializing Project directory
npm init
Installing Dependencies
npm install --save-dev gulp
Creatng gulpfile.js in root directory
var gulp = require('gulp');

gulp.task('default', function() {
  // place code for your default task here
});
Preprocessing with Gulp 

Compile Sass to CSS with the help of gulp-sass PKG

$ npm install gulp-sass --save-dev

 --save-dev flag to ensure that gulp-sass gets added to devDependencies in package.json Now add following line to gulpfile.js
var sass = require('gulp-sass');
  Now add following line in gulpfile.js
gulp.task('sass', function(){
    return gulp.src('css/sass/shuvrow.scss')
        .pipe(sass()) // Using gulp-sass
        .pipe(gulp.dest('css'))
});
Now add watch to change dynamically.
 Add following line on gulofile.js
gulp.task('watch', function(){
    gulp.watch('css/sass/shuvrow.scss', ['sass']);
    // Other watchers
})
Now run following command in terminal
gulp watch
Now you can see shuvrow.css file changes automatically.
Live Reloading => Auto sync 
Run Following command:

npm install browser-sync --save-dev
Add following line on gulpfile.js
var browserSync = require('browser-sync').create();
Now add following lines
gulp.task('browserSync', function() {
  browserSync.init({
    server: {
      baseDir: ''
    },
  })
})
Now update following code
gulp.task('sass', function(){
    return gulp.src('css/sass/main.scss')
        .pipe(sass()) // Using gulp-sass
        .pipe(gulp.dest('css'))
});
to
gulp.task('sass', function(){
    return gulp.src('css/sass/main.scss')
        .pipe(sass()) // Using gulp-sass
        .pipe(gulp.dest('css'))
        .pipe(browserSync.reload({
            stream: true
        }))
});
Now, we have to run both the watch and browserSync tasks at the same time for live-reloading
To do this update following codes
gulp.task('watch',function(){
    gulp.watch('css/sass/main.scss', ['sass']);
    // Other watchers
});
to
gulp.task('watch',['browserSync','sass'], function(){
    gulp.watch('css/sass/main.scss', ['sass']);
    // Other watchers
});
Now run gulp watch command on terminal to see all new changes on browser automatically.

0 comments:

Post a Comment