Gulp学习2

之前已经配置过一篇啦, 只不过那次是针对browserify

搬运

http://markpop.github.io/2014/09/17/Gulp入门教程/

你的工程文件夹要安装Gulp

你需要有package.json 不能是空文件哦 至少得有个{}才行,要不然npm不知道如何填写依赖,--save-dev会报错的

$ npm install gulp --save-dev

国内环境要用cnpm哟!

需要哪些插件呢

  • sass的编译(gulp-ruby-sass)
  • 自动添加css前缀(gulp-autoprefixer)
  • 压缩css(gulp-minify-css)
  • js代码校验(gulp-jshint)
  • 合并js文件(gulp-concat)
  • 压缩js代码(gulp-uglify)
  • 压缩图片(gulp-imagemin)
  • 自动刷新页面(gulp-livereload)
  • 图片缓存,只有图片替换了才压缩(gulp-cache)
  • 更改提醒(gulp-notify)
  • 清除文件(del)

可以一口气安装他们

cnpm install gulp-ruby-sass gulp-autoprefixer gulp-minify-css gulp-jshint gulp-concat gulp-uglify gulp-imagemin gulp-notify gulp-rename gulp-livereload gulp-cache del --save-dev

gulp-notify

gulp plugin to send messages based on Vinyl Files or Errors to Mac OS X, Linux or Windows using the node-notifier module. Fallbacks to Growl or simply logging

del

Delete files/folders using globs

rimraf

The UNIX command rm -rf for node

gulp-cache

原博这么说的

Grunt的imagemin插件就利用了缓存来避免重复压缩,而Gulp要利用gulp-cache来完成,当然啦,不仅限定于缓存图片。

比如之前build的时候已经压缩了img中的图片 后来又添加了一张 有了cache就可以避免重复压缩已经有的三张

gulp-autoprefixer

Autoprefixer的每个版本都包含一份最新的 Can I Use 数据

Autoprefixer默认将支持主流浏览器最近2个版本

主流浏览器最近2个版本用“last 2 versions”;

全球统计有超过1%的使用率使用“>1%”;

大于某个版本用“ff>20”或"ff>=20".

    .pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))

OR

    .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))

目录结构

-src

--styles

-dist

--assets

---css

运行&&更新

注册一个任务

gulp.task('styles', function() {....});

假如你只想运行这一个task gulp styles

那么如何一口气运行多个任务呢

gulp.task('default', ['watch', 'scripts', 'images']);

自动更新是最重要的, 免得我再敲命令嘛

// Rerun the task when a file changes
gulp.task('watch', function() {
gulp.watch('src/styles/*.*', ['styles']);
}); gulp.task('default', ['watch','styles']);

显然这里是针对文件改变后重新编译 页面并没有刷新

样式

sass的编译使用 gulp-ruby-sass

不用gulp-sass的原因是这个太大了 下载安装要很久 而且容易安装失败(它还依赖node-sass)

原博可能因为时间早的缘故,其写法目前是不能跑通的

gulp-ruby-sass Offcial Site

var gulp = require('gulp');
var sass = require('gulp-ruby-sass'); gulp.task('sass', function () {
return sass('source/file.scss')
.on('error', sass.logError)
.pipe(gulp.dest('result'));
});

其它

//image
gulp.task('images', function() {
return gulp.src('src/styles/imgs/*')
.pipe(cache(imagemin({ optimizationLevel: 3, progressive: true, interlaced: true })))
.pipe(gulp.dest('dist/css/img'))
.pipe(notify({ message: 'Images task complete' }));
}); gulp.task('scripts', function() {
gulp.src('src/scripts/lib/*.js')
.pipe(concat('lib.js'))
.pipe(gulp.dest('dist/js/lib'));
// Minify and copy all JavaScript (except vendor scripts)
// with sourcemaps all the way down
return gulp.src('src/scripts/*.js')
.pipe(sourcemaps.init())
.pipe(jshint('.jshintrc'))
.pipe(jshint.reporter('default'))
.pipe(uglify())
.pipe(concat('all.min.js'))
.pipe(sourcemaps.write())
.pipe(gulp.dest('dist/js'))
.pipe(notify({ message: 'Scripts task complete' }));
});

reload

原博用的是gulp-livereload 但是我没有懂他是如何用这个livereload的

没看到原博是如何关联 自己的服务器和 gulp-livereload

可能是因为原博需要安装一个Chrome插件 所以不再代码中关联 这个解决办法并不好

gulp-connect可以帮助我们自动刷新

gulp-connect Official Site

实际上它是 connect 和 gulp-livereload 的再封装

我们需要建立一个服务器

gulp.task('connectDist', function () {
connect.server({
root: 'dist',
port: 8001,
livereload: true
});
});

然后如果文件改变了 就让当前服务器刷新

gulp.task('styles', function() {
return rsass('src/styles/main.scss')
.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(connect.reload())
.pipe(notify({ message: 'Styles task complete' }));
});

当然了你的默认任务里面要加上服务器这一个

gulp.task('default', ['connectDist','styles','images','scripts','html','watch']);

browser-sync

和gulp-connect差不多的方式

gulp.task('watch', function() {
browserSync.init({
server: {
baseDir: "./dist"
}
});
gulp.watch('sass/demo.scss', ['styles']).on('change', browserSync.reload);
gulp.watch('src/styles/*.scss', ['styles']).on('change', browserSync.reload);
gulp.watch('src/scripts/*.js', ['scripts']).on('change', browserSync.reload);
gulp.watch('src/styles/imgs/*', ['images']).on('change', browserSync.reload);
gulp.watch('src/index.html', ['html']).on('change', browserSync.reload); }); //使用 gulp-ruby-sass
gulp.task('styles', function() {
rsass('src/styles/main.scss', { sourcemap: true })
.on('error', sass.logError)
.pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
.pipe(minifycss())
// For inline sourcemaps
.pipe(sourcemaps.write())
// For file sourcemaps
.pipe(sourcemaps.write('maps', {
includeContent: false,
sourceRoot: 'src/styles'
})) .pipe(gulp.dest('dist/css/'))
.pipe(reload({stream: true}))
.pipe(notify({ message: 'Styles task complete' }));
});

Gulp的任务重一定要有return吗

http://stackoverflow.com/questions/21699146/gulp-js-task-return-on-src

https://github.com/gulpjs/gulp/blob/master/docs/API.md#async-task-support

return 用在存在依赖的task中

默认情况下Gulp同时并行运行所有任务 但是有些任务是依赖前面任务运行结果的

所以用return

gulp.task('two', ['one'], function() {
// task 'one' is done now
});

Gulp 之二的更多相关文章

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

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

  2. Gulp实战(二)

    一.配置环境 1.基于NodeJs安装Git,npm,gulp 2.安装各类插件 3.参考文档 http://www.tuicool.com/articles/UbaqyyJ http://www.t ...

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

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

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

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

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

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

  6. 前端工具gulp使用

    一.构建gulp环境 1.下载nodejs gulp基于node.js,要通过nodejs的npm安装gulp,所以要先安装node.js环境.(英文官网/中文官网链接). 通过cmd命令窗口确定安装 ...

  7. gulp.js简单操作

    一.安装gulp 1.深入设置任务之前,需先安装gulp: $ npm install gulp -g 2.这会将gulp安装到全域环境下,让你可以存取gulp的CLI.接著,需要在本地端的专案进行安 ...

  8. Gulp 学习总结

    Gulp 自动化工具开发非常方便,便于上手,值得使用. 一.Gulp安装 gulp是基于NodeJS运行的,所以需要想安装NodeJS.  http://nodejs.org/download/ 安装 ...

  9. 前端自动化构建工具Gulp简单入门

    昨天听同事分享了Gulp的一些简单使用,决定自己也试一试. 一.安装 gulp是基于nodejs的,所以要先下载安装node(直接搜node,在官网下载就好了) 1.全局安装gulp npm inst ...

随机推荐

  1. ASP.Net连接WebServer使用Https协议(证书)

    ASP.Net使用Https(证书)协议连接WebService 最近使用ASP.Net连接WebService,不过走的协议是Https的,我一般用的使用都是普通的http协议.所以刚开始有点不值从 ...

  2. struts2表单提交的乱码的问题的解决

    今天碰到一乱码问题,百思不得其解. 最后解决办法是设置了表单的提交方式,将method设置为post,解决问题.虽然默认的提交方式是post.但是如果不显式设置的话,就会出现我所出现的问题. 总结下处 ...

  3. springFramework 源码学习之源码下载与编译

    1.源码下载 Spring已经将源码从svn迁移到了git.对于习惯了svn的人来说依然可以svn checkout,最好checkout到英文路径下,本人中文路径编译不过,具体原因不明,路径: ht ...

  4. 提取 ECharts 中的svg地图信息

    地图的需求还是蛮大的,全国都要自己画的话,还是需要投入比较大的人力. ECharts中有地图,那我们能不能把里面的地图文件提取出来呢,主要逻辑在map.js中. 看源代码发现,ECharts中地图信息 ...

  5. 解析:用 CSS3 和 JavaScript 制作径向动画菜单

    原作者的解析(英文):http://creative-punch.net/2014/02/making-animated-radial-menu-css3-javascript/ 原作者的解析(译文) ...

  6. C/C++招聘的一些感受和经验【转】

    找工作本人认为最重要的就是前期准备了.     首先.简历一定要写的切合主题.招聘单位要的是你的技能,这个只要大概能符合就可以,关键他们需要的是你的开发经验,一定要在简历中完美的体现出你之前所参与的项 ...

  7. java通过JNI接口调用C语言-初级

    JNI(java native interface):即java本地调用C的接口. 先看整体运行: 下面是过程: #vim test.java public class test{ public na ...

  8. Confluent

    Confluent介绍(一)   最开始接触confluent是通过这篇博客,How to Build a Scalable ETL Pipeline with Kafka Connect,对于做大数 ...

  9. OSG显示文字——自定义显示文字函数

    #include <Windows.h> #include <osg/Geode> #include <osg/Geometry> #include <osg ...

  10. SendMessage的返回值,就是由相应的响应消息函数的返回值(解释的简洁明了)

    SendMessage Return Values The return value specifies the result of the message processing and depend ...