gulp构建 原 发表于 2017-04-20 | 分类于 前端 , 构建打包 , gulp | 浏览 次 入门指南1. 全局安装 gulp:1$ npm install --global gulp 2. 作为项目的开发依赖(devDependencies)安装:1$ npm install --save-dev gulp 3. 在项目根目录下创建一个名为 gulpfile.js 的文件:12345var gulp = require('gulp');gulp.task('default', function() { // 将你的默认的任务代码放在这}); 4. 运行 gulp:1$ gulp 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116var gulp = require('gulp');var browserSync = require('browser-sync');// 引入组件var jshint = require('gulp-jshint');var sass = require('gulp-sass');// 编译Sassvar concat = require('gulp-concat');var uglify = require('gulp-uglify');var rename = require('gulp-rename');var connect = require('gulp-connect'); //自动刷新服务var rev = require('gulp-rev');//加MD5后缀var revReplace = require('gulp-rev-replace');//替换引用的加了md5后缀的文件名,修改过,用来加cdn前缀var minifyHtml = require('gulp-minify-html'); //压缩htmlvar minifyCss = require('gulp-minify-css');var gulpif = require('gulp-if');var cssver = require('gulp-make-css-url-version'); //css urlvar imagemin = require('gulp-imagemin');var pngquant = require('imagemin-pngquant'); //png图片压缩插件var spritesmith = require('gulp-spritesmith');var spriter=require('gulp-css-spriter');var condition = true;var paths = { scripts: './app/js/*.js',//js存放 scss:'./app/css/scss/*.scss',//scss存放 css:'./app/css/*.css',//看守的css目录 cssScss:'./app/css',//压缩sass存放的css目录 minCss:'./app/css/minCss',//压缩的minicss rootJs:'./dist/js', //打包后js rootCss:'./dist/css',//打包后css minHtml:'./dist',//打包后html};// 检查脚本gulp.task('lint', function() { return gulp.src(paths.scripts) .pipe(jshint()) .pipe(jshint.reporter('default'));});// 编译Sassgulp.task('sass', function() { gulp.src(paths.scss) .pipe(sass()) .pipe(gulp.dest(paths.cssScss));});// 合并,压缩文件gulp.task('scripts', function() { gulp.src(paths.scripts) .pipe(concat('app.js')) .pipe(gulp.dest(paths.rootJs)) .pipe(rename('app.min.js')) .pipe(uglify()) .pipe(gulp.dest(paths.rootJs));});//浏览器监听刷新gulp.task('reload',function(){ gulp.src('./*.html').pipe(connect.reload());});//服务器自动刷新gulp.task('connect',function(){ connect.server({ port:3000, livereload:true })})//压缩Html/更新引入文件版本gulp.task('miniHtml', function () { return gulp.src('./*.html') //.pipe(revCollector()) .pipe(gulpif( condition, minifyHtml({ empty: true, spare: true, quotes: true }) )) .pipe(gulp.dest(paths.minHtml));});//压缩/合并CSSgulp.task('miniCss', function () { gulp.src(paths.css) .pipe(minifyCss({ advanced: false,//类型:Boolean 默认:true [是否开启高级优化(合并选择器等)] compatibility: 'ie7',//保留ie7及以下兼容写法 类型:String 默认:''or'*' [启用兼容模式; 'ie7':IE7兼容模式,'ie8':IE8兼容模式,'*':IE9+兼容模式] keepBreaks: true,//类型:Boolean 默认:false [是否保留换行] keepSpecialComments: '*' //保留所有特殊前缀 当你用autoprefixer生成的浏览器前缀,如果不加这个参数,有可能将会删除你的部分前缀 })) .pipe(cssver()) .pipe(gulp.dest(paths.minCss));});gulp.task('minImage', function () { return gulp.src('./app/images/*') .pipe(imagemin({ progressive: true, use: [pngquant()] //使用pngquant来压缩png图片 })) .pipe(gulp.dest('./dist/images'));});gulp.task('sprite',function(){ var timestamp =+ new Date(); //需要自动合并雪碧图的样式文件 return gulp.src('./app/css/*.css') .pipe(spriter({ //生成的spriter的位置 'spriteSheet':'./dist/images/sprite/sprite'+timestamp+'.png', //生成样式文件图片引用地址的路径 //如下将生产:backgound:url(../images/sprite20324232.png) 'pathToSpriteSheetFromCSS':'../images/sprite'+timestamp+'.png' })) .pipe(minifyCss()) //产出路径 .pipe(gulp.dest('./dist/css/sprite'));});//监听文件状态gulp.task('watch', function() { gulp.watch("./*.html",['reload']);});gulp.task('default',['watch','scripts','sass','lint','connect','miniHtml','miniCss','minImage','sprite']);