转载:gulp文件
这是我的文件目录结构图
下面是我gulpfile.js的配置

'use strict'
var gulp=require('gulp');
var gutil=require('gulp-util');
var uglify=require('gulp-uglify');
var watchPath=require('gulp-watch-path');
var combiner=require('stream-combiner2');
var sourcemaps=require('gulp-sourcemaps');
var minifycss=require('gulp-minify-css');
var autoprefixer=require('gulp-autoprefixer');
var sass=require('gulp-sass');
var imagemin=require('gulp-imagemin');
var handleError=function(err){
var colors=gutil.colors;
console.log('\n')
gutil.log(colors.red('Error!'))
gutil.log('fileName: ' + colors.red(err.fileName))
gutil.log('lineNumber: ' + colors.red(err.lineNumber))
gutil.log('message: ' + err.message)
gutil.log('plugin: ' + colors.yellow(err.plugin))
}
var combiner=require('stream-combiner2')
gulp.task('gutil',function(){
gutil.log('message');
gutil.log(gutil.colors.red('error'))
gutil.log(gutil.colors.green('message:')+"some")
})
//压缩js
gulp.task('uglifyjs',function(){
var combined=combiner.obj([
gulp.src('src/js/**/*.js'),
sourcemaps.init(),
uglify(),
sourcemaps.write('./'),
gulp.dest('dist/js/')
])
combined.on('error',handleError)
})
//压缩css
gulp.task('minifycss',function(){
gulp.src('src/css/**/*.css')
.pipe(sourcemaps.init())
.pipe(autoprefixer({
browsers: 'last 2 versions'
}))
.pipe(minifycss())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('dist/css'));
})
//编译scss
gulp.task('sass',function(){
gulp.src('src/sass/**/*.scss')
.on('error',function(err){
console.error('Error!',err.message)
})
.pipe(sourcemaps.init())
.pipe(autoprefixer({
browsers: 'last 2 versions'
}))
.pipe(sass({outputStyle: 'compact'}))
.pipe(minifycss())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('dist/css'))
})
//压缩图片
gulp.task('image', function () {
gulp.src('src/images/**/*')
.pipe(imagemin({
progressive: true
}))
.pipe(gulp.dest('dist/images'))
})
// gulp.watch('src/js/**/*.js',function(event){
// console.log(event);
// 当修改 src/js/log.js 文件时
// event {
// // 发生改变的类型,不管是添加,改变或是删除
// type: 'changed',
// // 触发事件的文件路径
// path: 'D:/wamp/www/gulpBuildProject/src/js/log.js'
// }
// })
//捕获错误信息
var handleError=function(err){
var colors=gutil.colors;
console.log('\n');
gutil.log(colors.red('Error!'))
gutil.log('filename: '+colors.red(err.filename))
gutil.log('lineNumber: '+ colors.red(err.lineNumber))
gutil.log('message: ' + err.message)
gutil.log('plugin: ' + colors.yellow(err.plugin))
}
//监听js修改
gulp.task('watchjs',function(){
gulp.watch('src/js/**/*.js',function(event){
var paths=watchPath(event,'src/','dist/')
paths={
srcPath: 'src/js/log.js',
srcdir:'src/js/',
distPath:'dist/js/log.js',
distDir:'dist/js',
srcFilename:'log.js',
distFilename:'log.js'
}
gutil.log(gutil.colors.green(event.type)+ ' ' + paths.srcPath)
gutil.log('Dist '+ paths.distPath)
var combined=combiner.obj([
gulp.src(paths.srcPath),
uglify(),
gulp.dest(paths.distDir)
])
// gulp.src(paths.srcPath)
// .pipe(uglify())
// .pipe(gulp.dest(paths.distDir))
combined.on('error',handleError);
})
})
//监听css修改
gulp.task('watchcss',function(){
gulp.watch('src/css/**/*.css',function(event){
var paths=watchPath(event,'src/','dist/');
gutil.log(gutil.colors.green(event.type) + ' ' + paths.srcPath)
gutil.log('Dist ' +paths.distPath)
gulp.src(paths.srcPath)
.pipe(sourcemaps.init())
.pipe(autoprefixer({
browsers: 'last 2 versions'
}))
.pipe(minifycss())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(paths.distDir));
})
})
//监听编译sass
gulp.task('watchsass',function(){
gulp.watch('src/sass/**/*',function(event){
var paths=watchPath(event,'src/sass/','dist/css/')
gulp.log(gutil.colors.green(event.type)+ ' ' + paths.srcPath)
gulp.log('Dist ' + paths.distPath)
sass(paths.srcPath)
.on('error',function(err){
console.error('Error!', err.message);
})
.pipe(sourcemaps.init())
.pipe(sass())
.pipe(minifycss())
.pipe(autoprefixer({
browsers:'last 2 versions'
}))
.pipe(sourcemaps.write('./'))
pipe(gulp.dest(paths.distDir))
})
})
//监听压缩图片
gulp.task('watchimage', function () {
gulp.watch('src/images/**/*', function (event) {
var paths = watchPath(event,'src/','dist/')
gutil.log(gutil.colors.green(event.type) + ' ' + paths.srcPath)
gutil.log('Dist ' + paths.distPath)
gulp.src(paths.srcPath)
.pipe(imagemin({
progressive: true
}))
.pipe(gulp.dest(paths.distDir))
})
})
//配置文件复制任务
gulp.task('watchcopy',function(){
gulp.watch('src/fonts/**/*',function(event){
var paths=watchPath(event)
gulp.log(gutil.colors.green(event.type)+ ' ' + paths.srcPath)
gulp.log('Dist ' + paths.distPath)
gulp.src(paths.srcPath)
.pipe(gulp.dest(paths.distDir))
})
})
gulp.task('copy',function(){
gulp.src('src/fonts/**/*')
.pipe(gulp.dest('dist/fonts/'))
})
gulp.task('default',['watchjs','watchcss','watchsass','watchimage','watchcopy'])

另外以下插件有需要也可以用得上
【gulp-plumber】例外處理
【gulp-livereload】自動重新載入頁面
【gulp-notify】gulp 處理通知
【browser-sync】瀏覽器同步檢視
转载:gulp文件的更多相关文章
- [转载]DriverStore文件夹特别大,能删除吗?
[转载]DriverStore文件夹特别大,能删除吗? 转自博客园https://www.cnblogs.com/lovebing/p/6951833.html 这篇文章,清理完C盘多了20G!不要太 ...
- [转载]config文件的一个很好的实现
以下是转载于网上的一个很好的config文件的实现,留存以备案 //Config.h #pragma once #include <string> #include <map> ...
- 【转载】文件上传那些事儿,文件ajax无刷上传
导语 正好新人导师让我看看能否把产品目前使用的FileUploader从老的组件库分离出来的,自己也查阅了相关的各种资料,对文件上传的这些事有了更进一步的了解.把这些知识点总结一下,供自己日后回顾,也 ...
- 转载--gulp入门
关于gulp的入门文章,先转载了 http://markpop.github.io/2014/09/17/Gulp%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B/
- (转载)C++文件读写函数之——fopen、fread和fwrite、fgetc和fputc、fgets和fputs、ftellf和fseek、rewind
http://blog.sina.com.cn/s/blog_61437b3b0102v0bt.html http://blog.csdn.net/chenwk891/article/details/ ...
- [转载]CAD文件版本
http://blog.sina.com.cn/s/blog_4c9fa4dd0101il1v.html 在工作中会遇到打开一张图纸时出现“图形文件无效”的提示,大部都是因为用低版本软件打开了高版本的 ...
- [转载] Linux 文件系统结构介绍
原文: https://linux.cn/article-6132-weibo.html Linux中的文件是什么?它的文件系统又是什么?那些配置文件又在哪里?我下载好的程序保存在哪里了?在 Linu ...
- 转载:python文件打开方式详解——a、a+、r+、w+区别
第一步 排除文件打开方式错误: r只读,r+读写,不创建 ###f.readline()是读取第一行,f.readlines()是读取全部并返回一个列表 w新建只写,w+新建读写,会将文件内 ...
- 转载 .htaccess文件RewriteRule语法规则
详见: http://blog.csdn.net/scchary/article/details/40045807 # -- 位于行首时表示注释. [F] -- Forbidden( ...
随机推荐
- [LeetCode] Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- c#中多线程同步Lock(锁)的研究以及跨线程UI的操作
本文只针对C#中,多线程同步所用到的锁(lock)作为研究对象.由于想更直观的显示结果,所以,在做demo的时候,就把多线程通过事件操作UI的代码也写了出来,留作备忘和分享吧. 其实多线程的同步,使用 ...
- 【原】Spark之机器学习(Python版)(一)——聚类
kmeans聚类相信大家都已经很熟悉了.在Python里我们用kmeans通常调用Sklearn包(当然自己写也很简单).那么在Spark里能不能也直接使用sklean包呢?目前来说直接使用有点困 ...
- VS2015 使用及插件推荐
环境搭建与插件推荐 Cpp in Win10 x64 vs2015 + Resharper/Visual Assist X vs2015Pro: Download or see BaiDu 中文专 ...
- 2016 daily
2016.01.06 leetcode 切题数达到 200+,截止目前 137.虽然一年 63 题看似不多,但是 easy 的题目基本已经切完,质量 >> 数量(专注 leetcode,可 ...
- 应如何取B/S的B端的IP
我们常讨论说要取真实IP,不同场景所谓的真实IP含义不一样. 如你要根据客户端IP去判断客户所在区域,那么要记录客户的出口IP,这里的出口IP才是你所谓的真实IP. 如你要判断多个客户端是不是同一个, ...
- IOS比较常用的第三方组件及应用源代码(持续更新中)
把平时看到或项目用到的一些插件进行整理,文章后面分享一些不错的实例,若你有其它的插件欢迎分享,不断的进行更新~ 一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com ...
- 理解C# 4 dynamic(4) – 让人惊艳的Clay
Clay非常类似于ExpandoObject, 可以看做是ExpandoObject的加强版. 它们能够让我们在不需要定义类的情况下,就构建出我们想要的对象.Clay和ExpandoObject相比, ...
- ngrok反向隧道--获取内网IP
ngrok反向隧道 前情提要:小明与小白各有一台主机,两台主机在同一内网,小明想直接通过内网ssh到小白的主机上.但是小白的ip地址会不断的变化,而小明不想每次都要麻烦小白查看ip.于是小明催生了一个 ...