一个gulp配置文件

var gulp = require('gulp');
var browserSync = require('browser-sync').create();
var imagemin = require('gulp-imagemin');
var uglify = require('gulp-uglify');
var sourcemaps = require('gulp-sourcemaps');
var rev = require('gulp-rev');
var del = require('del');
var revReplace = require('gulp-rev-replace');
var colors = require('colors');
var minifyCss = require('gulp-minify-css');
var htmlmin = require('gulp-htmlmin');

// 开发用
gulp.task('server', function() {
    browserSync.init({
        server: {
            baseDir: 'src/'
        }
    });
    browserSync.watch("src/css/**/*.css", function(event, file) {
        if (event === "change") {
            browserSync.reload("*.css");
        }
    });
    browserSync.watch("src/js/**/*.js", function(event, file) {
        if (event === "change") {
            browserSync.reload("*.js");
        }
    });
    browserSync.watch("src/**/*.html").on("change", browserSync.reload);
});

// 删除上次发布内容
gulp.task('del', function() {
    // 使用同步方法,异步会有问题
    del.sync(['dist/*','rev-manifest.json'], function(err, paths) {
        console.log('delete file/folder:\n', colors.cyan(paths.join('\n')));
    });
});

// js压缩 改名 发布
gulp.task('js', function(cb) {
    return gulp.src('src/**/js/*.js')
        // .pipe(sourcemaps.init({
        //     loadMaps: true
        // }))
        .pipe(uglify({
            output: {
                ascii_only: true
            },
        }))
        .pipe(rev())
        // .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest('dist/'))
        .pipe(rev.manifest({
            base: '',
            merge: true
        }))
        .pipe(gulp.dest(''));
});

// css压缩 改名 发布
gulp.task('css',function(){
    return gulp.src('src/**/*.css')
    .pipe(minifyCss())
    .pipe(rev())
    .pipe(gulp.dest('dist/'))
    .pipe(rev.manifest({
        base: '',
        merge: true
    }))
    .pipe(gulp.dest(''));
});

// 图片压缩及发布
gulp.task('image',function(){
    return gulp.src(['src/**/*.png','src/**/*.jpg','src/**/*.gif'])
    .pipe(imagemin({
        progressive: true,
        interlaced: true
    }))
    .pipe(gulp.dest('dist/'));
});

gulp.task('html',['js','css'], function() {
    var manifest = gulp.src('rev-manifest.json');
    var fixManifest = gulp.src('fix-manifest.json');
    return gulp.src('src/**/*.html')
    .pipe(revReplace({
        replaceInExtensions: ['.html'],
        manifest: manifest
    }))
    // .pipe(htmlmin({collapseWhitespace: true}))
    .pipe(gulp.dest('dist'));
});

gulp.task('other', function() {
    return gulp.src(['src/**/lib/*.js','src/**/*.json'])
    .pipe(gulp.dest('dist/'));
});


// 默认任务即为发布
gulp.task('default',['del','js','css','image','html', 'other']);

你可能感兴趣的:(一个gulp配置文件)