让前端飞Web 前端开发

再学一次gulp自动化构建项目(含有开发环境和生产环境之分)

2018-08-18  本文已影响0人  Fernweh_Nancy

前言

用gulp构建官网的项目,起初用别人配置好的gulpfile来构建,后来发现有很多不人性化的操作,比如说在开发调试中,打印出console,按这个去找源文件的出处,发现代码被压缩了,找的可费劲了,说白就是没分配好开发环境和生产环境的。然后是在生产环境中没清除掉console,作为一枚处女座,忍受不了,于是打算大改造gulpfile.js。如有不足之处,请指出来哈,我将会献上我的膝盖~

分配开发环境和生产环境

const SRC_DIR='./src/';   //源路径,不支持相对路径,下面也是
const DEV_DIR='./dev/';   //生成开发环境的文件
const DIST_DIR='./dist/';  //生成生产环境的文件


const config={
    src:SRC_DIR,
    dist:DIST_DIR,
    dev:DEV_DIR,
    html:{
        src:SRC_DIR+'*.html',
        dev:DEV_DIR,
        dist:DIST_DIR
    },
    assets:{
        src:SRC_DIR+'assets/**/*',
        dev:DEV_DIR+'assets',
        dist:DIST_DIR+'assets'
    },
    style:{
        src:SRC_DIR+'style/*.less',  //如果是scss或者css,就改对应的
        dev:DEV_DIR+'css',
        dist:DIST_DIR+'css'
    },
    script:{
        src:SRC_DIR+'script/*.js',
        dev:DEV_DIR+'js',
        dist:DIST_DIR+'js'
    },
    img:{
        src:SRC_DIR+'images/**/*',
        dev:DEV_DIR+'images',
        dist:DIST_DIR+'images'
    }
};

module.exports=config;  //把这个接口暴露给别的文件用

const gulp=require('gulp'); //引用gulp插件
const $=require('gulp-load-plugins')();  //自动加载插件,可以省略一个一个引用插件
const config=require('./index.js'); //引用配置的路径文件
const open=require('open'); //开启服务

function dev(){
    gulp.task('html:dev',function(){
        return gulp.src(config.html.src).pipe($.fileInclude()).pipe(gulp.dest(config.html.dev)).pipe($.connect.reload())
    });
    gulp.task('assets:dev',function(){
        return gulp.src(config.assets.src).pipe(gulp.dest(config.assets.dev)).pipe($.connect.reload())
    });
    gulp.task('style:dev',function(){
        return gulp.src(config.style.src).pipe($.less()).pipe($.autoprefixer({
                browseers: ['last 2 versions','Firefox>=20','last 2 Explorer versions','last 3 Safari versions'],
                cascade: true
        })).pipe(gulp.dest(config.style.dev)).pipe($.connect.reload())
    });
    gulp.task('script:dev',function(){
        return gulp.src(config.script.src).pipe($.babel()).pipe($.babel()).pipe(gulp.dest(config.script.dev)).pipe($.connect.reload())
    });
    gulp.task('img:dev',function(){
        return gulp.src(config.img.src).pipe($.imagemin()).pipe(gulp.dest(config.img.dev)).pipe($.connect.reload())
    });
    gulp.task('dev',['html:dev','style:dev','script:dev','img:dev'],function(){
            $.connect.server({
                root:config.dev,
                port:3030,
                livereload:true
            });
            open('http://localhost:3030');
            gulp.watch(config.html.src,['html:dev']);
            gulp.watch(config.html.src,['style:dev']);
            gulp.watch(config.html.src,['script:dev']);
            gulp.watch(config.html.src,['img:dev']);
    })
}
module.exports=dev;
//配置生产的文件

const gulp=require('gulp'); //引用gulp插件
const $=require('gulp-load-plugins')();  //自动加载插件,可以省略一个一个引用插件
const config=require('./index.js'); //引用配置的路径文件

function prod(){
    gulp.task('html',function(){
        return gulp.src(config.html.src).pipe($.fileInclude()).pipe(gulp.dest(config.html.dist))
    });
    gulp.task('assets',function(){
        return gulp.src(config.assets.src).pipe(gulp.dest(config.assets.dist))
    });
    gulp.task('style',function(){
        return gulp.src(config.style.src).pipe($.less()).pipe($.autoprefixer({
                browseers: ['last 2 versions','Firefox>=20','last 2 Explorer versions','last 3 Safari versions'],
                cascade: true
        })).pipe($.cleanCss({compatibility: 'ie8'}))
        .pipe(gulp.dest(config.style.dist))
    });
    gulp.task('script',function(){
        return gulp.src(config.script.src)
        .pipe($.babel())
        .pipe($.uglify())
        .pipe($.stripDebug({
            debugger:true,console:true,alert:false
        }))
        .pipe(gulp.dest(config.script.dist))
    });
    gulp.task('img',function(){
        return gulp.src(config.img.src).pipe($.imagemin()).pipe(gulp.dest(config.img.dist))
    });
    gulp.task('build',['html','style','script','assets','img'])
}
module.exports=prod;

是不是一目了然呢?

最后新建gulpfile.js

const gulp=require('gulp');
const prod=require('./config/gulpfile.prod.js');
const dev=require('./config/gulpfile.dev.js');
const config=require('./config/index.js');
const $=require('gulp-load-plugins')(); 

dev();  //启动开发环境,gulp dev
prod(); //启动生产环境,  gulp prod

gulp.task('clean',function(){
    gulp.src([config.dev,config.dist])
    .pipe($.clean());
});
gulp.task('sprite', function () {  //生成雪碧图,gulp sprite,分别生成dev和dist
  let spriteData = gulp.src(config.src+'/images/sprite_2/*.png')
  .pipe($.spritesmith({
    imgName: 'images/sprite_2.png',
    cssName: 'css/sprite.css',
    padding: 2, // 图片之间的间距
    algorithm: 'left-right', //图片排列格式,默认是top-down,我这里稍微修改下
    algorithmOpts: {sort: false} //是否排序
  }));

  return spriteData.pipe(gulp.dest(config.dist)).pipe(gulp.dest(config.dev));
});

插件介绍

然后至于怎么用,看上面的gulpfile配置喽!

然后献上:https://blog.csdn.net/qq_15096707/article/details/54293203
我是参考这个,思路不错的,可以“去粗取精”

溜了溜了~

上一篇 下一篇

猜你喜欢

热点阅读