码迷,mamicode.com
首页 > 其他好文 > 详细

Gulp(一)

时间:2016-05-11 19:34:10      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

一.简介

gulp 是基于 Nodejs 的自动任务运行器,能自动化地完成javascript/coffee/sass/less/html/image/css等文件的的测试、检查、合并、压缩、格式化、浏览器自动刷新、部署文件生成,并监听文件在改动后重复指定的这些步骤。

在实现上, gulp 借鉴了Unix操作系统的管道(pipe)思想,前一级的输出,直接变成后一级的输入,使得在操作上非常简单.

技术分享

 

二.配置文件

var gulp = require(‘gulp‘);
var del = require(‘del‘);//删除文件/文件夹
var rev = require(‘gulp-rev‘);//对文件名加MD5后缀
var nano = require(‘gulp-cssnano‘);//删除空白和注释,并且压缩代码
var uglify = require(‘gulp-uglify‘);//通过UglifyJS来压缩JS文件
var useref = require(‘gulp-useref‘);
var imagemin = require(‘gulp-imagemin‘);//压缩图片文件(包括PNG、JPEG、GIF和SVG图片)
var revCollector = require(‘gulp-rev-collector‘);//路径替换
var browserSync = require(‘browser-sync‘).create();//静态文件服务器,同时也支持浏览器自动刷新
var gulpSequence = require(‘gulp-sequence‘);
var uncss = require(‘gulp-uncss‘);
var htmlmin = require(‘gulp-htmlmin‘);
var base64 = require(‘gulp-base64‘);
var changed = require(‘gulp-changed‘);
var postcss = require("gulp-postcss");//css预编译器
var sprites = require(‘postcss-sprites‘).default;//CSS精灵
var autoprefixer = require(‘autoprefixer‘);//自动补全浏览器兼容性前缀
var cssgrace = require(‘cssgrace‘);//CSS后处理工具,hackIE

var SRC_DIR = ‘./src/‘;
var PKG_DIR = ‘./tmp/pkg/‘;
var REV_DIR = ‘./tmp/rev/‘;
var DST_DIR = ‘./dist/‘;
var UNCSS_REG = [/.advise/, /.block/, /.g-bd .m-view-2 .category li:nth-child/, /^.g-bd ul li .info/, /.hljs/];

gulp.task(‘clean‘, function() {
    return del([‘dist‘, ‘tmp‘]);
});

/*
 * 合并请求
 * <!-- build:css ../css/index.pkg.css --><!-- endbuild -->
 * <!-- build:js ../js/index.pkg.js --><!-- endbuild -->
 */
gulp.task(‘pkg‘, function() {
    return gulp.src(SRC_DIR + ‘view/*.html‘)
        .pipe(useref())
        .pipe(gulp.dest(PKG_DIR + ‘view/‘));
});

/*
 * 移动非jpg/png资源到img文件夹
 * 
 */
gulp.task(‘move-img-other‘, function() {
    return gulp.src([SRC_DIR + ‘img/**/*.*!(jpg|png)‘, SRC_DIR + ‘component/img/**/*.*!(jpg|png)‘])
        .pipe(gulp.dest(‘./tmp/pkg/img/‘));
});

/*
 * 压缩IMG
 * 
 */
gulp.task(‘min-img‘, function() {
    return gulp.src([SRC_DIR + ‘img/**/*.*(jpg|png)‘, SRC_DIR + ‘component/img/**/*.*(jpg|png)‘])
        .pipe(imagemin())
        .pipe(gulp.dest(‘./tmp/pkg/img/‘));
});

/*
 * 压缩CSS(PC)
 * 
 */
gulp.task("min-css-pc", function() {
    // PostCSS
    var processors = [
        sprites({
            ‘stylesheetPath‘: PKG_DIR + ‘css/‘,
            ‘spritePath‘: PKG_DIR + ‘img/‘
        }),
        autoprefixer({
            browsers: [‘> 1%‘, ‘last 2 versions‘, ‘ie 6-11‘]
        }),
        cssgrace
    ];
    return gulp.src([PKG_DIR + ‘css/**/*.css‘])
        .pipe(nano({
            discardComments: {
                removeAll: true
            }
        }))
        .pipe(postcss(processors))
        .pipe(gulp.dest(PKG_DIR + ‘css/‘))
});

/*
 * 压缩CSS(Mobile)
 * 
 */
gulp.task("min-css-mobile", function() {
    // PostCSS
    var processors = [
        autoprefixer({
            browsers: [‘> 1%‘, ‘last 2 versions‘]
        })
    ];
    return gulp.src([PKG_DIR + ‘css/**/*.css‘])
        .pipe(uncss({
            html: [PKG_DIR + ‘**/*.html‘],
            ignore: UNCSS_REG
        })).pipe(nano({
            discardComments: {
                removeAll: true
            }
        }))
        .pipe(postcss(processors))
        .pipe(gulp.dest(PKG_DIR + ‘css/‘))
});

/*
 * 压缩JS
 * 
 */
gulp.task(‘min-js‘, function() {
    return gulp.src(PKG_DIR + ‘js/**/*.js‘)
        .pipe(uglify())
        .pipe(gulp.dest(PKG_DIR + ‘js/‘));
});

/*
 * 版本化IMG
 * 
 */
gulp.task(‘rev-img‘, function() {
    return gulp.src(PKG_DIR + ‘img/**/*‘)
        .pipe(rev())
        .pipe(gulp.dest(REV_DIR + ‘img/‘))
        .pipe(rev.manifest())
        .pipe(gulp.dest(REV_DIR + ‘img/‘));
});

/*
 * 版本化CSS
 * 
 */
gulp.task(‘rev-css‘, function() {
    return gulp.src(PKG_DIR + ‘css/**/*‘)
        .pipe(rev())
        .pipe(gulp.dest(REV_DIR + ‘css/‘))
        .pipe(rev.manifest())
        .pipe(gulp.dest(REV_DIR + ‘css/‘));
});

/*
 * 版本化JS
 * 
 */
gulp.task(‘rev-js‘, function() {
    return gulp.src(PKG_DIR + ‘js/**/*‘)
        .pipe(rev())
        .pipe(gulp.dest(REV_DIR + ‘js/‘))
        .pipe(rev.manifest())
        .pipe(gulp.dest(REV_DIR + ‘js/‘));
});

/*
 * 收集图片到CSS
 */
gulp.task(‘col-css‘, function() {
    return gulp.src([REV_DIR + ‘img/*.json‘, REV_DIR + ‘css/*.css‘])
        .pipe(revCollector())
        .pipe(gulp.dest(DST_DIR + ‘css/‘));
});

/*
 * 移动IMG文件到目标文件夹
 */
gulp.task(‘col-img‘, function() {
    return gulp.src([REV_DIR + ‘img/*‘, ‘!‘ + REV_DIR + ‘/img/*.json‘])
        .pipe(gulp.dest(DST_DIR + ‘img/‘));
});

/*
 * 移动JS文件到目标文件夹
 */
gulp.task(‘col-js‘, function() {
    return gulp.src(REV_DIR + ‘js/*.js‘)
        .pipe(gulp.dest(DST_DIR + ‘js/‘));
});

/*
 * 收集资源到HTML
 */
gulp.task(‘col-html‘, function() {
    return gulp.src([REV_DIR + ‘**/*.json‘, PKG_DIR + ‘view/*.html‘])
        .pipe(revCollector())
        .pipe(htmlmin({
            // collapseWhitespace: true,
            removeComments: true
        }))
        .pipe(gulp.dest(DST_DIR + ‘view/‘));
});

/*
 * 图片base64
 */
gulp.task(‘base64‘, function() {
    return gulp.src(PKG_DIR + ‘/**/*.css‘)
        .pipe(base64({
            extensions: [‘svg‘, ‘png‘, /\.jpg#datauri$/i],
        }))
        .pipe(gulp.dest(PKG_DIR));
});


/*
 * 移动mock文件夹
 */
gulp.task(‘move-mock‘, function() {
    return gulp.src(SRC_DIR + ‘mock/**/*‘)
        .pipe(gulp.dest(DST_DIR + ‘mock/‘));
});

/*
 * 静态服务器
 */
gulp.task(‘bs‘, function() {
    browserSync.init({
        files: "**", //监听整个项目
        open: "external",
        server: {
            baseDir: "./dist/",
            index: ‘view/index.html‘
        }
    });
});

/*
 * 静态服务器(代理)
 */
gulp.task(‘bsp‘, function() {
    browserSync.init({
        proxy: "127.0.0.1"
    });
});

/*
 * PC打包方案
 */
gulp.task(‘pc‘, gulpSequence(
    ‘clean‘, ‘pkg‘, ‘min-img‘, 
    [‘min-img‘, ‘min-css-pc‘, ‘min-js‘], ‘move-img-other‘, 
    [‘rev-img‘, ‘rev-css‘, ‘rev-js‘], 
    [‘col-img‘, ‘col-css‘, ‘col-js‘, ‘col-html‘],
    ‘move-mock‘, ‘bs‘
));

/*
 * Mobile打包方案
 */
gulp.task(‘mobile‘, gulpSequence(
    ‘clean‘, ‘pkg‘, ‘min-img‘, ‘base64‘, ‘move-img-other‘,
    [‘min-img‘, ‘min-css-mobile‘, ‘min-js‘], 
    [‘rev-img‘, ‘rev-css‘, ‘rev-js‘], 
    [‘col-img‘, ‘col-css‘, ‘col-js‘, ‘col-html‘],
    ‘move-mock‘, ‘bs‘
));


gulp.task(‘default‘, [‘pc‘], function() {
    return del([‘tmp/‘]);
});

Gulp(一)

标签:

原文地址:http://www.cnblogs.com/winyou/p/5483068.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!