参考文档:https://gulpjs.com/
https://markpop.github.io/2014/09/17/Gulp%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B/
http://www.gulpjs.com.cn/docs/api/
http://www.cnblogs.com/hutuzhu/p/5276000.html
1 var gulp = require('gulp'); 2 var clean = require('gulp-clean'); 3 var sass = require('gulp-sass'); 4 var runSequence = require('run-sequence'); 5 var autoprefixer = require('gulp-autoprefixer'); 6 var concat = require('gulp-concat'); 7 var uglify = require('gulp-uglify'); 8 var pump = require('pump') 9 10 11 var src = { 12 sass: [ 13 'src/sass/**/*.sass' //编译用 14 ], 15 css: [ 16 'src/css/**/*.css' //清除用 17 ] 18 }; 19 20 var build = { 21 css: 'src/css', 22 }; 23 24 25 function log(msg) { 26 util.log(util.colors.blue(msg)); 27 } 28 29 // 处理错误 30 function handleError(err) { 31 log(err.toString()); 32 this.emit('end'); 33 } 34 35 //--------------------------------------- 36 gulp.task('清除', function () { 37 return gulp.src(src.css, { 38 read: false 39 }) 40 .pipe(clean({ 41 force: true 42 })); 43 }); 44 45 46 gulp.task('监听', function () { 47 runSequence('清除', ['编译']); 48 // gulp.watch(src.css, ['监听']); 49 }); 50 51 52 //--------------------------------------- 53 54 55 gulp.task('编译', function () { 56 return gulp.src(src.sass) 57 .pipe(sass().on('error', sass.logError)) 58 .pipe(autoprefixer({ 59 browsers: ['iOS >= 7', 'Android >= 4.1'] 60 })) 61 .pipe(gulp.dest(build.css)) 62 }); 63 64 gulp.task('watch', function () { 65 gulp.watch(src.sass, ['监听']); 66 67 }); 68 69 //++++++++++++++++++++++++++++ 70 71 gulp.task('scripts', function () { 72 return gulp.src(['./src/nJs/one.js', './src/nJs/two.js']) 73 .pipe(concat({path: 'new.js', stat: {mode: 0666}})) 74 .pipe(gulp.dest('./src/outJs')); 75 }); 76 77 78 gulp.task('watch:script', function () { 79 gulp.watch('./src/nJs/**/*.js', ['scripts']); 80 81 }); 82 83 84 //----------------------压缩代码--------------------------- 85 86 gulp.task('compress', function (cb) { 87 pump([ 88 gulp.src('./src/nJs/**/*.js'), 89 uglify(), 90 gulp.dest('./dist') 91 ], 92 cb 93 ); 94 }); 95 96 //------------------------------------------------ 97 98 gulp.task('build-1', function () { 99 runSequence('清除', ['编译']); 100 // gulp.start('build:app', 'build:vendor'); 101 }); 102 103 gulp.task('default', function () { 104 runSequence('build-1','scripts',"compress", ['watch','watch:script']); 105 });