~~~~(>_<)~~~~前几天写了一个gulp总结,偶然之间发现了阮大神写的gulp教程,顿时,感受本身写的就是渣渣啊!因此,今天学习下阮大大的教程,对于上一篇gulp文章深深的自责,侮辱了各位看官的眼,表示深深的自责。css
gulp是一个自动化的任务运行工具,会按照咱们预先设定的顺序自动执行一系列的任务,这样能够提升咱们的工做效率,尤为是在这个以时间换金钱的时代。(*^__^*) 嘻嘻……html
gulp基于node平台,因此首先须要安装node环境,安装完node环境以后,能够执行下面的命令。前端
npm install -g gulp npm install --save-dev gulp
安装gulp以外,还须要安装gulp的插件模块,下面以安装gulp-uglify模块为演示。node
npm install --save-dev gulp-uglify
在项目的根目录下建立gulpfile.js文件。git
var gulp = require('gulp'); var uglify = require('gulp-uglify'); gulp.task('minify', function () { gulp.src('js/app.js') .pipe(uglify()) .pipe(gulp.dest('build')) });
上面代码中,gulpfile.js加载gulp和gulp-uglify模块以后,使用gulp模块的task方法指定任务minify。task方法有两个参数,第一个是任务名,第二个是任务函数。在任务函数中,使用gulp模块的src方法,指定所要处理的文件,而后使用pipe方法,将上一步的输出转为当前的输入,进行链式处理。github
task方法的回调函数使用了两次pipe方法,也就是说作了两种处理。第一种处理是使用gulp-uglify模块,压缩源码;第二种处理是使用gulp模块的dest方法,将上一步的输出写入本地文件,这里是build.js(代码中省略了后缀名js)。shell
执行minify任务时,就在项目目录中执行下面命令就能够了。npm
gulp minify
gulp模块的src方法,用于产生数据流。它的参数表示所要处理的文件,这些指定的文件会转换成数据流。参数的写法通常有如下几种形式。json
src方法的参数还能够是一个数组,用来指定多个成员。gulp
gulp.src(['js/**/*.js', '!js/**/*.min.js'])
dest方法将管道的输出写入文件,同时将这些输出继续输出,因此能够依次调用屡次dest方法,将输出写入多个目录。若是有目录不存在,将会被新建。
gulp.src('./client/templates/*.jade') .pipe(jade()) .pipe(gulp.dest('./build/templates')) .pipe(minify()) .pipe(gulp.dest('./build/minified_templates'));
dest方法还能够接受第二个参数,表示配置对象。
gulp.dest('build', { cwd: './app', mode: '0644' })
配置对象有两个字段。cwd字段指定写入路径的基准目录,默认是当前目录;mode字段指定写入文件的权限,默认是0777。
task方法用于定义具体的任务。它的第一个参数是任务名,第二个参数是任务函数。下面是一个很是简单的任务函数。
gulp.task('greet', function () { console.log('Hello world!'); });
task方法还能够指定按顺序运行的一组任务。
gulp.task('build', ['css', 'js', 'imgs']);
上面代码先指定build任务,它由css、js、imgs三个任务所组成,task方法会并发执行这三个任务。注意,因为每一个任务都是异步调用,因此没有办法保证js任务的开始运行的时间,正是css任务运行结束。
若是但愿各个任务严格按次序运行,能够把前一个任务写成后一个任务的依赖模块。
gulp.task('css', ['greet'], function () { // Deal with CSS here });
上面代码代表,css任务依赖greet任务,因此css必定会在greet运行完成后再运行。
task方法的回调函数,还能够接受一个函数做为参数,这对执行异步任务很是有用。
// 执行shell命令 var exec = require('child_process').exec; gulp.task('jekyll', function(cb) { // build Jekyll exec('jekyll build', function(err) { if (err) return cb(err); // return error cb(); // finished task }); });
若是一个任务的名字为default,就代表它是“默认任务”,在命令行直接输入gulp命令,就会运行该任务。
gulp.task('default', function () { // Your default task }); // 或者 gulp.task('default', ['styles', 'jshint', 'watch']);
执行的时候,直接使用gulp,就会运行styles、jshint、watch三个任务。
watch方法用于指定须要监视的文件。一旦这些文件发生变更,就运行指定任务。
gulp.task('watch', function () { gulp.watch('templates/*.tmpl.html', ['build']); });
上面代码指定,一旦templates目录中的模板文件发生变化,就运行build任务。
watch方法也能够用回调函数,代替指定的任务。
gulp.watch('templates/*.tmpl.html', function (event) { console.log('Event type: ' + event.type); console.log('Event path: ' + event.path); });
另外一种写法是watch方法所监控的文件发生变化时(修改、增长、删除文件),会触发change事件。能够对change事件指定回调函数。
var watcher = gulp.watch('templates/*.tmpl.html', ['build']); watcher.on('change', function (event) { console.log('Event type: ' + event.type); console.log('Event path: ' + event.path); });
除了change事件,watch方法还可能触发如下事件。
watcher对象还包含其余一些方法。
通常状况下,gulpfile.js中的模块须要一个个加载。
var gulp = require('gulp'), jshint = require('gulp-jshint'), uglify = require('gulp-uglify'), concat = require('gulp-concat'); gulp.task('js', function () { return gulp.src('js/*.js') .pipe(jshint()) .pipe(jshint.reporter('default')) .pipe(uglify()) .pipe(concat('app.js')) .pipe(gulp.dest('build')); });
上面代码中,除了gulp模块之外,还加载另外三个模块。
这种一一加载的写法,比较麻烦。使用gulp-load-plugins模块,能够加载package.json文件中全部的gulp模块。上面的代码用gulp-load-plugins模块改写,就是下面这样。
var gulp = require('gulp'), gulpLoadPlugins = require('gulp-load-plugins'), plugins = gulpLoadPlugins(); gulp.task('js', function () { return gulp.src('js/*.js') .pipe(plugins.jshint()) .pipe(plugins.jshint.reporter('default')) .pipe(plugins.uglify()) .pipe(plugins.concat('app.js')) .pipe(gulp.dest('build')); });
上面代码假设package.json文件包含如下内容。
{ "devDependencies": { "gulp-concat": "~2.2.0", "gulp-uglify": "~0.2.1", "gulp-jshint": "~1.5.1", "gulp": "~3.5.6" } }
gulp-livereload模块用于自动刷新浏览器,反映出源码的最新变化。它除了模块之外,还须要在浏览器中安装插件,用来配合源码变化。
var gulp = require('gulp'), less = require('gulp-less'), livereload = require('gulp-livereload'), watch = require('gulp-watch'); gulp.task('less', function() { gulp.src('less/*.less') .pipe(watch()) .pipe(less()) .pipe(gulp.dest('css')) .pipe(livereload()); });
上面代码监视less文件,一旦编译完成,就自动刷新浏览器。
最后附上本身前一年前写的一个自动化任务,仅供参考:
// 载入Gulp模块 var gulp = require('gulp'); var less = require('gulp-less'); var autoprefixer = require('gulp-autoprefixer'); var cssnano = require('gulp-cssnano'); var concat = require('gulp-concat'); var uglify = require('gulp-uglify'); var htmlmin = require('gulp-htmlmin'); var browserSync = require('browser-sync'); var reload = browserSync.reload; gulp.task('style', function() { gulp.src('src/styles/*.less') .pipe(less()) .pipe(autoprefixer({ browsers: ['last 2 versions'] })) .pipe(cssnano()) .pipe(gulp.dest('dist/styles')) .pipe(reload({ stream: true })); }); gulp.task('script', function() { gulp.src('src/scripts/*.js') .pipe(concat('app.js')) .pipe(uglify()) .pipe(gulp.dest('dist/scripts')) .pipe(reload({ stream: true })); }); gulp.task('image', function() { gulp.src('src/images/*.*') .pipe(gulp.dest('dist/images')) .pipe(reload({ stream: true })); }) gulp.task('html', function() { gulp.src('src/*.html') .pipe(htmlmin({ collapseWhitespace: true, collapseBooleanAttributes: true, removeAttributeQuotes: true, removeComments: true, removeEmptyAttributes: true, removeScriptTypeAttributes: true, removeStyleLinkTypeAttributes: true, })) .pipe(gulp.dest('dist')) .pipe(reload({ stream: true })); }); gulp.task('serve', ['style', 'script', 'image', 'html'], function() { browserSync({ notify: false, port: 2015, server: { baseDir: ['dist'] } }); gulp.watch('src/styles/*.less', ['style']); gulp.watch('src/scripts/*.js', ['script']); gulp.watch('src/images/*.*', ['image']); gulp.watch('src/*.html', ['html']); });
在github上面的地址:风流倜傥小小张