zoukankan      html  css  js  c++  java
  • Gulp系列文章实际项目配置

    目录结构

    项目地址

    image压缩、精灵图合成

    var gulp = require('gulp');
    var imagemin = require('gulp-imagemin');
    var spritesmith = require('gulp.spritesmith');
    gulp.task('img', function() {
      //压缩图片
      gulp.src('src/images/*.{png,jpg,gif,ico}')
        .pipe(imagemin())
        .pipe(gulp.dest('dist/images'));
      //制作雪碧图
      gulp.src('src/images/sprite/*')
        .pipe(spritesmith({
          'imgName': 'sprite.png',
          'cssName': 'sprite.css',
          'padding': 5
        }))
        .pipe(gulp.dest('src/styles/sprite'));   
      gulp.src('src/styles/sprite/sprite.png')     
        .pipe(gulp.dest('dist/images'));
    });
    gulp.task('default', ['img']);
    

    sass编译、合并、压缩

    var gulp = require('gulp');
    var concat = require('gulp-concat');
    var sass = require('gulp-sass');
    var autoprefixer = require('gulp-autoprefixer');
    var cleancss = require('gulp-clean-css');
    var clean = require('gulp-clean');
    var rename = require("gulp-rename");
    
    gulp.task('sass', function() {
      return  gulp.src('src/styles/*.scss')
        .pipe(sass().on('error', sass.logError))
        .pipe(gulp.dest('dist/styles/css'));
    });
    
    gulp.task('css', ['sass'], function() {
      return gulp.src('dist/styles/css/*.css')
        //合并文件
        .pipe(concat('style.css'))
        //根据设置浏览器版本自动处理浏览器前缀
        .pipe(autoprefixer({
          browsers: ['last 2 versions', 'Android >= 4.0']
        }))
        //压缩css文件
        .pipe(cleancss({ compatibility: 'ie9' }))
        .pipe(rename("style.min.css"))
        //输出到目标目录
        .pipe(gulp.dest('dist/styles'));
    });
    
    gulp.task('default', ['css']);
    

    JS编译压缩

    var gulp = require('gulp');
    var babel = require('gulp-babel');
    var uglify = require('gulp-uglify');
    gulp.task('js', function(){
      return gulp.src('src/scripts/*.js')
        .pipe(babel({
          presets: ["babel-preset-es2015", "babel-preset-es2016", "babel-preset-es2017"].map(require.resolve)
        }))
        .pipe(uglify())
        .pipe(gulp.dest('dist/scripts'))
    })
    
    gulp.task('default', ['js']);
    

    为文件设置版本号

    var gulp = require('gulp');
    var rev = require('gulp-rev');
    var useref = require('gulp-useref');
    var revReplace = require('gulp-rev-replace');
    gulp.task('file', function() {
      gulp.src('*.html')
        .pipe(useref())
        .pipe(rev())
        .pipe(revReplace())
        .pipe(gulp.dest(''));
    });
    gulp.task('default', ['file']);
    

    监控变化

    var gulp = require('gulp');
    var gulpSequence = require('gulp-sequence');
    
    // image
    var imagemin = require('gulp-imagemin');
    var spritesmith = require('gulp.spritesmith');
    gulp.task('img', function() {
      //压缩图片
      gulp.src('src/images/*.{png,jpg,gif,ico}')
        .pipe(imagemin())
        .pipe(gulp.dest('dist/images'));
      //制作雪碧图
      gulp.src('src/images/sprite/*')
        .pipe(spritesmith({
          'imgName': 'sprite.png',
          'cssName': 'sprite.css',
          'padding': 5
        }))
        .pipe(gulp.dest('src/styles/sprite'));   
      gulp.src('src/styles/sprite/sprite.png')     
        .pipe(gulp.dest('dist/images'));
    });
    
    // css
    var concat = require('gulp-concat');
    var sass = require('gulp-sass');
    var autoprefixer = require('gulp-autoprefixer');
    var cleancss = require('gulp-clean-css');
    var clean = require('gulp-clean');
    var rename = require("gulp-rename");
    
    gulp.task('sass', function() {
      return gulp.src('src/styles/*.scss')
        .pipe(sass().on('error', sass.logError))
        .pipe(gulp.dest('dist/styles/css'));
    });
    
    gulp.task('css', ['sass'], function() {
      return gulp.src('dist/styles/css/*.css')
        //合并文件
        .pipe(concat('style.css'))
        //根据设置浏览器版本自动处理浏览器前缀
        .pipe(autoprefixer({
          browsers: ['last 2 versions', 'Android >= 4.0']
        }))
        //压缩css文件
        .pipe(cleancss({ compatibility: 'ie9' }))
        .pipe(rename("style.min.css"))
        //输出到目标目录
        .pipe(gulp.dest('dist/styles'));
    });
    
    
    // js 
    var babel = require('gulp-babel');
    var uglify = require('gulp-uglify');
    gulp.task('js', function(){
      return gulp.src('src/scripts/*.js')
        .pipe(babel({
          presets: ['babel-preset-es2015', 'babel-preset-es2016', 'babel-preset-es2017'].map(require.resolve)
        }))
        .pipe(uglify())
        .pipe(gulp.dest('dist/scripts'))
    })
    
    // file hash
    var rev = require('gulp-rev');
    var useref = require('gulp-useref');
    var revReplace = require('gulp-rev-replace');
    gulp.task('file', function() {
      return gulp.src('*.html')
        .pipe(useref())
        .pipe(rev())
        .pipe(revReplace())
        .pipe(gulp.dest(''));
    });
    
    
    // watch 
    gulp.task('auto',function(){
      gulp.watch('src/images', ['img']);
      gulp.watch('src/styles/*.scss',['css']);
      gulp.watch('src/scripts/*.js',['js']);
      gulp.watch('dist/styles/style.min.css',['file']);
    })
    
    
    gulp.task('default',gulpSequence('img', 'css', 'js', 'file', 'auto'));
    

    项目地址

  • 相关阅读:
    步步为营 C# 技术漫谈 一、反射机制
    Windows API、CRT和STL
    MVC3 基本业务开发框架
    .NET实现之(WebBrowser数据采集—终结篇)
    步步为营 C# 技术漫谈 二、ASP.NET 页生命周期
    步步为营 .NET 代码重构学习笔记 十
    步步为营 .NET 代码重构学习笔记 十三
    MagicDict开发总结6 [划词 检索]
    步步为营 .NET 代码重构学习笔记系列总结
    项目多少是可以配置的?
  • 原文地址:https://www.cnblogs.com/yesyes/p/15375787.html
Copyright © 2011-2022 走看看