Gulp First

very first gulp example

1. check node and npm

At first you have install node and npm.

1
2
npm -v (check out npm version)
node -v (check out node version)

2. install build tool

Install gulp and gulp-uglify to your dependencies at package.json
use manager user to do this is better.

1
2
sudo npm install --save-dev gulp
sudo npm install --save-dev gulp-uglify

3. write code

Create a gulpfile.js at the root of your project:

1
2
3
4
5
6
7
8
9
10
* gulpfile.js

var gulp = require('gulp');
var uglify = require('gulp-uglify');

gulp.task('default', function() {
gulp.src('example.js')
.pipe(uglify())
.pipe(gulp.dest('dist'))
});

4. create a example.js file for test.

1
cp gulpfile.js example.js

5. Run gulp

1
gulp (Beacuse method name set as default, so you can run gulp not with method name)

6. Result

You will found that a dist directory had been created, and under dist, these has a file named example.js, this is the Compressed file.Like this

1
var gulp=require("gulp"),uglify=require("gulp-uglify");gulp.task("uglify",function(){gulp.src("example.js").pipe(uglify()).pipe(gulp.dest("dist"))});

  • This is a good result that usually seen at big website’s online js file. :)
    oceanpad.github.io
    oceanpad.github.io