配置一套基于gulp
的工作流,满足日常的基本开发。
一、目录结构
一套基于gulp的工作流
二、 package.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 { "name" : "gulp-project" , "scripts" : { "dev" : "gulp dev" , "build" : "gulp build" , "upload" : "gulp upload" , "zip" : "gulp zip" }, "devDependencies" : { "babel-preset-es2015" : "^6.22.0" , "browser-sync" : "^2.15.0" , "gulp" : "^3.9.1" , "gulp-autoprefixer" : "^3.1.1" , "gulp-babel" : "^6.1.2" , "gulp-clean" : "^0.3.2" , "gulp-clean-css" : "^2.0.12" , "gulp-concat" : "^2.6.0" , "gulp-ftp" : "^1.1.0" , "gulp-git" : "^1.11.3" , "gulp-htmlmin" : "^2.0.0" , "gulp-imagemin" : "^3.0.3" , "gulp-rename" : "^1.2.2" , "gulp-rev-append" : "^0.1.6" , "gulp-sass" : "^2.3.2" , "gulp-uglify" : "^2.0.0" , "gulp-util" : "^3.0.7" , "gulp-zip" : "^3.2.0" } }
三、 gulpfile.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 let gulp = require ('gulp' ), browserSync = require ('browser-sync' ).create(), reload = browserSync.reload, ftp = require ('gulp-ftp' ), gutil = require ('gulp-util' ), sass = require ('gulp-sass' ), cleancss = require ('gulp-clean-css' ), autoprefixer = require ("gulp-autoprefixer" ), uglify = require ('gulp-uglify' ), concat = require ('gulp-concat' ), rename = require ('gulp-rename' ), clean = require ('gulp-clean' ), imagemin = require ('gulp-imagemin' ), zip = require ('gulp-zip' ), rev = require ('gulp-rev-append' ), htmlmin = require ('gulp-htmlmin' ), git = require ('gulp-git' ), babel = require ("gulp-babel" );
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 gulp.task('sass:dev' , () => { gulp.src('src/sass/*.scss' ) .pipe(sass()) .pipe(gulp.dest('src/css/' )) .pipe(reload({ stream : true })) }); gulp.task('css:dev' , ['sass:dev' ], () => { gulp.src(['src/css/*.css' , '!src/css/areaMap.css' ]) .pipe(concat('all.css' )) .pipe(gulp.dest('dist/css/' )) }); gulp.task('js:dev' , () => { gulp.src('src/js/*.js' ) .pipe(babel({ presets: ['es2015' ] })) .pipe(gulp.dest('dist/js/' )) .pipe(reload({ stream : true })) }); gulp.task('html:dev' , () => { gulp.src('src/tpl/*.html' ) .pipe(gulp.dest('dist' )) }); gulp.task('lib:dev' , () => { gulp.src('./src/lib/*' ) .pipe(gulp.dest('./dist/lib/' )); }); gulp.task('dev' , ['css:dev' , 'js:dev' , 'html:dev' , 'img' ,'copyFonts' ,'lib' ], () => { browserSync.init({ server: { baseDir: "dist" }, notify: false }); gulp.watch('src/js/*.js' , ['js:dev' ]); gulp.watch('src/sass/*.scss' , ['sass:dev' ]); gulp.watch('src/tpl/*.html' , ['html:dev' ]); gulp.watch('src/fonts/**' , ['copyFonts' ]); gulp.watch('src/images/**' , ['img' ]); gulp.watch('src/lib/**' , ['lib' ]); });
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 gulp.task('sass' , () => { gulp.src('src/sass/*.scss' ) .pipe(sass({ outputStyle: 'compressed' })) .pipe(sass()) .pipe(gulp.dest('src/css/' )) }); gulp.task('css' , ['sass' ], () => { gulp.src(['src/css/*.css' , '!src/css/areaMap.css' ]) .pipe(concat('all.css' )) .pipe(autoprefixer({ browsers: ['last 2 versions' , 'Android >= 4.0' ], cascade: true , remove: false })) .pipe(cleancss()) .pipe(gulp.dest('dist/css' )); }); gulp.task('js' , () => { gulp.src('src/js/*.js' ) .pipe(babel({ presets: ['es2015' ] })) .pipe(concat('all.js' )) .pipe(uglify()) .pipe(gulp.dest('dist/js' )); }); gulp.task('html' , () => { gulp.src('src/tpl/*.html' ) .pipe(rev()) .pipe(htmlmin({ removeComments: true , collapseWhitespace: true , collapseBooleanAttributes: true , removeEmptyAttributes: true , removeScriptTypeAttributes: true , removeStyleLinkTypeAttributes: true , minifyJS: true , minifyCSS: true })) .pipe(gulp.dest('dist' )) }); gulp.task('img' , () => { gulp.src('src/images/*.{png,jpg,gif,ico}' ) .pipe(imagemin({ optimizationLevel: 5 , progressive: true , interlaced: true , multipass: true })) .pipe(gulp.dest('dist/images' )); }); gulp.task('copyFonts' , function ( ) { gulp.src('src/fonts/**/*' ) .pipe(gulp.dest('dist/fonts/' )) }); gulp.task('clean' , () => { gulp.src('dist' , { read : false }) .pipe(clean({ force : true })); }); gulp.task('lib' , () => { gulp.src('/src/lib/*' ) .pipe(gulp.dest('/dist/lib/' )); });
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // ================ 打包主体dist 文件夹 zip ==== //打包主体dist 文件夹并按照时间重命名 gulp.task('zip', function(){ function checkTime(i) { if (i < 10) { i = "0" + i } return i } var d=new Date(); var year=d.getFullYear(); var month=checkTime(d.getMonth() + 1); var day=checkTime(d.getDate()); var hour=checkTime(d.getHours()); var minute=checkTime(d.getMinutes()); return gulp.src('./dist/**') .pipe(zip(year+month+day +hour+minute+'.zip')) .pipe(gulp.dest('./zip')); });
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 gulp.task('build' , ['clean' ,'css' , 'js' , 'img' , 'html' ,'copyFonts' ,'lib' ]); gulp.task('upload' , () => { gulp.src('dist/**' ) .pipe(ftp({ host: '8.8.8.8' , port: 22 , user: 'username' , pass: 'password' , remotePath: '/project' })) .pipe(gutil.noop()) })