Sunday 15 May 2016

How to setup gulp / Getting started with gulp

Getting Started with gulp


Gulp is a task/build runner for development. It allows you to do a lot of stuff within your development workflow. You can compile sass files, uglify and compress js files and much more. The kicker for gulp is that its a streaming build system which doesn't write temp files. It's like Pipes in bash.


Installing gulp

Before We configure tasks, we need to install gulp globally and as a dev dependency. use following command to install gulp globally

$ npm install gulp -g

after installing gulp globally you can access CLI now we will need to install gulp locally.
cd into project and run following command to install gulp as a dev dependency 

$ npm install gulp —save-dev

this command install gulp locally and add it to package.json file as a devdependencies

Installing Gulp Plugins

after installing the gulp globally and as a dev dependency we need to install gulp plugins which will do a specific task 

Sass compile (gulp-ruby-sass)
Autoprefixer (gulp-autoprefixer)
Minify CSS (gulp-cssnano)
JSHint (gulp-jshint)
Concatenation (gulp-concat)
Uglify (gulp-uglify)
Compress images (gulp-imagemin)
LiveReload (gulp-livereload)
Caching of images so only changed images are compressed (gulp-cache)
Notify of changes (gulp-notify)
Clean files for a clean build (del)
we can install all this plugin using npm

$ npm install gulp-ruby-sass gulp-autoprefixer gulp-cssnano gulp-jshint gulp-concat gulp-uglify gulp-imagemin gulp-notify gulp-rename gulp-livereload gulp-cache del --save-dev

this will install all the gulp plugins and add it to package.son as devedependencies

Load plugins and creating tasks


We need to crate a glupfile.js in project directory and than load all the plugins in gulp file.js. like

var gulp = require('gulp'),
    sass = require('gulp-ruby-sass'),
    autoprefixer = require('gulp-autoprefixer'),
    cssnano = require('gulp-cssnano'),
    jshint = require('gulp-jshint'),
    uglify = require('gulp-uglify'),
    imagemin = require('gulp-imagemin'),
    rename = require('gulp-rename'),
    concat = require('gulp-concat'),
    notify = require('gulp-notify'),
    cache = require('gulp-cache'),
    livereload = require('gulp-livereload'),
    del = require(‘del');

We will use gulp.task API to create tasks.
gulp.task('styles', function() { ... )};
the about task styles can run from terminal using
$ gulp styles


Compile Sass, Autoprefix  and minify

Firstly we will create a task for SASS compiling. this task will sass run it through Autoprefixer and save it to our destination with minified version. when the task is finished this will auto-refresh the page and notify that the task is completed

gulp.task('styles', function() {
  return sass('src/styles/main.scss', { style: 'expanded' })
    .pipe(autoprefixer('last 2 version'))
    .pipe(gulp.dest('dist/assets/css'))
    .pipe(rename({suffix: '.min'}))
    .pipe(cssnano())
    .pipe(gulp.dest('dist/assets/css'))
    .pipe(notify({ message: 'Styles task complete' }));
  });
}); 

   


JSHint, concat, and minify JavaScript

now we will setup a script task to lint, concat and uglify JS files and create a min file

gulp.task('scripts', function() {
  return gulp.src('src/scripts/**/*.js')
    .pipe(jshint('.jshintrc'))
    .pipe(jshint.reporter('default'))
    .pipe(concat('main.js'))
    .pipe(gulp.dest('dist/assets/js'))
    .pipe(rename({suffix: '.min'}))
    .pipe(uglify())
    .pipe(gulp.dest('dist/assets/js'))
    .pipe(notify({ message: 'Scripts task complete' }));
  });
});

Here we use the gulp.src API to specify our input files.

Compress Images

We will create next gulp task to compressing images. this task will take any source image and run them through image min plugin and compress image and save them to targeted folder


Clean UP

Its a good idea to clean out the destination folders and rebuild the files when deploying, so all the files will be upto date. here is the gulp task for cleanup

gulp.task('clean', function() {
    return del(['dist/assets/css', 'dist/assets/js', 'dist/assets/img']);
  });
});


Default Task

We have already defined all the tasks and we can run each task in terminal using gulp task name
but if we want than we can create a default task which can ran by using gulp command

gulp.task('default', ['clean'], function() {
    gulp.start('styles', 'scripts', 'images');
});


the above code clean task will run before the tasks in gulp.start. 

Watch

we use watch to watch files and perform necessary tasks. to watch files we have to create new task with gulp.watch API which will watch files.
gulp.task('watch', function() {

  // Watch .scss files
  gulp.watch('src/styles/**/*.scss', ['styles']);

  // Watch .js files
  gulp.watch('src/scripts/**/*.js', ['scripts']);

  // Watch image files
  gulp.watch('src/images/**/*', ['images']);

});


LiveReload

Live reload will auto-refresh page on file change. we just need to modify our gulp task watch to conferring the live reload server

gulp.task('watch', function() {

  // Create LiveReload server
  livereload.listen();

  // Watch any files in dist/, reload on change
  gulp.watch(['dist/**']).on('change', livereload.changed);

});




at the end we need to put all the above code together 
/*!
 * gulp
 * $ npm install gulp-ruby-sass gulp-autoprefixer gulp-cssnano gulp-jshint gulp-concat gulp-uglify gulp-imagemin gulp-notify gulp-rename gulp-livereload gulp-cache del --save-dev
 */

// Load plugins
var gulp = require('gulp'),
    sass = require('gulp-ruby-sass'),
    autoprefixer = require('gulp-autoprefixer'),
    cssnano = require('gulp-cssnano'),
    jshint = require('gulp-jshint'),
    uglify = require('gulp-uglify'),
    imagemin = require('gulp-imagemin'),
    rename = require('gulp-rename'),
    concat = require('gulp-concat'),
    notify = require('gulp-notify'),
    cache = require('gulp-cache'),
    livereload = require('gulp-livereload'),
    del = require('del');

// Styles
gulp.task('styles', function() {
  return sass('src/styles/main.scss', { style: 'expanded' })
    .pipe(autoprefixer('last 2 version'))
    .pipe(gulp.dest('dist/styles'))
    .pipe(rename({ suffix: '.min' }))
    .pipe(cssnano())
    .pipe(gulp.dest('dist/styles'))
    .pipe(notify({ message: 'Styles task complete' }));
});

// Scripts
gulp.task('scripts', function() {
  return gulp.src('src/scripts/**/*.js')
    .pipe(jshint('.jshintrc'))
    .pipe(jshint.reporter('default'))
    .pipe(concat('main.js'))
    .pipe(gulp.dest('dist/scripts'))
    .pipe(rename({ suffix: '.min' }))
    .pipe(uglify())
    .pipe(gulp.dest('dist/scripts'))
    .pipe(notify({ message: 'Scripts task complete' }));
});

// Images
gulp.task('images', function() {
  return gulp.src('src/images/**/*')
    .pipe(cache(imagemin({ optimizationLevel: 3, progressive: true, interlaced: true })))
    .pipe(gulp.dest('dist/images'))
    .pipe(notify({ message: 'Images task complete' }));
});

// Clean
gulp.task('clean', function() {
  return del(['dist/styles', 'dist/scripts', 'dist/images']);
});

// Default task
gulp.task('default', ['clean'], function() {
  gulp.start('styles', 'scripts', 'images');
});

// Watch
gulp.task('watch', function() {

  // Watch .scss files
  gulp.watch('src/styles/**/*.scss', ['styles']);

  // Watch .js files
  gulp.watch('src/scripts/**/*.js', ['scripts']);

  // Watch image files
  gulp.watch('src/images/**/*', ['images']);

  // Create LiveReload server
  livereload.listen();

  // Watch any files in dist/, reload on change
  gulp.watch(['dist/**']).on('change', livereload.changed);

});



});

Sunday 4 October 2015

How To create arrow using CSS

We can now create arrow using CSS3 Properties. Its very silmple to create arrow using css. Today I am showing you that how can you create arrows using css3 properties.

So first create a basic html page. and add div or and other tag for creating arrow.

HTML


Now you have to write the css which will transform this elements into arrow. For creating arrows we have to define elements height and width to '0px' as we will play with borders and creating arrows.so here is our css

CSS


Now you can see the results here.

RESULT

Thursday 1 October 2015

How to user UI router in Angular js Application

Hi, Guys, Today I am telling you that how to use AngularUI Router
AngularUI Router  is basically used for routing in angularjs application. its work like angular ng-route, but AngularUI Router  has more features and power than ngRoute. If you are building a complex and large scale application than I am suggesting you to use UIRouter in your application for flexible routing and nested view.
AngularUI Router is also supports nested views.

Get Started

For use AngularUI router you need angularUi Router script. There are many methods to download script and you can also use cdn.
  • via npm: by running $ npm install angular-ui-router from your console
  • or via Bower: by running $ bower install angular-ui-router from your console
  • or via cdn link to cdn
Include angular-ui-router.js in your index.html and than  Add 'ui.router' to your main module's list of dependencies 
so your index.html will look like following.


I have added  ui-sref directive to manage state transitions. this directive auto generate href attribute for given state.
I have also used ui-view Directive. in this directive our view will be loaded in the ui-veiw.
Now lets create some child templates which will be loaded according to given state in ui-sref.


Finally We use  $stateProvider to set up our states in the module config, as in the following:


In this code I have passed parameters in state which I have initialized in ui-sref. I can define controller and templates also.
Please let me know if you have any trouble in integrating ui-router.

Now see our code in action here





You can also download this code from here
Angular UI router Example Seed

Wednesday 30 September 2015

How to create single page application using angular js

Hi guys, Angular js a grate framework to build single page application. Angularjs is most popular framework to build single page application.
In this post I am showing you that how to use routing (ng-route) to build a single page app using angular.
For routing we have to include angular-route.js in our angular project. and our html will look like and for rendering the different views of our application we will use ng-view directives in our body section where we want to render the different views.
I have also included 2 more files app.js and controller.js.
In app.js file we will define the app also write the routing code. and in controller.js we will write the controllers.
We can create multiple controllers in a single js files. but If you have lots of functionality than you should create different files for different controllers. I have created a single file for controllers controller.js
I have also created a basic navigation bar for switching the view.



In app.js file I am defining app and also defining routes using ng-route. I have added ng-route dependency in my module. I have to use the config function for configuring the routes. you can see in the code snippet.




  • when('/view1'): The view1 view will be shown when the URL hash fragment is /view1. To construct this view, Angular will use the view1.html template and the view1 controller.
View1.html

  • when('/view2'): The view2 view will be shown when the URL hash fragment matches '/view2', . To construct the phone details view, Angular will use the view.htmltemplate and the view2 controller.
View2.html

  • otherwise({redirectTo: '/'}): triggers a redirection to when the browser address doesn't match either of our routes.
Now you can see it in action. you can switch between views by just clicking on navbar and the view will change. check the example below


Tuesday 29 September 2015

How To Start With Angular JS

There are so many tutorials and videos for start up with angularjs. When I was started with angularjs I found that most of tutorials are too confusing.
So I am showing that How to create a basic app with angularjs.

What We Need For Startup

  1. Text Editor(sublime text, notepad++, dreamweaver)
  2. Browser (chrome, firefox),
  3. angularjs Library. You can download it from angularjs.org
Create a file named index.html and  create a initial html page.and attach  angularjs library in your html page. In this page I am showing you how the ng-model works and how to bind the data.Now you can see the results in the result tab of codepen demo. So for data binding we just need to add an angular library.
In Next Post I will let you know that How to create single page application using angular js.

<html ng-app="">  
 <head>  
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>  
 </head>  
 <body>  
   <p>First Name : <input type="text" ng-model="f_name" placeholder="First Name"></p>  
   <p>Last Name : <input type="text" ng-model="l_name" placeholder="LastName"></p>  
   <h1>Hello {{f_name}} {{l_name}}</h1>  
 </body>  
 </html>  
See the Pen jbyZQL by Love Trivedi (@lovetrivedi) on CodePen.