原博主:幻天芒 原文地址:http://www.cnblogs.com/humin/p/4337442.html

gulp是什么?

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

与著名的构建工具grunt相比,有什么优势呢?

  1. 易于使用,代码优于配置
  2. 高效,不会产生过多的中间文件,减少I/O压力
  3. 易于学习,API非常少,你能在很短的事件内学会gulp

那些常用的gulp插件

No.1、run-sequence

Links: https://www.npmjs.com/package/run-sequence

作用:让gulp任务,可以相互独立,解除任务间的依赖,增强task复用

推荐指数:★★★★★

No.2、browser-sync

Links: http://www.browsersync.io/

作用:静态文件服务器,同时也支持浏览器自动刷新

推荐指数:★★★★★

No.3、del

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

作用:删除文件/文件夹

推荐指数:★★★★★

No.4、gulp-coffee

Links: https://github.com/wearefractal/gulp-coffee

作用:编译coffee代码为Js代码,使用coffeescript必备

推荐指数:★★★★

No.5、coffee-script

Links: https://www.npmjs.com/package/coffee-script

作用:gulpfile默认采用js后缀,如果要使用gulpfile.coffee来编写,那么需要此模块

推荐指数:★★★

No.6、gulp-nodemon

Links: https://www.npmjs.com/package/gulp-nodemon

作用:自动启动/重启你的node程序,开发node服务端程序必备

推荐指数:★★★★★

No.7、yargs

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

作用:用于获取启动参数,针对不同参数,切换任务执行过程时需要

推荐指数:★★★

No.8、gulp-util

Links: https://www.npmjs.com/package/gulp-util

作用:gulp常用的工具库

推荐指数:★★★★★

No.9、gulp-uglify

Links: https://www.npmjs.com/package/gulp-uglify

作用:通过UglifyJS来压缩JS文件

推荐指数:★★★★

No.9、gulp-concat

Links: https://www.npmjs.com/package/gulp-concat

作用:合并JS

推荐指数:★★★★

No.10、gulp-sourcemaps

Links: https://www.npmjs.com/package/gulp-sourcemaps

作用:处理JS时,生成SourceMap

推荐指数:★★★★

No.11、gulp-less

Links:https://www.npmjs.com/package/gulp-less

作用:将less预处理为css

推荐指数:★★★★

No.12、gulp-sass

Links:https://www.npmjs.com/package/gulp-sass

作用:将sass预处理为css

推荐指数:★★★★

No.13、gulp-autoprefixer

Links:https://www.npmjs.com/package/gulp-autoprefixer

作用:使用Autoprefixer来补全浏览器兼容的css。

推荐指数:★★★★

No.14、gulp-minify-css

Links:https://www.npmjs.com/package/gulp-minify-css

作用:压缩css。

推荐指数:★★★★

No.15、connect-history-api-fallback

Links:https://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...
)

由于connect-history-api-fallback这个包的用法改变,所以在browserSync中使用的时候,请使用middleware: [historyApiFallback()]

前端构建之gulp与常用插件(转载)的更多相关文章

  1. 前端构建之gulp与常用插件

    gulp是什么? http://gulpjs.com/ 相信你会明白的! 与著名的构建工具grunt相比,有什么优势呢? 易于使用,代码优于配置 高效,不会产生过多的中间文件,减少I/O压力 易于学习 ...

  2. 前端构建工具之gulp_常用插件

    gulp常用插件的使用 今天来看看一下gulp的常用插件的使用 就像gruntjs需要一个Gruntfile.js文件一样,gulp也需要一个文件作为它的主文件,在gulp中这个文件叫做gulpfil ...

  3. (转载)前端构建工具gulp使用

    前端构建工具gulp使用 前端自动化流程工具,用来合并文件,压缩等. Gulp官网 http://gulpjs.com/ Gulp中文网 http://www.gulpjs.com.cn/ Gulp中 ...

  4. 前端构建工具gulp

    前端构建工具gulp使用   前端自动化流程工具,用来合并文件,压缩等. Gulp官网 http://gulpjs.com/ Gulp中文网 http://www.gulpjs.com.cn/ Gul ...

  5. 前端构建大法 Gulp 系列 (四):gulp实战

    前端构建大法 Gulp 系列 (一):为什么需要前端构建 前端构建大法 Gulp 系列 (二):为什么选择gulp 前端构建大法 Gulp 系列 (三):gulp的4个API 让你成为gulp专家 前 ...

  6. 前端构建大法 Gulp 系列 (三):gulp的4个API 让你成为gulp专家

    系列目录 前端构建大法 Gulp 系列 (一):为什么需要前端构建 前端构建大法 Gulp 系列 (二):为什么选择gulp 前端构建大法 Gulp 系列 (三):gulp的4个API 让你成为gul ...

  7. 前端构建大法 Gulp 系列 (二):为什么选择gulp

    系列目录 前端构建大法 Gulp 系列 (一):为什么需要前端构建 前端构建大法 Gulp 系列 (二):为什么选择gulp 前端构建大法 Gulp 系列 (三):gulp的4个API 让你成为gul ...

  8. 前端构建大法 Gulp 系列 (一):为什么需要前端构建

    系列目录 前端构建大法 Gulp 系列 (一):为什么需要前端构建 前端构建大法 Gulp 系列 (二):为什么选择gulp 前端构建大法 Gulp 系列 (三):gulp的4个API 让你成为gul ...

  9. (转)前端构建工具gulp入门教程

    前端构建工具gulp入门教程 老婆婆 1.8k 2013年12月30日 发布 推荐 10 推荐 收藏 83 收藏,20k 浏览 本文假设你之前没有用过任何任务脚本(task runner)和命令行工具 ...

随机推荐

  1. 自己动手修改Robotium代码(上)

    Robotium作为Android自动化测试框架,还有许多不完善的地方,也不能满足测试人员的所有要求.那么,本文以四个实际中碰到的问题为例,介绍改动Robotium源码的过程. public bool ...

  2. ABP module-zero +AdminLTE+Bootstrap Table+jQuery权限管理系统第十三节--RBAC模式及ABP权限管理(附送福利)

    ABP+AdminLTE+Bootstrap Table权限管理系统一期 Github:https://github.com/Jimmey-Jiang/ABP-ASP.NET-Boilerplate- ...

  3. 《java.util.concurrent 包源码阅读》15 线程池系列之ScheduledThreadPoolExecutor 第二部分

    这篇文章主要说说DelayedWorkQueue. 在ScheduledThreadPoolExecutor使用DelayedWorkQueue来存放要执行的任务,因为这些任务是带有延迟的,而每次执行 ...

  4. Animation-list,帧动画+属性动画,做出Flash般的效果

    我们会用到PS,即使不会也不要怂,只需要几步傻瓜式操作即可. 属性动画可以看看我另一篇文章:属性动画详解 效果图 相信机智的各位,看完之后一定能发挥创意,做出更酷更炫的效果 图层获取 首先你需要找一张 ...

  5. 纯Socket(BIO)长链接编程的常见的坑和填坑套路

    本文章纯属个人经验总结,伪代码也是写文章的时候顺便白板编码的,可能有逻辑问题,请帮忙指正,谢谢. Internet(全球互联网)是无数台机器基于TCP/IP协议族相互通信产生的.TCP/IP协议族分了 ...

  6. QQ空间掉帧率优化实战

    商业转载请联系腾讯WeTest获得授权,非商业转载请注明出处. WeTest 导读 空间新业务需求日益增多,在业务开发阶段的疏忽,或者是受到其他业务的影响(比如一些非空间的业务网络回包或者逻辑在主线程 ...

  7. C#使用Xamarin开发可移植移动应用终章(11.获取设备信息与常用组件,开源一个可开发模版.)

    前言 系列目录 C#使用Xamarin开发可移植移动应用目录 源码地址:https://github.com/l2999019/DemoApp 可以Star一下,随意 - - 说点什么.. 本系列,终 ...

  8. Mysql主从原理

    MySQL的Replication(英文为复制)是一个多MySQL数据库做主从同步的方案,特点是异步复制,广泛用在各种对mysql有更高性能.更高可靠性要求的场合.与之对应的是另一个同步技术是MySQ ...

  9. centos yum install redis

    linux下yum安装redis以及使用 1.yum install redis      --查看是否有redis   yum 源 [root@localhost ~]# yum install r ...

  10. SpringCloud Feign对Hystrix(断路由)的支持

    第一步:首先开启Feign对Hystrix的支持,在properties文件中添加以下配置: feign.hystrix.enabled=true. 第二步:在上一篇Feign的基础上添加Hystri ...