这是我的文件目录结构图

 下面是我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文件的更多相关文章

  1. [转载]DriverStore文件夹特别大,能删除吗?

    [转载]DriverStore文件夹特别大,能删除吗? 转自博客园https://www.cnblogs.com/lovebing/p/6951833.html 这篇文章,清理完C盘多了20G!不要太 ...

  2. [转载]config文件的一个很好的实现

    以下是转载于网上的一个很好的config文件的实现,留存以备案 //Config.h #pragma once #include <string> #include <map> ...

  3. 【转载】文件上传那些事儿,文件ajax无刷上传

    导语 正好新人导师让我看看能否把产品目前使用的FileUploader从老的组件库分离出来的,自己也查阅了相关的各种资料,对文件上传的这些事有了更进一步的了解.把这些知识点总结一下,供自己日后回顾,也 ...

  4. 转载--gulp入门

    关于gulp的入门文章,先转载了 http://markpop.github.io/2014/09/17/Gulp%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B/

  5. (转载)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/ ...

  6. [转载]CAD文件版本

    http://blog.sina.com.cn/s/blog_4c9fa4dd0101il1v.html 在工作中会遇到打开一张图纸时出现“图形文件无效”的提示,大部都是因为用低版本软件打开了高版本的 ...

  7. [转载] Linux 文件系统结构介绍

    原文: https://linux.cn/article-6132-weibo.html Linux中的文件是什么?它的文件系统又是什么?那些配置文件又在哪里?我下载好的程序保存在哪里了?在 Linu ...

  8. 转载:python文件打开方式详解——a、a+、r+、w+区别

    第一步 排除文件打开方式错误: r只读,r+读写,不创建      ###f.readline()是读取第一行,f.readlines()是读取全部并返回一个列表 w新建只写,w+新建读写,会将文件内 ...

  9. 转载 .htaccess文件RewriteRule语法规则

    详见: http://blog.csdn.net/scchary/article/details/40045807    # -- 位于行首时表示注释.       [F] -- Forbidden( ...

随机推荐

  1. redis 学习笔记(7)-cluster 客户端(jedis)代码示例

    上节学习了cluster的搭建及redis-cli终端下如何操作,但是更常用的场景是在程序代码里对cluster读写,这需要redis-client对cluster模式的支持,目前spring-dat ...

  2. 自动判断应该Ajax还是return

    起因 最近回顾以前的代码,发现一个偶尔会见到的现象.一个类里面的方法可能需要Ajax返回,也有可能需要函数return.这个现象发生在网站MVC中的 逻辑层(或模型层),示例如下.IndexCtrl是 ...

  3. booting logo & booting animation

    開機第一張圖片: 圖片位置: linux_repo/vendor/mediatek/proprietary/bootable/bootloader/lk/dev/logo 因為 project 選用 ...

  4. FMDB的使用方法

    转自:http://blog.devtang.com/blog/2012/04/22/use-fmdb/ 前言 SQLite (http://www.sqlite.org/docs.html) 是一个 ...

  5. mybatis多对一关联

    mybatis多对一关联查询实现 1.定义实体 定义实体的时候需要注意,若是双向关联,就是说双方的属性中都含有对方对象作为域属性出现, 那么在写toString()方法时需要注意,只让某一方输出即可, ...

  6. python实战(开发新浪微博应用)

    #coding=utf-8 """ 本应用主要功能 1.用户选择喜欢的标签加关注 2.获取用户粉丝中自己还没有关注的,->加关注,提高粉丝稳定性 3.获取用户关注列 ...

  7. Git中如何利用生成SSH个人公钥访问git仓库

    Git中如何利用生成SSH个人公钥访问git仓库方法(这里以coding平台为例): 1. 获取 SSH 协议地址 在项目的代码页面点击 SSH 切换到 SSH 协议, 获得访问地址, 请使用这个地址 ...

  8. android GPS定位源码 地图显示位置源码 有用到的小伙伴自提取

    package com.jasgroup.cn.amhdeam; import java.io.IOException; import java.util.Iterator; import andro ...

  9. .net 项目生成时自动更新版本号

    https://www.codeproject.com/articles/31236/how-to-update-assembly-version-number-automaticall Exampl ...

  10. Linux--Tail命令

    inux tail命令用途是依照要求将指定的文件的最后部分输出到标准设备,通常是终端,通俗讲来,就是把某个档案文件的最后几行显示到终端上,假设该档案有更新,tail会自己主动刷新,确保你看到最新的档案 ...