前端構建之gulp與經常使用插件

gulp是什麼?css

http://gulpjs.com/ 相信你會明白的!html

與著名的構建工具grunt相比,有什麼優點呢?前端

  1. 易於使用,代碼優於配置
  2. 高效,不會產生過多的中間文件,減小I/O壓力
  3. 易於學習,API很是少,你能在很短的事件內學會gulp

那些經常使用的gulp插件

No.一、run-sequence

Linkshttps://www.npmjs.com/package/run-sequencenode

做用:讓gulp任務,能夠相互獨立,解除任務間的依賴,加強task複用git

推薦指數:★★★★★github

No.二、browser-sync

Linkshttp://www.browsersync.io/npm

做用:靜態文件服務器,同時也支持瀏覽器自動刷新json

推薦指數:★★★★★gulp

No.三、del

Links:https://www.npmjs.com/package/delapi

做用:刪除文件/文件夾

推薦指數:★★★★★

No.四、gulp-coffee

Linkshttps://github.com/wearefractal/gulp-coffee

做用:編譯coffee代碼爲Js代碼,使用coffeescript必備

推薦指數:★★★★

No.五、coffee-script

Linkshttps://www.npmjs.com/package/coffee-script

做用:gulpfile默認採用js後綴,若是要使用gulpfile.coffee來編寫,那麼須要此模塊

推薦指數:★★★

No.六、gulp-nodemon

Linkshttps://www.npmjs.com/package/gulp-nodemon

做用:自動啓動/重啓你的node程序,開發node服務端程序必備

推薦指數:★★★★★

No.七、yargs

Linkshttps://www.npmjs.com/package/yargs

做用:用於獲取啓動參數,針對不一樣參數,切換任務執行過程時須要

推薦指數:★★★

No.八、gulp-util

Linkshttps://www.npmjs.com/package/gulp-util

做用:gulp經常使用的工具庫

推薦指數:★★★★★

No.九、gulp-uglify

Linkshttps://www.npmjs.com/package/gulp-uglify

做用:經過UglifyJS來壓縮JS文件

推薦指數:★★★★

No.九、gulp-concat

Linkshttps://www.npmjs.com/package/gulp-concat

做用:合併JS

推薦指數:★★★★

No.十、gulp-sourcemaps

Linkshttps://www.npmjs.com/package/gulp-sourcemaps

做用:處理JS時,生成SourceMap

推薦指數:★★★★

No.十一、gulp-less

Linkshttps://www.npmjs.com/package/gulp-less

做用:將less預處理爲css

推薦指數:★★★★

No.十二、gulp-sass

Linkshttps://www.npmjs.com/package/gulp-sass

做用:將sass預處理爲css

推薦指數:★★★★

No.1三、gulp-autoprefixer

Linkshttps://www.npmjs.com/package/gulp-autoprefixer

做用:使用Autoprefixer來補全瀏覽器兼容的css。

推薦指數:★★★★

No.1四、gulp-minify-css

Linkshttps://www.npmjs.com/package/gulp-minify-css

做用:壓縮css。

推薦指數:★★★★

No.1五、connect-history-api-fallback

Linkshttps://www.npmjs.com/package/connect-history-api-fallback

做用:開發angular應用必須,用於支持HTML5 history API.

推薦指數:★★★

通常的gulpfile文件(採用coffee編寫)

首先是,node應用程序:

gulp = require(‘gulp‘)
runSequence = require(‘run-sequence‘)

coffee = require(‘gulp-coffee‘)
gutil = require(‘gulp-util‘)
del = require(‘del‘)
nodemon = require(‘gulp-nodemon‘)
argv = require(‘yargs‘).argv
rename = require(‘gulp-rename‘)
browserSync = require(‘browser-sync‘)
reload = browserSync.reload

# 處理參數
isDebug = not (argv.r || false)

# --入口任務-----------------------------------------------------------------
gulp.task(‘default‘, (callback)->
  runSequence(
    [‘clean‘]
    [‘coffee-server‘, ‘copy-server‘, ‘copy-client‘, ‘coffee-client‘, ‘copy-views‘]
    ‘serve‘
    [‘browserSync‘, ‘watch‘]
    callback
  )
)
# --構建相關任務---------------------------------------
gulp.task(‘clean‘, (callback)->
  del([‘./dist/‘], callback)
)

gulp.task(‘coffee-server‘, ->
  gulp.src([
    ‘./src/**/*.coffee‘
    ‘!./src/public/**/*.coffee‘
    ‘!./src/views/**‘
  ])
  .pipe(coffee({bare: true}).on(‘error‘, gutil.log))
  .pipe(gulp.dest(‘./dist/‘))
)

gulp.task(‘copy-server‘, ->
  gulp.src([
    ‘./src/config*/*.json‘
    ‘./src/database*/*.*‘
  ])
  .pipe(gulp.dest(‘./dist/‘))
)

gulp.task(‘copy-client‘, ->
  gulp.src([
    ‘./src/public*/**/*‘
    ‘!./src/public*/**/*.coffee‘
  ])
  .pipe(gulp.dest(‘./dist/‘))
)

gulp.task(‘coffee-client‘, ->
  gulp.src([
    ‘./src/public*/**/*.coffee‘
  ])
  .pipe(coffee({bare: true}).on(‘error‘, gutil.log))
  .pipe(gulp.dest(‘./dist/‘))
)

gulp.task(‘copy-views‘, ->
  gulp.src(‘./src/views/**/*.html‘)
  .pipe(rename({extname: ‘.vash‘}))
  .pipe(gulp.dest(‘./dist/views‘))
)


# --啓動程序,打開瀏覽器任務----------------------------------------------------
nodemon_instance = undefined
gulp.task(‘serve‘, (callback)->
  called = false
  if not nodemon_instance
    nodemon_instance = nodemon({
      script: ‘./dist/index.js‘
      ext: ‘none‘
    })
    .on(‘restart‘, ->
      console.log(‘restart server......................‘)
    )
    .on(‘start‘, ->
      if not called
        called = true
        callback()
    )
  else
    nodemon_instance.emit("restart")
    callback()
  nodemon_instance
)

gulp.task(‘browserSync‘, ->
  browserSync({
    proxy: ‘localhost:3000‘
    port: 8888
  #files: [‘./src/public/**/*‘]
    open: true
    notify: true
    reloadDelay: 500 # 延遲刷新
  })
)



# --監視任務------------------------------------------------
gulp.task(‘watch‘, ->
  gulp.watch([
    ‘./src/**/*.*‘
    ‘!./src/**/*.coffee‘
  ], [‘reload-client‘])
  gulp.watch(‘./src/**/*.coffee‘, [‘reload-server‘])
)

gulp.task(‘reload-client‘, (callback) ->
  runSequence(
    [‘copy-client‘, ‘coffee-client‘, ‘copy-views‘]
    ‘bs-reload‘
    callback
  )
)

gulp.task(‘reload-server‘, (callback) ->
  runSequence(
    [‘copy-server‘, ‘coffee-server‘]
    ‘serve‘
    ‘bs-reload‘
    callback
  )
)

gulp.task(‘bs-reload‘, ->
  browserSync.reload()
)

接下來是前端網站:

gulp = require(‘gulp‘)
gutil = require(‘gulp-util‘)
coffee = require(‘gulp-coffee‘)
del = require(‘del‘)
runSequence = require(‘run-sequence‘)
browserSync = require(‘browser-sync‘)
historyApiFallback = require(‘connect-history-api-fallback‘)
# 入口點
gulp.task(‘default‘, ->
  runSequence(
    [‘clean‘]
    [‘copy‘]
    [‘serve‘]
  )
)

gulp.task(‘copy‘, ->
  gulp.src([
    ‘./src/**/*.*‘
    ‘!./src/**/*.coffee‘
    ‘!./src/**/*.less‘
  ])
  .pipe(gulp.dest(‘./dist‘))
)

gulp.task(‘clean‘, (callback)->
  del([‘./dist/‘], callback)
)

gulp.task(‘serve‘, ->
  browserSync({
    server: {
      baseDir: "./dist"
      middleware: [historyApiFallback]
    }
    port: 2222
  })
)

gulp.task(‘watch‘, ->
  # do something...
)
相關文章
相關標籤/搜索