var c = {
rootPath: 'src',//项目文件夹
outputPath: 'output',//文件编译后的输出目录
revPath: 'manifest',//rev映射文件目录
appjs: 'app.js',// run JS
homePage: 'index.html',
cssFolderPaths: ['css'],//需要压缩的CSS目录
jsFolderPaths: ['model'],//需要压缩的JS目录
ngjsFolderPaths: ['app', 'common', 'directives', 'filters'],//需要压缩的ngJS目录
htmlFolderPaths: ['app', 'layout'],
filterFolderPaths: ['frameworks', 'images']
}; var root = function (path) {
if (path)
return c.rootPath + '/' + path
return c.rootPath;
}; var output = function (path) {
if (path)
return c.outputPath + '/' + path
return c.outputPath;
} var fs = require('fs');
var path = require('path');
var gulp = require("gulp"),
clean = require('gulp-clean'),
ngAnnotate = require('gulp-ng-annotate'),//压缩ngjs
util = require('gulp-util'),
filter = require('gulp-filter'),
ngmin = require('gulp-ngmin'),
stripDebug = require('gulp-strip-debug'),
minifyhtml = require("gulp-minify-html"),//压缩html
minify = require("gulp-minify-css"),//压缩css
uglify = require("gulp-uglify"),//压缩代码
rename = require("gulp-rename"),//重命名
concat = require("gulp-concat"),//合并代码
sourcemaps = require("gulp-sourcemaps"),
rev = require("gulp-rev"),//对文件名加MD5后缀
revReplace = require("gulp-rev-replace"),//路径替换
merge = require("merge-stream"); //入口命令
gulp.task('run', ['clear'],
function () {
setTimeout(function () {
gulp.run([
'filter',
'css',
'html',
'global',
'model',
'ngjs'], function () {
var manifest = gulp.src("./" + c.revPath + '/*.json');
return gulp.src([root(c.homePage)])
.pipe(revReplace({
manifest: manifest
}))
.pipe(gulp.dest(c.outputPath));
});
}, 2000);
}); //所有不需要压缩及合并的目录
gulp.task('filter', function () {
return c.filterFolderPaths.map(function (cpath) {
return gulp.src([root(cpath + '/**')])
.pipe(gulp.dest(output(cpath)));
});
}); //压缩css
gulp.task('css', function () {
var tasks = c.cssFolderPaths.map(function (cpath) {
return gulp.src([root(cpath + '/**/*.css')])
.pipe(minify())
.pipe(rev())
.pipe(gulp.dest(output(cpath)))
.pipe(rev.manifest(cpath + '.json'))
.pipe(gulp.dest('./' + c.revPath));
});
return merge(tasks);
}); //压缩html
gulp.task('html', function () {
var tasks = c.htmlFolderPaths.map(function (cpath) {
return gulp.src([root(cpath + '/**/*.html')])
.pipe(minifyhtml())
.pipe(gulp.dest(output(cpath)));
});
return merge(tasks);
}); //压缩global js文件,并替换index.html中的引用
gulp.task('global', function () {
return gulp.src([root('/*.js')])
.pipe(ngAnnotate())
.pipe(ngmin({ dynamic: false }))
.pipe(stripDebug())
.pipe(uglify({ outSourceMap: false }))//压缩ngjs
// .pipe(concat(c.appjs))
.pipe(rev()) //文件名加MD5后缀
.pipe(gulp.dest(c.outputPath))
.pipe(rev.manifest('global.json'))//生成manifest.json
.pipe(gulp.dest('./' + c.revPath));
}); //压缩model
gulp.task('model', function () {
var tasks = c.jsFolderPaths.map(function (cpath) {
return gulp.src([root(cpath + '/**/*.js')])
.pipe(uglify())
.pipe(rev())
.pipe(gulp.dest(output(cpath)))
.pipe(rev.manifest(cpath + '.json'))
.pipe(gulp.dest('./' + c.revPath));
});
return merge(tasks);
}); //压缩ngjs
gulp.task('ngjs', function () {
var tasks = c.ngjsFolderPaths.map(function (cpath) {
return gulp.src([root(cpath + '/**/*.js')])
.pipe(uglify())
.pipe(rename(function (path) {
if (path.basename.toLowerCase().indexOf('login') > -1) {
path.basename = path.basename.toLowerCase();
}
return path;
}))
.pipe(rev())
.pipe(gulp.dest(output(cpath)))
.pipe(rev.manifest(cpath + '.json'))
.pipe(gulp.dest('./' + c.revPath));
});
return merge(tasks);
}); //清理目录
gulp.task('clear', function () {
gulp.src(c.outputPath, { read: false })
.pipe(clean());
gulp.src(c.revPath, { read: false })
.pipe(clean());
gulp.src(['dist', 'rev'], { read: false })
.pipe(clean());
//gulp.src(root('/**/*.*scc'), { read: false })
// .pipe(clean());
});

  

记录下自己写的gulp打包脚本的更多相关文章

  1. Linux下Maven+SVN自动打包脚本

        公司的开发环境每次部署项目都很麻烦,需要手动打包并上传上去.这个太麻烦了,所以就准备搞个自动打包的脚本.脚本自动从svn代码库里面更新最新的代码下来,然后maven打包,最后把war包丢到to ...

  2. gulp 粗粗学习 记录下

    看视频学习 粗粗记录下 以便以后学习 1.初记录 gulp.task //定义一个任务 gulp.src //锁定到做task任务的文件路径 gulp.dest //锁定到任务做完后文件去向的路径 g ...

  3. xcode8.3 shell 自动打包脚本 记录

    题记 xcode升级8.3后发现之前所用的xcode自动打包基本无法使用,因此在网上零碎找到些资料,将之前的脚本简化.此次脚本是基于xcode证书配置进行打包(之前是指定描述文件.相对繁琐).因此代码 ...

  4. webpack打包和gulp打包工具详细教程

    30分钟手把手教你学webpack实战 阅读目录 一:什么是webpack? 他有什么优点? 二:如何安装和配置 三:理解webpack加载器 四:理解less-loader加载器的使用 五:理解ba ...

  5. gulp打包详解

    gulp的作用 删除文件中冗余的内容,压缩文件,减小文件体积 实际项目中运行的都是压缩完成以后的文件 减小加载响应时间 gulp打包压缩对象 html,css,js,sass,webserver 音频 ...

  6. vmware虚拟机下ubuntu 13.04使用zeranoe脚本交叉编译ffmpeg

    2013-07-01今天是建党节,习总书记指出,党的建设要以“照镜子.正衣冠.洗洗澡.治治病”为总要求.希望我们的党越来越纯洁,为人民谋福利.言归正传,每次项目中需要编译相应的ffmpeg,都很费时费 ...

  7. 使用linux下的crontab定时任务跑定时脚本

    使用linux下的crontab定时任务跑定时脚本 tags:定时任务 定时脚本 crontab linux定时脚本 linux 引言:应该有许多人曾经很好奇一些定时脚本是怎么做出来的.我们这次就来说 ...

  8. 使用gulp打包普通项目

    前言: 在使用gulp打包工具之前,我做的H5项目在浏览器中的缓存是很严重的,若改了一点css,加了一句js代码,不手动清除浏览器缓存是看不到效果的.老总也在项目演示当中遇到这些问题,一查找原因却是缓 ...

  9. assetBundle打包脚本与LUA

    AssetBundles与脚本 所有Unity的AssetBundle,无论是从本地获取 还是www,或者打包整个场景.物体上的脚本都不会被编译.所以AssetBundle打包的时候即使物体上有脚本. ...

随机推荐

  1. 文档流 css中间float clear和布局

    文档流 先说说什么是公文流转  什么流 它是一系列连续的东西 <div style="background-color:pink;width:40px;height:80px;&quo ...

  2. Hibeernate中的两种分页方式

    1. return getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(S ...

  3. asp.net webform生命周期

  4. Android adb端口转发调试助手Packet Sender

    相信大家做过安卓开发或者安卓自动化测试开发的都离不开adb这个Android Debug Bridge这个工具,该工具有个很重要的功能就是端口转发.比如你在目标安卓机器端建立了一个服务来处理获取当前界 ...

  5. ASP.NET 5:初始化数据库

    ASP.NET 5:初始化数据库 1.初始化数据库 1.2目录 这不是专述模式/架构设计的帖子,架构搭建以讲解文章为目的,先不过多分层. 截这个图也是便于你对应下面找代码文件路径! 1.2代码 先控制 ...

  6. javascript3

    计算阶乘函数:<script> function factorial(n){ var product=1; while (n>1){ product*=n;//product=pro ...

  7. php错误及异常捕捉

    原文:php错误及异常捕捉 在实际开发中,错误及异常捕捉仅仅靠try{}catch()是远远不够的. 所以引用以下几中函数. a)   set_error_handler 一般用于捕捉  E_NOTI ...

  8. 在Installshield的安装进度中显示自己设置的信息

    原文:在Installshield的安装进度中显示自己设置的信息 以Installscript msi project为例,在installshield所制作的安装包安装过程中显示安装进度的,就在On ...

  9. UC编程:通过fwrite()和write()比较标准库函数和系统调用的速度

    fwrte是C标准库中提供的函数,是对write函数的扩展与封装,write则是Unix系统提供的函数.按照常理来讲,系统调用肯定比使用库快的多,但是事实正好相反 Why?原因就在于缓冲的问题,fwi ...

  10. Buildroot阅读笔记

    之前有写一篇文章:http://www.cnblogs.com/tfanalysis/p/3625430.html理清如何make menuconfig的问题,现在今天在无意间多注意了一下buildr ...