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
| var gulp = require('gulp'); var gutil = require('gulp-util'); var clean = require('gulp-clean'); var debug = require('gulp-debug'); var cache = require('gulp-cache'); var babel = require('gulp-babel'); var uglify = require('gulp-uglify'); var changed = require('gulp-changed'); var htmlmin = require('gulp-htmlmin'); var imagemin = require('gulp-imagemin'); var htmlclean = require('gulp-htmlclean'); var minifycss = require('gulp-minify-css'); var pngquant = require('imagemin-pngquant');
gulp.task('generate-minify-css', function() { return gulp.src('./public/css/**/*.css') .pipe(changed('/tmp/blog/css/', {extension:'.css', hasChanged: changed.compareContents})) .pipe(minifycss()) .pipe(gulp.dest('/tmp/blog/css/')) .pipe(debug({title: '压缩css源文件:'})); });
gulp.task('copy-minify-css', ['generate-minify-css'], function() { return gulp.src('/tmp/blog/css/**/*.css') .pipe(changed('./public/css/', {extension:'.css', hasChanged: changed.compareContents})) .pipe(gulp.dest('./public/css/')) .pipe(debug({title: '拷贝经压缩的css源文件:'})); });
gulp.task('generate-minify-js', function() { return gulp.src('./public/lib/**/*.js') .pipe(changed('/tmp/blog/lib/', {extension:'.js', hasChanged: changed.compareContents})) .pipe(babel({presets: ['es2015']})) .pipe(uglify()) .pipe(gulp.dest('/tmp/blog/lib/')) .pipe(debug({title: '压缩js源文件:'})); });
gulp.task('copy-minify-js', ['generate-minify-js'], function() { return gulp.src('/tmp/blog/lib/**/*.js') .pipe(changed('./public/lib/', {extension:'.js', hasChanged: changed.compareContents})) .pipe(gulp.dest('./public/lib/')) .pipe(debug({title: '拷贝经压缩的js源文件:'})); });
gulp.task('generate-minify-html', function() { return gulp.src('./public/**/*.html') .pipe(changed('/tmp/blog/', {extension:'.html', hasChanged: changed.compareContents})) .pipe(htmlclean()) .pipe(htmlmin({ removeComments: true, minifyJS: true, minifyCSS: true, minifyURLs: true, })) .pipe(gulp.dest('/tmp/blog/')) .pipe(debug({title: '压缩html源文件:'})); });
gulp.task('copy-minify-html', ['generate-minify-html'], function() { return gulp.src('/tmp/blog/**/*.html') .pipe(changed('./public/', {extension:'.html', hasChanged: changed.compareContents})) .pipe(gulp.dest('./public/')) .pipe(debug({title: '拷贝经压缩的html源文件:'})); });
gulp.task('minify-images', function() { gulp.src('./public/asset/**/*.{png,jpg,gif,ico}') .pipe(cache(imagemin({ progressive: true, interlaced: false, svgoPlugins: [{removeViewBox: false}], multipass: false, optimizationLevel: 5, use: [pngquant()] }))) .pipe(gulp.dest('./public/asset')) .pipe(debug({title: '压缩图片:'})); });
gulp.task('minify-js', ['generate-minify-js', 'copy-minify-js']); gulp.task('minify-css', ['generate-minify-css', 'copy-minify-css']); gulp.task('minify-html', ['generate-minify-html', 'copy-minify-html']);
|