Wednesday, March 4, 2015

Gulp

What is Gulp?

  • Gulp is advertised as streaming build system that does the job of Minification, Compilation, Watcher of File Changes etc and can be compared to grunt.

What are Environment Prerequistes?

  • Gulp should be installed Globally using the command npm install gulp -g that we can access the local Glup Files using the Command Line  Commands.
  • The Project Directory we are working should have Gulp Configuration File and Gulp installed within [whihc can be done by using the command npm install gulp].

How Gulp Works overall?

    We execute the Project running on Gulp using the command gulp. This would invoke the globally installed gulp and make it to search for the Gulp Configuration File [gulp.js] located locally within Project directory.

   Gulp.js then loads the locally installed gulp to achieve the Gulp tasks configured within.


Sample Gulp File to Live Reload the html files and also to host the files in the Server by invoking the index.html file as the StartUp

  1. var gulp = require('gulp');
  2. var express = require('express');
  3. var lr = require('tiny-lr')();

  4. gulp.task('serve',function(){
  5.   var app = express();
  6.   app.use(require('connect-livereload')());
  7.   app.use(express.static(__dirname));
  8.   app.listen(4000);
  9. });

  10. gulp.task('liveReload',function(){
  11. lr.listen(35729);
  12. });

  13. gulp.task('default',['serve','liveReload'], function () {
  14.   gulp.watch('*',notifyLivereload);
  15.   gulp.watch('*.js',notifyLivereload);
  16. });


  17. // Notifies livereload of changes detected
  18. // by `gulp.watch()`
  19. function notifyLivereload(event) {

  20.   // `gulp.watch()` events provide an absolute path
  21.   // so we need to make it relative to the server root
  22.   var fileName = require('path').relative(__dirname, event.path);

  23.   lr.changed({
  24.     body: {
  25.       files: [fileName]
  26.     }
  27.   });
  28. }







No comments:

Post a Comment