|
| 1 | +const gulp = require('gulp'); |
| 2 | +const less = require('gulp-less'); |
| 3 | +const px2rem = require('gulp-px3rem'); |
| 4 | +const rename = require('gulp-rename'); |
| 5 | +const connect = require('gulp-connect'); |
| 6 | +const cleanCSS = require('gulp-clean-css'); |
| 7 | +const plumber = require('gulp-plumber'); //编译报错,不会中断程序 |
| 8 | + |
| 9 | +const postcss = require('gulp-postcss'); |
| 10 | +const autoprefixer = require('autoprefixer'); |
| 11 | + |
| 12 | +var sourceFiles = [ 'public/images/*','public/images/*/*', 'public/mp3/*', 'public/index.html' ]; |
| 13 | + |
| 14 | + |
| 15 | +//打包需要的模块 |
| 16 | +const uglifyJs = require('gulp-uglify'); |
| 17 | +const cssmin = require('gulp-cssmin'); |
| 18 | +const gulpCopy = require('gulp-copy'); |
| 19 | +const through = require('through2'); |
| 20 | + |
| 21 | +gulp.task('less', function () { |
| 22 | + gulp.src('public/less/*.less') |
| 23 | + .pipe(plumber()) |
| 24 | + .pipe(less()) |
| 25 | + .pipe(px2rem({ |
| 26 | + remUnit: 40, |
| 27 | + remPrecision: 6 |
| 28 | + })) |
| 29 | + .pipe(rename(function (path) { |
| 30 | + var s = path.basename.replace('.debug', ''); |
| 31 | + |
| 32 | + path.basename = s; |
| 33 | + })) |
| 34 | + .pipe(postcss([ autoprefixer({ browsers: ['last 2 versions'] }) ])) |
| 35 | + //.pipe(cleanCSS({compatibility: 'ie8'})) //最后压缩,打开重新运行 gulp 即可 |
| 36 | + .pipe(gulp.dest('public/css')) |
| 37 | + .pipe(connect.reload()); |
| 38 | +}); |
| 39 | + |
| 40 | +gulp.task('html',function(){ |
| 41 | + gulp.src('public/*.html') |
| 42 | + .pipe(connect.reload()) |
| 43 | +}); |
| 44 | + |
| 45 | +gulp.task('connect', function () { |
| 46 | + connect.server({ |
| 47 | + root: 'public', |
| 48 | + port: '80', |
| 49 | + livereload: true |
| 50 | + }) |
| 51 | +}); |
| 52 | + |
| 53 | +gulp.task('watch', function () { |
| 54 | + gulp.watch('public/less/*.less', ['less']); |
| 55 | + gulp.watch('public/*.html',['html']); |
| 56 | +}); |
| 57 | + |
| 58 | +//构建 压缩js,css,html,合并 |
| 59 | +function toDou(n){ |
| 60 | + return n<10?'0'+n:''+n; |
| 61 | +} |
| 62 | +let oDate = new Date(); |
| 63 | +const distDir = 'dist'+oDate.getFullYear()+toDou(oDate.getMonth()+1)+toDou(oDate.getDate()); |
| 64 | + |
| 65 | +gulp.task('uglifyJs',function(){ |
| 66 | + return gulp.src('public/js/*.js') |
| 67 | + .pipe(uglifyJs()) |
| 68 | + .pipe(gulp.dest('public/'+distDir+'/js')); |
| 69 | +}); |
| 70 | +gulp.task('cssmin',function(){ |
| 71 | + return gulp.src('public/css/*.css') |
| 72 | + .pipe(cssmin()) |
| 73 | + .pipe(gulp.dest('public/'+distDir+'/css')) |
| 74 | +}); |
| 75 | +gulp.task('copy', function(){ |
| 76 | + return gulp |
| 77 | + .src(sourceFiles) |
| 78 | + .pipe(gulpCopy('public/'+distDir, { prefix: 1 })) |
| 79 | + .pipe(verify()); |
| 80 | +}); |
| 81 | + |
| 82 | +function verify () |
| 83 | +{ |
| 84 | + var options = { objectMode: true }; |
| 85 | + return through(options, write, end); |
| 86 | + |
| 87 | + function write (file, enc, cb) |
| 88 | + { |
| 89 | + console.log('file', file.path); |
| 90 | + cb(null, file); |
| 91 | + } |
| 92 | + |
| 93 | + function end (cb) |
| 94 | + { |
| 95 | + console.log('done'); |
| 96 | + cb(); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +gulp.task('default', ['connect','watch', 'less','html']); |
| 101 | +gulp.task('build',['uglifyJs','cssmin','copy']); |
| 102 | + |
| 103 | + |
0 commit comments