这是我的文件目录结构图

 下面是我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使用入门-综合运用>使用Gulp构建一个项目的更多相关文章

  1. 使用 gulp 构建一个项目

    本章将介绍 gulp-watch-path stream-combiner2 gulp-sourcemaps gulp-autoprefixer 您还可以直接学习以下模块: 安装 Node 和 gul ...

  2. vue 快速入门 系列 —— 使用 vue-cli 3 搭建一个项目(上)

    其他章节请看: vue 快速入门 系列 使用 vue-cli 3 搭建一个项目(上) 前面我们已经学习了一个成熟的脚手架(vue-cli),笔者希望通过这个脚手架快速搭建系统(或项目).而展开搭建最好 ...

  3. vue 快速入门 系列 —— 使用 vue-cli 3 搭建一个项目(下)

    其他章节请看: vue 快速入门 系列 使用 vue-cli 3 搭建一个项目(下) 上篇 我们已经成功引入 element-ui.axios.mock.iconfont.nprogress,本篇继续 ...

  4. 2016-7-15(1)使用gulp构建一个项目

    gulp是前端开发过程中自动构建项目的工具,相同作用的还有grunt.构建工具依 靠插件能够自动监测文件变化以及完成js/sass/less/html/image/css/coffee等文件的语法检查 ...

  5. 使用gulp构建一个项目

    gulp是前端开发过程中自动构建项目的工具,相同作用的还有grunt.构建工具依靠插件能够自动监测文件变化以及完成js/sass/less/html/image/css/coffee等文件的语法检查. ...

  6. gulp详细入门教程

    本文链接:http://www.ydcss.com/archives/18 gulp详细入门教程 简介: gulp是前端开发过程中对代码进行构建的工具,是自动化项目的构建利器:她不仅能对网站资源进行优 ...

  7. 续Gulp使用入门三步压缩CSS

    gulp 压缩css 一.安装 gulp-minify-css 模块 提示:你需要使用命令行的 cd 切换到对应目录后进行安装操作. 在命令行输入 npm install gulp-minify-cs ...

  8. 【原】gulp快速入门

    今天刚入职了一家新公司,结果明天就要开始项目了.上级说要用gulp来打包代码,所以今晚花了一晚来看这个gulp, 可以说已经入门了.所以做一个小小的总结 : 首先全局安装gulp npm instal ...

  9. gulp详细入门教程(转载)

    本文转载自: gulp详细入门教程

随机推荐

  1. thread_Semaphore信号量

    Semaphore也是一个线程同步的辅助类,可以维护当前访问自身的线程个数,并提供了同步机制.  使用Semaphore可以控制同时访问资源的线程个数,例如,实现一个文件允许的并发访问数. 一个计数信 ...

  2. MVC应用程序结构与规划

    对MVC好长一段时间练习,说句实在的话,还有很多是感到陌生,很多是生疏...... 很多网友也是刚想学习MVC,看到Insus.NET每学习一种方法,一个技巧均写成博文,也很希望能获取到练习的源程序以 ...

  3. 重新想象 Windows 8 Store Apps (48) - 多线程之其他辅助类: SpinWait, SpinLock, Volatile, SynchronizationContext, CoreDispatcher, ThreadLocal, ThreadStaticAttribute

    [源码下载] 重新想象 Windows 8 Store Apps (48) - 多线程之其他辅助类: SpinWait, SpinLock, Volatile, SynchronizationCont ...

  4. FL2440驱动添加(4)LED 驱动添加

    硬件信息:FL2440板子,s3c2440CPU带四个LED,分别在链接GPB5,GPB6,GPB8,GPB10 内核版本:linux-3.8.0 led驱动代码如下: 值得注意地方地方: 1,定时器 ...

  5. (转)x11vnc配置--ubuntu14.04

    原文网址:http://www.cnblogs.com/elmaple/p/4354814.html x11vnc是连接到真实的X会话,相比vnc4server和tightvncserver自己创建不 ...

  6. Spring RMI Example

    一: 提供服务的远程一端 1-1. applicationContext.xml <?xml version="1.0" encoding="UTF-8" ...

  7. sphinx使用随笔

    为什么需要进行全文搜索呢? 一个表中有a.b.c多个字段.我们使用sql进行like搜索的时候,往往只能匹配某个字段.或者是这样的形式:a LIKE “%关键词%”or b LIKE “关键词” 这样 ...

  8. Picasso

    1.简介 Picasso是Square公司出品的一个强大的图片下载和缓存图片库1)在adapter中需要取消已经不在视野范围的ImageView图片资源的加载,否则会导致图片错位,Picasso已经解 ...

  9. [iOS] 使用xib作为应用程序入口 with IDE

    [iOS] 使用xib作为应用程序入口 with IDE 在「使用xib做为应用程序入口 with Code」这篇文章中,介绍了如何透过写Code的方式,来使用xib做为应用程序的入口.但其实在Xco ...

  10. JS写返回上一级

    应产品需求,自己的网站上要有返回上一级的需求,几经周折,做个小总结. (1): $("XX").on("click",function(){      wind ...