• gulp笔记(从开发到发布)


    前言:gulp相对而言比较简单,趁有空整理出一套自己喜欢的结构来。自己想做些简单的小网站时候可以复用,代码经过测试,可以直接拿来使用。

    主要实现功能:ES6转换,sass编译,合成雪碧图,css自动加前缀,压缩合并编译后的css,img图片压缩,js压缩,实施监听并刷新浏览器,以及sftp插件上传代码到服务器(未验证)

    开发环境:node v8.9.4  gulp v3.9.1

    目录结构:

    先上package.json

    {
      "name": "gulpOnly",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo "Error: no test specified" && exit 1"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "devDependencies": {
        "babel-core": "^6.26.0",
        "babel-preset-env": "^1.6.1",
        "browser-sync": "^2.23.7",
        "get-gulp-args": "0.0.1",
        "gulp": "^3.9.1",
        "gulp-autoprefixer": "^5.0.0",
        "gulp-babel": "^7.0.1",
        "gulp-clean": "^0.4.0",
        "gulp-concat": "^2.6.1",
        "gulp-css-img-sprite": "^1.0.2",
        "gulp-imagemin": "^4.1.0",
        "gulp-minify-css": "^1.2.4",
        "gulp-sass": "^4.0.1",
        "gulp-sftp": "^0.1.5",
        "run-sequence": "^2.2.1",
        "streamfilter": "^1.0.7"
      },
      "dependencies": {
        "gulp-load-plugins": "^1.5.0",
        "gulp-uglify": "^3.0.0"
      }
    }

    下面是gulpfile

    const gulp = require('gulp');
    const gulpLoadPlugins = require('gulp-load-plugins');
    const plugins = gulpLoadPlugins({lazy: true});//加载package.json中的插件,仅限于gulp开头的插件
    const streamfilter = require('streamfilter'); //用于过滤文件
    const runSequence = require('run-sequence');//用于实现gulp同步执行
    const browserSync = require('browser-sync').create(); //用于启动服务,并自动刷新
    const getargs = require('get-gulp-args'); //用于得到命令行参数
    const dist = 'dist'; // 发布目录
    const src = 'src'; //源目录
    
    const args = getargs(); //得到命令行传参
    
    //服务器配置
    const servers = {
        dev : {
            host: 'xx.xx.xx.dev',
            user: 'username',
            pass: 'password',
            remotePath : '/'
        },
        test : {
            host: 'xx.xx.xx.test',
            user: 'username',
            pass: 'password',
            remotePath : '/'
        }
    };
    
    let watch = function(){
        gulp.watch(src + '/**/*.html' , ['copy']); 
        gulp.watch(src + '/**/*.js' , ['babel']);
        gulp.watch(src + '/**/*.scss' , ['sass']);
        gulp.watch(src + '/image/**', ['imgMin']);
        gulp.watch([src+'/**']).on('change',browserSync.reload); 
    }
    
    gulp.task('clean', ()=>{
        return gulp.src(dist)
                   .pipe(plugins.clean())
    })
    
    gulp.task('clean.html',()=>{
        return gulp
            .src([dist + '/**/*.html'])
            .pipe(plugins.clean());
    });
    
    gulp.task('clean.css',()=>{
        return gulp
            .src([dist + '/**/*.css'])
            .pipe(plugins.clean());
    });
    
    gulp.task('clean.js',()=>{
        return gulp
            .src([dist + '/**/*.js'])
            .pipe(plugins.clean());
    });
    
    gulp.task('clean.img',()=>{
        return gulp
            .src([dist + '/image/**/*'])
            .pipe(plugins.clean());
    });
    
    gulp.task('clean.slice',()=>{
        return gulp
            .src([dist + '/slice/**/*'])
            .pipe(plugins.clean());
    });
    
    gulp.task('copy',['clean'], () => {
        return gulp
            .src([src + '/**/*','!'+src+'/**/*.js','!'+src+'/**/*.scss','!'+src+'/image/**/*'])
            .pipe(gulp.dest(dist));
    });
    // sass编译,合成雪碧图,压缩合并编译后的css,自动加前缀
    gulp.task('sass',['clean.css','clean.slice'], () => {
        return gulp
            .src([src + '/**/*.scss']) 
            .pipe(plugins.sass({outputStyle: 'expanded'})//编译sass
            .on('error', plugins.sass.logError))
            .pipe(plugins.cssImgSprite({ //合成雪碧图
                cssDesDir: dist,
                imgDesDir: dist + '/slice',
                hash: true
            }))
            .pipe(plugins.autoprefixer({ // 自动加前缀
                browsers: ['last 2 versions','Safari >0', 'Explorer >0', 'Edge >0', 'Opera >0', 'Firefox >=20'],//last 2 versions- 主流浏览器的最新两个版本
                cascade: false, //格式美化
                remove:true //是否去掉不必要的前缀 默认:true 
            }))
            .pipe(plugins.concat('main.css'))
            .pipe(plugins.minifyCss()) //压缩
            .pipe(gulp.dest(dist+'/css/'))
    });
    
    //图片压缩
    gulp.task('imgMin',['clean.img'], ()=>{
        return gulp
            .src([src + '/image/*.*'])
            .pipe(plugins.imagemin(({ 
                  optimizationLevel: 3, 
                  progressive: true, 
                  interlaced: true})
             ))
            .pipe(gulp.dest(dist+"/image"))
    });
    
    //ES6转换
    gulp.task('babel',['clean.js'], () => {
        return gulp
            .src([src + '/**/*.js'])
            .pipe(plugins.babel({
                presets: ['env']
            }))
            .pipe(plugins.uglify())
            .pipe(gulp.dest(dist));
    });
    
    //启动本地服务
    gulp.task('server', ()=>{
        browserSync.init({
            notify : false,
            port:9000,  //端口号
            server:{
                baseDir:['dist']//服务根目录
            }
        });
    });
    
    //上传代码到服务器
    gulp.task('upload', ()=>{
        const globParams = {
            allowEmpty : false,
            base : dist
        };
        let stream = gulp.src([dist + '/**/*'], globParams);
        args.servers.split(',').forEach(serverName => {
            const server = servers[serverName];
            if(!server){
                console.warn('找不到服务器', serverName, ',只支持dev和test');
                return ;
            }
    
            stream = stream.pipe(plugins.sftp(server));
        });
    
        return stream;
    
    });
    
    //开发
    gulp.task('dev', function(done) {
        runSequence(['copy','imgMin','sass','babel'],'server');
        watch();
    });
    
    // 发布,分为dev开发环境,test测试环境
    gulp.task('deploy', function(){
        if(!args.servers){
            console.log('Usage:
    gulp deploy --servers=dev,test');
            return ;
        }
        runSequence(['copy','imgMin','sass','babel'],'upload');
    });
  • 相关阅读:
    014-More than one file was found with OS independent path 'META-INF/DEPENDENCIES'
    013-一个Activity怎么调用另一个Activity的方法返回数据(转)
    012-关于EditText中的getText()方法的返回值类型以及string的转换问题(转)
    011-frament中不能调用getSystemService()方法
    010-Android开发解决控件超出屏幕,不能正常显示的问题
    009-在Fragment中实现Activity跳转功能
    Oracle分区表
    Oracle构造列
    Oracle集合
    Oracle多对多、维表
  • 原文地址:https://www.cnblogs.com/solaZhan/p/8980584.html
Copyright © 2020-2023  润新知