It is the second part of the gulp for beginners series which deals in introducing gulp.js and then making you familiar with some basic gulp plugins. This article talks about 3 gulp plugins which are :
This plugin is used to remove the newlines and comments from the CSS/JS files or in other words it is used to minify the files.
var gulp = require('gulp');
var uglify = require('gulp-uglify');
gulp.task('ugly' , function() {
gulp.src('source/*.js')
.pipe(uglify())
.pipe(gulp.dest('destination'));
});
Run it at bash or command line using the following command
$ gulp ugly
this plugin is used to concatenate 2 or more files into a single output file.
var gulp = require('gulp');
var concat = require('gulp-concat');
gulp.task('scripts' , function() {
return gulp.src('source/*.js') //add your custom source here
.pipe(concat('min.js')) // name of the output file
.pipe(gulp.dest('destination4')); // name of the destination folder comes here
});
Run it at bash or command line using the following command
$ gulp scripts
This gulp plugin is used to rename a file. There are 3 ways of renaming a file
a) Rename via string
b) Rename via function
c) Rename via hash
Lets understand them one by one
//rename via string
var gulp = require('gulp');
var rename = require("gulp-rename");
gulp.task('default', function() {
gulp.src("maps/main.css")
.pipe(rename("css/gulp.min.css"))
.pipe(gulp.dest("./destination")); // /destination/css/gulp.min.css
});
Run it at bash or command line using the following command
$ gulp
//rename via function
var gulp = require('gulp');
var rename = require("gulp-rename");
gulp.task('default', function() {
gulp.src("maps/main.css")
.pipe(rename(function (path) {
path.dirname += "/css";
path.basename = "gulp.min";
path.extname = ".css"
}))
.pipe(gulp.dest("./dist")); // ./dist/css/gulp.min.css
});
Run it at bash or command line using the following command
$ gulp
//rename via hash
var gulp = require('gulp');
var rename = require("gulp-rename");
gulp.task('default', function() {
gulp.src("maps/main.css" , { base: process.cwd() })
.pipe(rename({
dirname: "main/css",
basename: "gulp",
prefix: "rename-",
suffix: "-min",
extname: ".js"
}))
.pipe(gulp.dest("./dist")); // ./dist/main/css/rename-gulp-min.js
});
Run it at bash or command line using the following command
$ gulp