building with Gulp

本文章基於本週三分享內容進行整理所做。javascript

Gulp 是什麼?

  • 構建系統css

  • 基於 Node.js 構建html

  • 使用 javascript 編寫java

  • 擁有衆多的插件node

  • 開源git

安裝 Gulp

全局安裝github

npm install -g gulp

安裝到項目中npm

npm install --save-dev gulp

使用 Gulp

建立 Gulpfile.js

var gulp = require('gulp'),
       uglify = require('gulp-uglify');
    
    gulp.task('minify', function () {
       gulp.src('js/app.js')
          .pipe(uglify())
          .pipe(gulp.dest('build'))
    });

分解爲 3 個部分gulp

  1. 定義模塊app

    var gulp = require('gulp'),
        uglify = require('gulp-uglify');
  2. 定義任務

    gulp.task('minify', function () {
        });
  3. 定義任務執行具體邏輯

    gulp.src('js/app.js')
        .pipe(uglify())
        .pipe(gulp.dest('build'))

    上述代碼可轉爲流程圖以下

    圖片描述

    另外一個例子

    gulp.task('js', function () {
       return gulp.src('js/*.js')
          .pipe(jshint())
          .pipe(jshint.reporter('default'))
          .pipe(uglify())
          .pipe(concat('app.js'))
          .pipe(gulp.dest('build'));
        });

圖片描述

gulp.src()

使用 node-glob 匹配文件路徑

js/app.js

js/*.js

js/**/*.js

!js/app.js    Excludes js/app.js from the match, which is useful if you want to match all files in a directory except for a particular file

*.+(js|css)    Matches all files in the root directory ending in .js or .css

包含多個文件

gulp.src(['js/**/*.js', '!js/**/*.min.js'])

gulp.task()

gulp.task('greet', function () {
   console.log('Hello world!');
});

gulp.task('build', ['css', 'js', 'imgs']);

gulp.task('css', ['greet'], function () {
   // Deal with CSS here
});

gulp.task('default', function () {
   // Your default task
});

gulp.watch()

gulp.task('watch', function () {
   gulp.watch('templates/*.tmpl.html', ['build']);
});

gulp.watch('templates/*.tmpl.html', function (event) {
   console.log('Event type: ' + event.type); // added, changed, or deleted
   console.log('Event path: ' + event.path); // The path of the modified file
});

var watcher = gulp.watch('templates/*.tmpl.html', ['build']);
watcher.on('change', function (event) {
   console.log('Event type: ' + event.type); // added, changed, or deleted
   console.log('Event path: ' + event.path); // The path of the modified file
});

除了 change 還支持 error、end、ready、nomatch等事件

動態更新

LiveReload

圖片描述

BrowserSync

圖片描述

爲什麼使用 Gulp

實現相同的功能

Grunt

grunt.initConfig({
   less: {
      development: {
         files: {
            "build/tmp/app.css": "assets/app.less"
         }
      }
   },

   autoprefixer: {
      options: {
         browsers: ['last 2 version', 'ie 8', 'ie 9']
      },
      multiple_files: {
         expand: true,
         flatten: true,
         src: 'build/tmp/app.css',
         dest: 'build/'
      }
   }
});

grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-autoprefixer');

grunt.registerTask('css', ['less', 'autoprefixer']);

Gulp

var gulp = require('gulp'),
   less = require('gulp-less'),
   autoprefix = require('gulp-autoprefixer');

gulp.task('css', function () {
   gulp.src('assets/app.less')
      .pipe(less())
      .pipe(autoprefix('last 2 version', 'ie 8', 'ie 9'))
      .pipe(gulp.dest('build'));
});

參考連接

相關文章
相關標籤/搜索