• gulp应用


    $ npm install gulp -g

    gulp安装到全域环境下,可以存取gulp的CLI

    $ npm install gulp --save-dev

    将gulp安装到本地端的项目内,并在package.json内的devDependencies注册。

    使用npm install 安装下列模块

    使用npm install 安装下列模块

    var gulp = require('gulp'), sass = require('gulp-ruby-sass'), autoprefixer = require('gulp-autoprefixer'), minifycss = require('gulp-minify-css'), jshint = require('gulp-jshint'), uglify = require('gulp-uglify'), imagemin = require('gulp-imagemin'), rename = require('gulp-rename'), clean = require('gulp-clean'), concat = require('gulp-concat'), notify = require('gulp-notify'), cache = require('gulp-cache'), livereload = require('gulp-livereload');

    示例文件

    //建立一个gulpfile.js档案,载入外挂:
    var gulp = require('gulp'),
        sass = require('gulp-sass'),
        autoprefixer = require('gulp-autoprefixer'),
        minifycss = require('gulp-minify-css'),
        jshint = require('gulp-jshint'),
        uglify = require('gulp-uglify'),
        imagemin = require('gulp-imagemin'),
        rename = require('gulp-rename'),
        clean = require('gulp-clean'),
        concat = require('gulp-concat'),
        notify = require('gulp-notify'),
        cache = require('gulp-cache'),
        livereload = require('gulp-livereload');
    
    //gulp.taskAPI用来建立任务。可以透过终端机输入$ gulp styles指令来执行任务。
    gulp.task('styles', function() {  
      return gulp.src('css/sass/pub.scss')
        .pipe(sass({ style: 'expanded' ,}))
        .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
        .pipe(gulp.dest('dist/css'))
        .pipe(rename({suffix: '.min'}))
        .pipe(minifycss())
        .pipe(gulp.dest('dist/css'))
        .pipe(notify({ message: 'Styles task complete' }));
    });
    
    //JSHint,拼接及缩小化JavaScript
    gulp.task('scripts', function() {  
      return gulp.src('src/scripts/**/*.js')
        .pipe(jshint('.jshintrc'))
        .pipe(jshint.reporter('default'))
        .pipe(concat('main.js'))
        .pipe(gulp.dest('dist/js'))
        .pipe(rename({suffix: '.min'}))
        .pipe(uglify())
        .pipe(gulp.dest('dist/js'))
        .pipe(notify({ message: 'Scripts task complete' }));
    });
    
    //对所有来源图片进行imagemin处理。
    gulp.task('images', function() {  
      return gulp.src('css/img/**/*')
        .pipe(imagemin({ optimizationLevel: 3, progressive: true, interlaced: true }))
        .pipe(gulp.dest('dist/assets/img'))
        .pipe(notify({ message: 'Images task complete' }));
    });
    
    //佈署之前,清除目的地目录并重建档案–以防任何已经被删除的来源档案遗留在目的地目录之中
    gulp.task('clean', function() {  
      return gulp.src(['dist/assets/css', 'dist/assets/js', 'dist/assets/img'], {read: false})
        .pipe(clean());
    });
    //预设任务,当只输入$ gulp指令时执行的任务
    gulp.task('default', ['clean'], function() {  
        gulp.start('styles', 'scripts', 'images');
    });
    
    
    //看守档案,并在更动发生后执行相关任务
    gulp.task('watch', function() {
    
      // 看守所有.scss档
      gulp.watch('css/sass/*.scss', ['styles']);
    
      // 看守所有.js档
     // gulp.watch('src/scripts/**/*.js', ['scripts']);
    
      // 看守所有图片档
      gulp.watch('css/img/**/*', ['images']);
    
    //即时重整(LiveReload),Gulp处理档案更动后,自动重新整理页面。
    
      // 建立即时重整伺服器
      var server = livereload();
    
      // 看守所有位在 dist/  目录下的档案,一旦有更动,便进行重整
      gulp.watch(['dist/**']).on('change', function(file) {
        server.changed(file.path);
      });
    
    });
    

    gulp 命令

    用 gulp +任务名 运行相应配置 用 gulp watch 监察目录变化并运行相应配置
    如果是用gulp-sass ,它是基于node-sass的 ,并保持对应模块名一致,gulp-sass不基于ruby而是c#性能更好。
  • 相关阅读:
    Java面试题及答案整理(持续更新)
    CentOS6.4-mini系统服务列表
    Linux内核编译,模块尺寸变大的解决办法
    Configuration Opennebula3.8 & 4.x Virtual Machines Contextualizing
    CentOS6.3上Opennebula 3.8 VLAN配置
    Ubuntu 12.04 LTS主机名、IP、DNS配置总结
    Linux su
    Git安装配置总结
    Linux CentOS添加163yum源
    Linux 内核编译 modules ehci-hcd, ohci-hcd, uhci-hcd not found
  • 原文地址:https://www.cnblogs.com/uh-huh/p/4950776.html
Copyright © 2020-2023  润新知