gulp plugins 插件介绍
原文:http://ju.outofmemory.cn/entry/103253
Gulp是一个构建工具, 功能类似grunt, 以及Java生态圈的ant, maven, gradle等。 其它的javascript生态圈的构建工具可以参考: List of JavaScript Build Tools
它采用了一种流式处理的方式, 编写起来简单直观。 相对于其它javascript构建工具, 母亲啊它的star数是仅次于grunt,流行度还是比较高的。
通过”代码优于配置” (code over configuration), 通过javascript编写构建任务, 充分利用javascript生态圈的组件, 可以实现简单灵活的任务管理。 通过node stream的方式,直接在内存中管道式处理流,不必缓存到硬盘上, 节省构建时间。
Gulp介绍请参考我转载的另一篇文章: Building With Gulp
另外有一篇很好的入门文章: Getting started with gulp, 繁体版, 简体中文硬翻版
从我的实践上来看, gulp要比grunt更好的管理构建过程。 编写简单,条理清晰,功能强大,学习无曲线。
Gulp是基于Node.js构建的,因此Gulp源文件和你用来定义任务的Gulp文件都被写进了JavaScript(或者CoffeeScript)里。 Gulp本身虽然不能完成很多任务,但它有大量插件可用,开发者可以访问插件页面或者在npm搜索gulpplugin或者gulpfriendly就能看到。例如,有些插件可以用来执行JSHint、编译CoffeeScript,执行Mocha测试,甚至更新版本号。现在有大约980个左右的插件可以使用。你可以到http://gulpjs.com/plugins/或者http://npmsearch.com/?q=keywords:gulpplugin查找所需的软件。
面对如此众多的插件, 想要全部了解并灵活运用它们几乎是不可能的事情。 实际开发中多参考别的项目的实现, 根据自己的需求寻找合适的插件, 总结并开发自己的插件, 逐步积累对gulp的认识。
本文列出常用的几个插件, 并在将来的开发中更新此文作为记录文档。 多个插件和grunt的插件功能类似。
首先介绍以下gulp本身的API, 相当的简洁,只有几个函数。
gulp API
流(Stream)能够通过一系列的小函数来传递数据,这些函数会对数据进行修改,然后把修改后的数据传递给下一个函数。
看一个简单例子:
var gulp =require('gulp'),uglify =require('gulp-uglify');gulp.task('minify',function(){gulp.src('js/app.js').pipe(uglify()).pipe(gulp.dest('build'))});
gulp.src()函数用字符串匹配一个文件或者文件的编号(被称为“glob”),然后创建一个对象流来代表这些文件,接着传递给uglify()函数,它接受文件对象之后返回有新压缩源文件的文件对象,最后那些输出的文件被输入gulp.dest()函数,并保存下来。
想了解更多的关于node stream方面的知识,可以访问stream-handbook。 stream-handbook中文翻译
gulp.src(globs[, options])
根据globs提供的文件列表, 得到一个Vinyl文件的stream, 可以按照管道模式给其它插件处理。
gulp.src('client/templates/*.jade').pipe(jade()).pipe(minify()).pipe(gulp.dest('build/minified_templates'));
gulp.dest(path[, options])
将管道中的数据写入到文件夹。
gulp.task(name[, deps], fn)
使用orchestrator定义任务。
gulp.task('somename',function(){// Do stuff});
deps 是任务数组,在执行本任务时数组中的任务要执行并完成。
gulp.watch(glob [, opts], tasks), gulp.watch(glob [, opts, cb])
监控文件。当监控的文件有所改变时执行特定的任务。
Recipes
下面的文章总结的几个常见问题的解决方案,非常有参考价值。
https://github.com/gulpjs/gulp/tree/master/docs/recipes#recipes
gulp-browserify
browserify可以为浏览器编译node风格的遵循commonjs的模块。 它搜索文件中的require()
调用, 递归的建立模块依赖图。
var gulp =require('gulp');var browserify =require('gulp-browserify');// Basic usage
gulp.task('scripts',function(){// Single entry point to browserifygulp.src('src/js/app.js').pipe(browserify({insertGlobals :true,debug :!gulp.env.production})).pipe(gulp.dest('./build/js'))});
gulp-jshint
gulp的jshint插件。
jshint是一个侦测javascript代码中错误和潜在问题的工具。
用法:
var jshint =require('gulp-jshint');var gulp =require('gulp');gulp.task('lint',function(){return gulp.src('./lib/*.js').pipe(jshint()).pipe(jshint.reporter('YOUR_REPORTER_HERE'));});
gulp-jslint
jslint是一个javascript代码质量检测工具。
gulp-jslint是它的gulp插件。
var gulp =require('gulp');var jslint =require('gulp-jslint');// build the main source into the min file
gulp.task('default',function(){return gulp.src(['source.js'])// pass your directives// as an object.pipe(jslint({// these directives can// be found in the official// JSLint documentation.node:true,evil:true,nomen:true,// you can also set global// declarations for all source// files like so:global:[],predef:[],// both ways will achieve the// same result; predef will be// given priority because it is// promoted by JSLint// pass in your prefered// reporter like so:reporter:'default',// ^ there's no need to tell gulp-jslint// to use the default reporter. If there is// no reporter specified, gulp-jslint will use// its own.// specify whether or not// to show 'PASS' messages// for built-in reportererrorsOnly:false}))// error handling:// to handle on error, simply// bind yourself to the error event// of the stream, and use the only// argument as the error object// (error instanceof Error).on('error',function(error){console.error(String(error));});});
imagemin
imagemin是压缩图片的工具。
var gulp =require('gulp');var imagemin =require('gulp-imagemin');var pngquant =require('imagemin-pngquant');gulp.task('default',function(){return gulp.src('src/images/*').pipe(imagemin({progressive:true,svgoPlugins:[{removeViewBox:false}],use:[pngquant()]})).pipe(gulp.dest('dist'));});
glup-sass
sass是编写css的一套语法。 使用它的预处理器可以将sass语法的css处理成css格式。
glup-sass语法:
var gulp =require('gulp');var sass =require('gulp-sass');gulp.task('sass',function(){gulp.src('./scss/*.scss').pipe(sass()).pipe(gulp.dest('./css'));});
gulp-ruby-sass是另外一款sass的gulp插件, 比glup-sass慢,但是更稳定,功能更多。 它使用compass预处理sass文件,所以你需要安装ruby和compass。
var gulp =require('gulp');var sass =require('gulp-ruby-sass');gulp.task('default',function(){return gulp.src('src/scss/app.scss').pipe(sass({sourcemap:true, sourcemapPath:'../scss'})).on('error',function(err){ console.log(err.message);}).pipe(gulp.dest('dist/css'));});
browser-sync
BrowserSync 是一个自动化测试辅助工具,可以帮你在网页文件变更时自动载入新的网页。
用法:
var gulp = require('gulp');var browserSync =require('browser-sync');// Static server
gulp.task('browser-sync',function(){browserSync({server:{baseDir:"./"}});});// or...gulp.task('browser-sync',function(){browserSync({proxy:"yourlocal.dev"});});
还可以使用proxy-middleware
作为http proxy,转发特定的请求。
gulp-handlebars
handlebars是一个模版引擎库, ember.js用它作为前端的模版引擎。
gulp-handlebars编译handlebars文件。
用法:
var handlebars =require('gulp-handlebars');var wrap =require('gulp-wrap');var declare =require('gulp-declare');var concat =require('gulp-concat');gulp.task('templates',function(){gulp.src('source/templates/*.hbs').pipe(handlebars()).pipe(wrap('Handlebars.template(<%= contents %>)')).pipe(declare({namespace:'MyApp.templates',noRedeclare:true,// Avoid duplicate declarations})).pipe(concat('templates.js')).pipe(gulp.dest('build/js/'));});
gulp-usemin
用来将HTML 文件中(或者templates/views)中没有优化的script 和stylesheets 替换为优化过的版本。
usemin 暴露两个内置的任务,分别为:
- useminPrepare 为将指定文件中的 usemin block 转换为单独的一行(优化版本)准备配置。这通过为每个优化步骤生成名为 generated 的子任务来完成。
- usemin 使用优化版本替换 usemin 块,如果在磁盘上可以找到 revisioned 版本,则替换为 revisioned 版本。
usemin块如下定义:
<!-- build:<pipelineId>(alternate search path) <path> -->
... HTML Markup, list of script / link tags.
<!-- endbuild -->
如
<!-- build:css style.css --><linkrel="stylesheet"href="http://colobu.com/2014/11/17/gulp-plugins-introduction/css/clear.css"/><linkrel="stylesheet"href="http://colobu.com/2014/11/17/gulp-plugins-introduction/css/main.css"/><!-- endbuild --><!-- build:js js/lib.js --><scriptsrc="http://colobu.com/2014/11/17/lib/angular-min.js"></script><scriptsrc="http://colobu.com/2014/11/17/lib/angular-animate-min.js"></script><!-- endbuild --><!-- build:js1 js/app.js --><scriptsrc="http://colobu.com/2014/11/17/gulp-plugins-introduction/js/app.js"></script><scriptsrc="http://colobu.com/2014/11/17/gulp-plugins-introduction/js/controllers/thing-controller.js"></script><scriptsrc="http://colobu.com/2014/11/17/gulp-plugins-introduction/js/models/thing-model.js"></script><scriptsrc="http://colobu.com/2014/11/17/gulp-plugins-introduction/js/views/thing-view.js"></script><!-- endbuild --><!-- build:remove --><scriptsrc="http://colobu.com/2014/11/17/gulp-plugins-introduction/js/localhostDependencies.js"></script><!-- endbuild -->
gulp-usemin用法如下:
var usemin =require('gulp-usemin');var uglify =require('gulp-uglify');var minifyHtml =require('gulp-minify-html');var minifyCss =require('gulp-minify-css');var rev =require('gulp-rev');gulp.task('usemin',function(){gulp.src('./*.html').pipe(usemin({css:[minifyCss(),'concat'],html:[minifyHtml({empty:true})],js:[uglify(), rev()]})).pipe(gulp.dest('build/'));});
gulp-uglify
uglify是一款javascript代码优化工具,可以解析,压缩和美化javascript。
用法:
var uglify =require('gulp-uglify');gulp.task('compress',function(){gulp.src('lib/*.js').pipe(uglify()).pipe(gulp.dest('dist'))});
gulp-sourcemaps
在现代javascript开发中, JavaScript脚本正变得越来越复杂。大部分源码(尤其是各种函数库和框架)都要经过转换,才能投入生产环境。
常见的转换情况:
- 压缩,减小体积。
- 多个文件合并,减少HTTP请求数。
- 其他语言编译成JavaScript。最常见的例子就是CoffeeScript。
这三种情况,都使得实际运行的代码不同于开发代码,除错(debug)变得困难重重。
Source map就是一个信息文件,里面储存着位置信息。也就是说,转换后的代码的每一个位置,所对应的转换前的位置。有了它,出错的时候,除错工具将直接显示原始代码,而不是转换后的代码。
var gulp =require('gulp');var plugin1 =require('gulp-plugin1');var plugin2 =require('gulp-plugin2');var sourcemaps =require('gulp-sourcemaps');gulp.task('javascript',function(){gulp.src('src/**/*.js').pipe(sourcemaps.init()).pipe(plugin1()).pipe(plugin2()).pipe(sourcemaps.write()).pipe(gulp.dest('dist'));});
其它一些关注度高的gulp插件
gulp-inject
可以注入css,javascript和web组件,不需手工更新ndex.html。
<!DOCTYPE html><html><head><title>My index</title><!-- inject:css --><!-- endinject --></head><body><!-- inject:js --><!-- endinject --></body></html>
var gulp =require('gulp');var inject =require("gulp-inject");gulp.task('index',function(){var target = gulp.src('./src/index.html');// It's not necessary to read the files (will speed up things), we're only after their paths:var sources = gulp.src(['./src/**/*.js','./src/**/*.css'],{read:false});return target.pipe(inject(sources)).pipe(gulp.dest('./src'));});
gulp-header
为管道中的文件增加header。
var header =require('gulp-header');gulp.src('./foo/*.js').pipe(header('Hello')).pipe(gulp.dest('./dist/')gulp.src('./foo/*.js').pipe(header('Hello <%= name %>\n',{ name :'World'})).pipe(gulp.dest('./dist/')gulp.src('./foo/*.js').pipe(header('Hello ${name}\n',{ name :'World'})).pipe(gulp.dest('./dist/')//var pkg =require('./package.json');var banner =['/**',' * <%= pkg.name %> - <%= pkg.description %>',' * @version v<%= pkg.version %>',' * @link <%= pkg.homepage %>',' * @license <%= pkg.license %>',' */',''].join('\n');gulp.src('./foo/*.js').pipe(header(banner,{ pkg : pkg })).pipe(gulp.dest('./dist/')
相应的还有一个gulp-footer
插件。
gulp-filter
筛选vinyl stream中的文件。
var gulp =require('gulp');var jscs =require('gulp-jscs');var gulpFilter =require('gulp-filter');gulp.task('default',function(){// create filter instance inside task functionvar filter = gulpFilter(['*','!src/vendor']);return gulp.src('src/*.js')// filter a subset of the files.pipe(filter)// run them through a plugin.pipe(jscs())// bring back the previously filtered out files (optional).pipe(filter.restore()).pipe(gulp.dest('dist'));});
gulp-changed
只允许改变的文件通过管道。
var gulp =require('gulp');var changed =require('gulp-changed');var ngmin =require('gulp-ngmin');// just as an examplevar SRC ='src/*.js';var DEST ='dist';gulp.task('default',function(){return gulp.src(SRC).pipe(changed(DEST))// ngmin will only get the files that// changed since the last time it was run.pipe(ngmin()).pipe(gulp.dest(DEST));});
gulp-bower
执行bower安装。
var gulp =require('gulp');var bower =require('gulp-bower');gulp.task('bower',function(){return bower().pipe(gulp.dest('lib/'))});
gulp-if
有条件的执行任务
gulp-replace
字符串替换插件。
var replace =require('gulp-replace');gulp.task('templates',function(){gulp.src(['file.txt']).pipe(replace(/foo(.{3})/g,'$1foo')).pipe(gulp.dest('build/file.txt'));});
gulp-shell
可以执行shell命令
gulp-exec
exec插件
gulp-install
安装npm和bower包, 如果它们的配置文件存在的话。
var install =require("gulp-install");gulp.src(__dirname +'/templates/**').pipe(gulp.dest('./')).pipe(install());
gulp-rename
改变管道中的文件名。
var rename =require("gulp-rename");// rename via string
gulp.src("./src/main/text/hello.txt").pipe(rename("main/text/ciao/goodbye.md")).pipe(gulp.dest("./dist"));// ./dist/main/text/ciao/goodbye.md// rename via function
gulp.src("./src/**/hello.txt").pipe(rename(function(path){path.dirname +="/ciao";path.basename +="-goodbye";path.extname =".md"})).pipe(gulp.dest("./dist"));// ./dist/main/text/ciao/hello-goodbye.md// rename via hash
gulp.src("./src/main/text/hello.txt",{base: process.cwd()}).pipe(rename({dirname:"main/text/ciao",basename:"aloha",prefix:"bonjour-",suffix:"-hola",extname:".md"})).pipe(gulp.dest("./dist"));// ./dist/main/text/ciao/bonjour-aloha-hola.md
gulp-ignore
忽略管道中的部分文件。
gulp-util
提供一些辅助方法。
gulp-clean
提供clean功能。
var gulp =require('gulp');var clean =require('gulp-clean');gulp.task('clean',function(){return gulp.src('build',{read:false}).pipe(clean());});
gulp-concat
连接合并文件。
var concat =require('gulp-concat');gulp.task('scripts',function(){gulp.src('./lib/*.js').pipe(concat('all.js')).pipe(gulp.dest('./dist/'))});
gulp-wrap
将一个lodash模版包装成流内容。
gulp-declare
安全的声明命名空间,设置属性。
var declare =require('gulp-declare');var concat =require('gulp-concat');gulp.task('models',function(){// Define each model as a property of a namespace according to its filenamegulp.src(['client/models/*.js']).pipe(declare({namespace:'MyApp.models',noRedeclare:true// Avoid duplicate declarations})).pipe(concat('models.js'))// Combine into a single file.pipe(gulp.dest('build/js/'));});
更多的文章
- gulp-cheatsheet
- 9个优秀的gulp插件
- gulp插件开发指导
Gulp是一个构建工具, 功能类似grunt, 以及Java生态圈的ant, maven, gradle等。 其它的javascript生态圈的构建工具可以参考: List of JavaScript Build Tools
它采用了一种流式处理的方式, 编写起来简单直观。 相对于其它javascript构建工具, 母亲啊它的star数是仅次于grunt,流行度还是比较高的。
通过”代码优于配置” (code over configuration), 通过javascript编写构建任务, 充分利用javascript生态圈的组件, 可以实现简单灵活的任务管理。 通过node stream的方式,直接在内存中管道式处理流,不必缓存到硬盘上, 节省构建时间。
Gulp介绍请参考我转载的另一篇文章: Building With Gulp
另外有一篇很好的入门文章: Getting started with gulp, 繁体版, 简体中文硬翻版
从我的实践上来看, gulp要比grunt更好的管理构建过程。 编写简单,条理清晰,功能强大,学习无曲线。
Gulp是基于Node.js构建的,因此Gulp源文件和你用来定义任务的Gulp文件都被写进了JavaScript(或者CoffeeScript)里。 Gulp本身虽然不能完成很多任务,但它有大量插件可用,开发者可以访问插件页面或者在npm搜索gulpplugin或者gulpfriendly就能看到。例如,有些插件可以用来执行JSHint、编译CoffeeScript,执行Mocha测试,甚至更新版本号。现在有大约980个左右的插件可以使用。你可以到http://gulpjs.com/plugins/或者http://npmsearch.com/?q=keywords:gulpplugin查找所需的软件。
面对如此众多的插件, 想要全部了解并灵活运用它们几乎是不可能的事情。 实际开发中多参考别的项目的实现, 根据自己的需求寻找合适的插件, 总结并开发自己的插件, 逐步积累对gulp的认识。
本文列出常用的几个插件, 并在将来的开发中更新此文作为记录文档。 多个插件和grunt的插件功能类似。
发表评论
发表评论
- shandawang 发表 8小时前 mysql 语句记录
- Lychee Li 发表 8小时前 PHP 开发者的 Docker 之旅
- datagod 发表 9小时前 【魔镜知识拓展培训】如何接入数据源之txt篇
- datagod 发表 9小时前 医疗数据怎样处理?来自英特尔的大数据应用
- datagod 发表 9小时前 三个案例告诉你,数据分析究竟是有多重要?
- MoiMark官方 发表 9小时前 深度解读快速充电技术
- zhu329599788@126 发表 11小时前 text/html & text/plain的区别
- zhu329599788@126 发表 9小时前 Mysql跨表更新 多表update sql语句总结
- zhu329599788@126 发表 9小时前 mysql 从一个表中查数据,插入另一个表
- zhu329599788@126 发表 9小时前 [转]mysql(workbench)更新数据时候的一个异常
- 博主 发表 12小时前 快快快!27个提升效率的iOS开源库推荐
- mssp299 发表 11小时前 CVE-2015-5090漏洞利用
- 19小时前如何使用sql语句查看mysql表上的索引信息 by 甄码农
- 19小时前如何查看mysql表列的详细信息包括注释默认值 by 甄码农
- 1天前javascript int转换为char, char转换为int by 甄码农
- 3天前javascript获得浏览器可用区域高度/宽度 by 甄码农
- 5天前linux系统下查看文件或目录的用户/组信息 by 玉开Sir
- 5天前git add 命令添加所有改动内容 by 甄码农
- 5天前git "ssh-add ~/.ssh/id_rsa" Could not open a connection to your authentication agent问题解决 by 甄码农
- 6天前rsync同步失败: rsync: failed to connect to xxx: Permission denied (13)解决方法 by 金背二郎
- 6天前一个简单的无需第三方包的获取网页内容的代码 by Foyon
- 6天前Android开发中FPS游戏实现的两种方式比较 by panzaitang
- 6天前ResultSet 获取返回记录数量 by 廖钊权
- 9天前python 脚本获得当前用户 by 金背二郎
转载于:https://www.cnblogs.com/gaoxue/p/4668322.html
gulp plugins 插件介绍相关推荐
- gulp 常用插件汇总
2017-07-26更新:图片压缩插件使用gulp-smushit,gulp-smushit压缩率比较大,gulp-imagemin 图片压缩插件压缩率不明显. 见下图压缩率: 1.gulp安装 参照 ...
- Maven实战——常用Maven插件介绍
http://www.infoq.com/cn/news/2011/04/xxb-maven-7-plugin 我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任 ...
- 常用Maven插件介绍(下)(转)
我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成,例如编译源代码是由maven- compiler-plugin完成的.进一步说,每个任务对应 ...
- Elasticsearch插件介绍及安装
转载来源 :Elasticsearch之插件介绍及安装 https://www.cnblogs.com/zlslch/p/6423631.html ES站点插件(以网页形式展现) 1.BigDesk ...
- android浏览器插件介绍
一 浏览器插件介绍: 1.1 概述 浏览插件本质是一个功能模块,是浏览器功能的一种扩充.其载体是dll或则so文件.它依附浏览器完成某一特定的功能.插件需要实现浏览器规定的一些函数,这些函数叫N ...
- Maven打包插件介绍
文章目录 1. maven介绍 2. 生命周期及插件 2.1 总览生命周期 2.2 打包插件 2.2.1 maven-jar-plugin 2.2.2 maven-assembly-plugin 2. ...
- gulp常用插件总结
近期在学习使用gulp,通过写这篇总结文章,让自己加深对gulp常用插件的印象. 1. gulp-refresh 插件地址:gulp-refresh 注:1.该插件需要配合Chrome浏览器的扩展程序 ...
- Jquery提交表单 Form.js官方插件介绍
來源:http://hi.baidu.com/dereky/blog/item/f9e8ab64c52f4ff3f736540c.html [JQuery框架应用]:form.js官方插件介绍 For ...
- (50)Vue Router插件介绍
一.Vue Router插件介绍 Vue Router 是 Vue.js 的官方插件,用来快速实现单页应用. 二.Vue Router学习内容 • 单页应用 • 前端路由 • Vue Router 三 ...
最新文章
- SPU表管理之查询获取SPU表列表数据
- tomcat服务器上https的SSL证书安装配置
- Python与R的区别和联系
- 过滤序列,惰性序列_Java 8的惰性序列实现
- JS制作日历小事件和数码时钟--JavaScript实例集锦(初学)
- 【今日CV 计算机视觉论文速览】Thu, 28 Mar 2019
- Flutter CupertinoSliverRefreshControl 苹果风格的刷新效果
- linux sed保存,linux sed
- 人脸对齐(十八)--Joint Face Alignment and 3D Face Reconstruction
- 【优化求解】基于matlab GUI模拟退火算法求解全局最大值最小值问题【含Matlab源码 1242期】
- 通过 Socket 实现 UDP 编程 入门
- C语言 段错误Segmentation Fault
- c语言项目图书馆借书,C语言打造—齐全的图书馆借阅系统
- 带你去看——WRC 2016 世界机器人博览会
- html表格内容自动换行符,html表格内容自动换行
- Windows未能启动,状态:0xc000014c的解决方法
- 【python实战】怎么用python自动登录CSDN
- 光学神经网络 Optical neural network
- python量化:如何利用时间序列索引找到股票日线行情中的每个月的第一个交易日?每年的最后一个交易日?
- Easyrecovery汉化版下载